随笔-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 藤本蔷薇 阅读(288) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: