注册-发送短信验证码功能开发
前提准备
凯信通:提供短信验证码的平台,如果是学习用处有免费的测试次数。
实体类编写
import java.net.URLEncoder;
public class SmsClientSend {
private static String url = "发送的URL";
private static String userid = "你的平台id";
private static String account = "你的平台用户名";
private static String password = "你的平台密码";
private static String action = "send";
private static String sendType = "get";
private static String codingType = "UTF-8";
private static String backEncodType = "UTF-8";
// private String mobile = "";
// private String content = "";
/*
* @param url :必填--发送连接地址URL
*
* @param userid :必填--用户ID,为数字
*
* @param account :必填--用户帐号
*
* @param password :必填--用户密码
*
* @param mobile :必填--发送的手机号码,多个可以用逗号隔比如>130xxxxxxxx,131xxxxxxxx
*
* @param content :必填--实际发送内容,
*
* @param action :选填--访问的事件,默认为send
*
* @param sendType :选填--发送方式,默认为POST
*
* @param codingType :选填--发送内容编码方式,默认为UTF-8
*
* @param backEncodType :选填--返回内容编码方式,默认为UTF-8
*
* @return 返回发送之后收到的信息
*/
public String sendSms(String mobile, String content) {
try {
StringBuffer send = new StringBuffer();
if (action != null && !"".equals(action)) {
send.append("action=").append(action);
} else {
send.append("action=send");
}
send.append("&userid=").append(userid);
send.append("&account=").append(URLEncoder.encode(account, codingType));
send.append("&password=").append(URLEncoder.encode(password, codingType));
send.append("&mobile=").append(mobile);
send.append("&content=").append(URLEncoder.encode(content, codingType));
if (sendType != null && (sendType.toLowerCase()).equals("get")) {
return SmsClientAccessTool.getInstance().doAccessHTTPGet(url + "?" + send.toString(), backEncodType);
} else {
return SmsClientAccessTool.getInstance().doAccessHTTPPost(url, send.toString(), backEncodType);
}
} catch (Exception e) {
e.printStackTrace();
return "未发送,编码异常";
}
}
}
后台接口
@RequestMapping(value = "sendNotice")
@ResponseBody
public AjaxRes sendNotice(HttpServletRequest req, HttpSession session) {
AjaxRes ar = getAjaxRes();
String regCode = getcode();
String regPhone = req.getParameter("telephone");
if (StringHelper.isNotEmpty(regPhone)) {
if (!checkPhoneIsReged(regPhone)) {
session.setAttribute("regPhone", regPhone);
session.setAttribute("regCode", regCode);
String content = "您正在注册验证,验证码是" + regCode + ",请在一分钟内按页面提示提交验证码,切勿将验证码泄露他人。";
SendNoticeBean snBean = new SendNoticeBean();
SmsClientSend scs = new SmsClientSend();
try {
scs.sendSms(regPhone, "【短信头】" + content);
ar.setSucceedMsg("验证码发送成功,请注意手机短信!");
} catch (Exception e) {
e.printStackTrace();
ar.setFailMsg("验证码发送失败,请重新发送!");
}
} else {
ar.setFailMsg("该手机号码已经注册过,请更换注册手机或至登陆页面登陆!");
}
} else {
ar.setFailMsg("请输入手机号码!");
}
return ar;
}
倒计时秒数js 方法
// 倒计时30秒
var countDown = 30;
function setTime(val) {
// 获取注册手机号
var telephone=$("#telphone").val().trim();
if(!JY.Validate.isPhone(telephone)){
return;
}
if (countDown == 0) {
val.removeAttribute("disabled");
val.value = "获取验证码";
countDown = 30;
return;
}else {
val.setAttribute("disabled", true);
val.value = "重新发送(" + countDown + "s)";
countDown--;
}
setTimeout(function() {
setTime(val)
}, 1000)
};
最后就是前台的ajax了,这个就可以自己写了。用来展示提示信息和与后台接口串联起来。