隨筆-71  評論-5  文章-0  trackbacks-0

          import java.io.IOException;
          import java.util.Date;

          import org.apache.commons.httpclient.HttpClient;
          import org.apache.commons.httpclient.HttpException;
          import org.apache.commons.httpclient.NameValuePair;
          import org.apache.commons.httpclient.methods.PostMethod;
          import org.dom4j.Document;
          import org.dom4j.DocumentException;
          import org.dom4j.DocumentHelper;
          import org.dom4j.Element;

          import com.alibaba.fastjson.JSONObject;
          import com.huoniu.openapi.constant.Constant.MESSAGE;
          import com.huoniu.openapi.constant.InvokeContext;
          import com.huoniu.openapi.model.RetCode;
          import com.huoniu.openapi.model.RetMsg;
          import com.huoniu.openapi.model.SmsCode;
          import com.huoniu.openapi.service.SmsCodeService;
          import com.huoniu.openapi.utils.AESUtil;
          import com.huoniu.openapi.utils.SmsUtil;
          import com.huoniu.openapi.web.interceptor.InvokeContextInitInterceptor;
          import com.puff.framework.annotation.Before;
          import com.puff.framework.annotation.Controller;
          import com.puff.framework.annotation.Inject;
          import com.puff.framework.annotation.InterceptorChain;
          import com.puff.framework.utils.JsonUtil;
          import com.puff.framework.utils.StringUtil;
          import com.puff.web.view.TextView;
          import com.puff.web.view.View;
          import com.puff.web.view.ViewFactory;

          @Controller("/rest/sms")
          public class HuyiSmsController {    

              private static String content = "您的驗證碼是:%s。請不要把驗證碼泄露給其他人。";
              public View send(){
                  
                  String invokeData = InvokeContext.getInvokeData();
                  if (StringUtil.blank(invokeData)) {
                      return json(RetMsg.error(RetCode.OTHER_ERROR, "發送短信失敗!"));
                  }
                  
                  JSONObject jsonObject = JSONObject.parseObject(invokeData);
                  String  mobile = jsonObject.getString("customer_no");
                  if (StringUtil.blank(mobile)) {
                      return json(RetMsg.error(RetCode.NULL_PARAM, "手機號碼不能為空"));
                  }
                      
                      HttpClient client = new HttpClient(); 
                      PostMethod method = new PostMethod(MESSAGE.NEW_MESSAGEURL);  //接口地址
                          
                      client.getParams().setContentCharset("UTF-8");
                      method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");

                      int mobile_code = (int)((Math.random()*9+1)*100000);        
                                                         System.out.println("mobile_code : "+mobile_code);
                      NameValuePair[] data = {//提交短信
                              new NameValuePair("account", MESSAGE.NEW_ACCOUNT), 
                              new NameValuePair("password", SmsUtil.MD5Encode(MESSAGE.NEW_PASSWORD)),
                              new NameValuePair("mobile", mobile), 
                              new NameValuePair("content", String.format(content, mobile_code)),
                      };
                      
                      method.setRequestBody(data);        

                      try {
                          client.executeMethod(method);    
                          String SubmitResult =method.getResponseBodyAsString();
                          Document doc = DocumentHelper.parseText(SubmitResult); 
                          Element root = doc.getRootElement();
                          String code = root.elementText("code");    
                          String msg = root.elementText("msg");    
                          String smsid = root.elementText("smsid");    
                                      
                          if(code == "2"){  //發送成功,寫庫
                              
                          }
                          
                      } catch (HttpException e) {
                          e.printStackTrace();
                      } catch (IOException e) {
                          e.printStackTrace();
                      } catch (DocumentException e) {
                          e.printStackTrace();
                      }    
                      
                  
                      return json(RetMsg.success("發送成功!!!"));
                  
              }
              
              
              
              public View json(RetMsg msg) {
                  String data = JsonUtil.toJson(msg);
                  if (InvokeContext.isEncrypt()) {
                      data = AESUtil.encrypt(data);
                  }
                  return ViewFactory.text(data, TextView.ContentType.JSON);
              }

          }
          posted on 2015-08-31 10:02 藤本薔薇 閱讀(306) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 夏津县| 墨玉县| 西林县| 厦门市| 舞钢市| 宁武县| 武冈市| 斗六市| 呼和浩特市| 和田县| 太和县| 开封市| 界首市| 高邑县| 左贡县| 武义县| 黎川县| 英吉沙县| 岢岚县| 黄梅县| 天长市| 鹤岗市| 平塘县| 新野县| 兴海县| 郑州市| 江口县| 突泉县| 定陶县| 广东省| 新平| 漠河县| 虞城县| 会同县| 水城县| 错那县| 衡南县| 夏津县| 灵丘县| 武安市| 湄潭县|