- 最近遇到用户注册时,需要调用短信接口,发送验证码,做了个调用淘宝短信接口的例子
- 首先需要对短信接口内容进行封装:
public class SendMsg2Util {
private static String url="http://gw.api.taobao.com/router/rest";
private static String appkey="xxxxxxxx";
private static String secret="xxxxxxxx";
public void sendMsg(String name,String phone,String vcode) throws ApiException{
TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
req.setExtend("XXX");
req.setSmsType("normal");
req.setSmsFreeSignName("注册验证");
req.setSmsParamString("{\"name\":\""+name+"\",\"code\":\""+vcode+"\"}");
req.setRecNum(phone);
req.setSmsTemplateCode("SMS_11415328");
AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
System.out.println(phone+"]验证码:"+vcode+">"+rsp.getMsg());
}
public void sendMsg(String name,String phone) throws ApiException{
TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
req.setExtend("XXX");
req.setSmsType("normal");
req.setSmsFreeSignName("变更验证");
req.setSmsParamString("{\"name\":\""+name+"\",\"phone\":\""+phone+"\"}");
req.setRecNum(phone);
req.setSmsTemplateCode("SMS_11535513");
AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
System.out.println(rsp.getBody());
}
public String sendVlidMsg(String name,String phone,String vcode) throws ApiException{
TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
req.setExtend("XXX");
req.setSmsType("normal");
req.setSmsFreeSignName("XXX");
req.setSmsParamString("{\"name\":\""+name+"\",\"code\":\""+vcode+"\"}");
req.setRecNum(phone);
req.setSmsTemplateCode("SMS_52125325");
AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
if(rsp.getErrorCode()==null ||rsp.getErrorCode().equals("0")){
return "0";
}else{
return rsp.getMsg();
}
}
public String createRandomVcode(){
String vcode = "";
for (int i = 0; i < 6; i++) {
vcode = vcode + (int)(Math.random() * 9);
}
return vcode;
}
}
<button type="button" class="vcode-btn" id="vcode-btn">获取验证码</button>
<div class="input_row" style="display: none;" id="vcode_div"><label for="name"><span class="iconfont icon-radiobutton2 medium_size"></span></label>
<div class="input_row_bd"><input id="vcode" type="text" placeholder="输入验证码" />
</div>
<div class="input_row_ft"><i class="weui-icon-warn"id="code_icon"></i></div>
</div>
$("#vcode-btn").click(function() {
$("#vcode_div").show();
$.ajax({
type : "post",
dataType : "text",
url : "getValidPhoneCode.do",
data : {
length : 4,
phone : phone,
},
success : function(result) {
var rsObj = typeof result === "object" ? result : $.parseJSON(result);
if (rsObj.status == 0) {
var step = 59;
$('#vcode-btn').html('重发60');
var _res = setInterval(function()
{
$("#vcode-btn").attr("disabled", true);
$('#vcode-btn').html('重发'+step);
step-=1;
if(step <= 0){
$("#vcode-btn").removeAttr("disabled");
$('#vcode-btn').html('获取验证码');
clearInterval(_res);
}
},1000);
}else{
alert(rsObj.responseDescription);
}
}
})
})
@RequestMapping("/getValidPhoneCode.do")
@ResponseBody
public void getValidPhoneCode(HttpServletRequest request,
HttpServletResponse response) {
ResponseHeader resData = new ResponseHeader();
resData.setStatus(-1);
int length = requestUtil.getIntValue(request, "length");
String phone = requestUtil.getStringValue(request, "phone");
String phonecode = EncodeUtilTools.getRanomNumCode(length);
request.getSession().setAttribute(phone, phonecode);
SendMsg2Util sendmsg=new SendMsg2Util();
String rs="0";
try {
rs=sendmsg.sendVlidMsg(name, phone, phonecode);
if(rs.equals("0")){
resData.setStatus(0);
}else{
resData.setResponseDescription(rs);
}
} catch (ApiException e) {
resData.setResponseDescription("短信接口异常,请稍后再试");
}
System.out.println(phone+"]验证码:"+phonecode+"_"+rs);
ResponseUtil.responseJson(response, resData);
return;
}
/**
* 生成随机数字
* @param 编码长度
* @return String
*/
public static String getRanomNumCode(int length) {
Random random = new Random();
String result = "";
for (int i = 0; i < length; i++) {
result += random.nextInt(10);
}
return result;
}
- 以上就完成的短信的发送功能,接下来就是对验证码进行验证
$.ajax({
url : DOMAIN_URL + "/wechat/getCheckSesCode.do",
data : {
code : vcode,
key : localphone,
},
type : "post",
dataType : "text",
success : function(result) {
var rsObj = typeof result === "object" ? result : $.parseJSON(result);
if (rsObj.status == 0) {
}else{
}
}
});
/**
* 检验手机校验的code是否正确
* @param request
* @param response
*/
@RequestMapping("/getCheckSesCode.do")
@ResponseBody
public void getCheckSesCode(HttpServletRequest request,
HttpServletResponse response) {
ResponseHeader resData = new ResponseHeader();
resData.setStatus(-1);
String key = requestUtil.getStringValue(request, "key");
String code = requestUtil.getStringValue(request, "code");
if(key!=null &&code!=null){
String scode=(String) request.getSession().getAttribute(key);
if(scode.equals(code)){
resData.setStatus(0);
}
}
ResponseUtil.responseJson(response, resData);
return;
}
/**
* 获得String类型请求参数
*
* @param request
* @param name
* @return
*/
public String getStringValue(HttpServletRequest request, String name) {
return StringUtil.isEmpty(name) ? null : request.getParameter(name);
}
- 通过以上步骤,就完成了发送验证码以及对验证码进行验证的功能