手机注册验证码操作思路与流程
1、前端传入手机号参数并做验证码倒计时
-
-
-
-
-
-
var obj = $('#btn_getCode');
-
-
-
-
-
-
var t = setInterval(function () {
-
-
$(obj).html('重新获取('+time+'s)');
-
-
-
-
-
-
-
-
-
2、随机生成验证码
-
public static String getSMSCode() {
-
return String.valueOf((int)(Math.random() * 9000) + 1000);
-
3、将生成的验证码通过第三方接口已短信形式发送给手机
-
-
-
-
private static boolean send(String phone, String content) throws Exception {
-
-
// 创建StringBuffer对象用来操作字符串
-
StringBuffer sb = new StringBuffer("http://http.yunsms.cn/tx/?");
-
-
-
// 向StringBuffer追加密码(密码采用MD5 32位 小写)
-
sb.append("&pwd=3019654cd7d57f8a8464e2b63f8c923c");
-
-
sb.append("&mobile=" + phone);
-
// 向StringBuffer追加消息内容转URL标准码
-
sb.append("&content=" + URLEncoder.encode(content,"gbk"));
-
BufferedReader in = null;
-
-
HttpURLConnection connection = null;
-
-
-
url = new URL(sb.toString());
-
connection = (HttpURLConnection) url.openConnection();
-
connection.setRequestMethod("POST");
-
in = new BufferedReader(new InputStreamReader(url.openStream()));
-
result = Integer.parseInt(in.readLine());
-
-
throw new Exception("发送短信失败",e);
-
-
-
-
-
if (connection != null) {
-
-
-
-
return result == SUCCESS_SMS;
-
4、保存验证码到数据库
要点: a、需要存的参数手机号、验证码、开始时间、结束时间
-
-
-
-
-
-
-
-
private String begin_time;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public SMSDto(String phone, String sms_code) {
-
-
-
this.sms_code = sms_code;
-
int cur = (int) (System.currentTimeMillis() / 1000);
-
this.begin_time = String.valueOf(cur);
-
this.end_time = String.valueOf(cur + GlobalContract.TIME_SMS);
-
-
b、先验证手机号码是否存在,存在则修改
5、验证码验证
// 1.验证【验证码】 SMSDto smsDto = smsUserDao.getSMSCode(phone); a、验证验证码是否正确 sms_code.equals(smsDto.getSms_code()) b、验证验证码是否过期 if (((long) (System.currentTimeMillis() / 1000)) < Long.parseLong(smsDto.getEnd_time())) { //未过期 }else{ //已过期 }
实现层关键代码:
-
-
private ResultVO sendSmsCode(String phone) throws Exception{
-
log.info(GlobalContract.LOG_BEGIN);
-
ResultVO resultVO = null;
-
-
-
String random = SMSUtil.getSMSCode();
-
-
if(SMSUtil.sendSMS(phone, random)){
-
-
SMSDto sms = new SMSDto(phone, random);
-
SMSDto smsDto = smsUserDao.getSMSCode(phone);
-
-
-
smsUserDao.addUserSMS(sms);
-
-
-
smsUserDao.updateUserSMS(sms);
-
-
-
resultVO = new ResultVO();
-
-
resultVO = new ResultVO(GlobalMessage.MSG_07);
-
-
log.info(GlobalContract.LOG_END);
-
-
SMSUtil类关键代码:
-
-
-
-
private static final String CONTENT_0 = "(验证码)感谢您的支持,祝您生活愉快!【xxx】";
-
-
public static final int SUCCESS_SMS = 100;
-
-
// public static void main(String[] args) throws Exception {
-
// System.out.println(sendSMS("18018025014", "123456"));
-
-
-
-
-
-
-
-
-
public static boolean sendSMS(String phone, String random) throws Exception {
-
-
return send(phone, random.concat(CONTENT_0));
-
-
-
-
-
-
-
public static String getSMSCode() {
-
-
return String.valueOf((int)(Math.random() * 9000) + 1000);
-
-
-
-
-
-
-
-
-
private static boolean send(String phone, String content) throws Exception {
-
-
// 创建StringBuffer对象用来操作字符串
-
StringBuffer sb = new StringBuffer("http://http.yunsms.cn/tx/?");
-
-
-
// 向StringBuffer追加密码(密码采用MD5 32位 小写)
-
sb.append("&pwd=3019654cd7d57f8a8464e2b63f8c923c");
-
-
sb.append("&mobile=" + phone);
-
// 向StringBuffer追加消息内容转URL标准码
-
sb.append("&content=" + URLEncoder.encode(content,"gbk"));
-
BufferedReader in = null;
-
-
HttpURLConnection connection = null;
-
-
-
url = new URL(sb.toString());
-
connection = (HttpURLConnection) url.openConnection();
-
connection.setRequestMethod("POST");
-
in = new BufferedReader(new InputStreamReader(url.openStream()));
-
result = Integer.parseInt(in.readLine());
-
-
throw new Exception("发送短信失败",e);
-
-
-
-
-
if (connection != null) {
-
-
-
-
return result == SUCCESS_SMS;
-
-
-