隨筆-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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 莒南县| 开封县| 定边县| 白水县| 聂拉木县| 宜兰市| 辽中县| 刚察县| 靖江市| 贺兰县| 扎兰屯市| 象州县| 徐水县| 义乌市| 宁明县| 清镇市| 南部县| 定日县| 陆丰市| 阳江市| 扶沟县| 涪陵区| 保山市| 怀远县| 浮山县| 唐山市| 富顺县| 桓台县| 阿克苏市| 千阳县| 霍林郭勒市| 新安县| 墨玉县| 雷州市| 庆城县| 西乌珠穆沁旗| 隆德县| 观塘区| 进贤县| 赤壁市| 德昌县|