隨筆-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 藤本薔薇 閱讀(308) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 永城市| 贞丰县| 大洼县| 绥滨县| 东宁县| 三门峡市| 瑞安市| 鹿邑县| 平遥县| 四平市| 武城县| 苍梧县| 轮台县| 皮山县| 灵璧县| 苍山县| 深圳市| 丹巴县| 建始县| 运城市| 德钦县| 井冈山市| 新巴尔虎右旗| 沐川县| 洪泽县| 临汾市| 靖边县| 兴安县| 永泰县| 鄯善县| 临武县| 外汇| 太康县| 棋牌| 石城县| 遵义市| 吉木乃县| 昌图县| 敦化市| 柘城县| 金门县|