详解手机注册验证码操作思路与流程

手机注册验证码操作思路与流程

1、前端传入手机号参数并做验证码倒计时

	/**
	 * 重新获取验证码倒计时
	 * @returns
	 */
	reGetSMS : function () {
		var obj = $('#btn_getCode');
		// 重新发送倒计时
		var validCode = true;
		var time=60;
		if (validCode) {
			validCode = false;
			var t = setInterval(function  () {
				time --;
				$(obj).html('重新获取('+time+'s)');
				if (time==0) {
					clearInterval(t);
					$(obj).html("重新获取");
					validCode = true;
					sms_flag = true;
				}
			},1000);
		}
	}

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追加用户名
		sb.append("uid=56262");
		// 向StringBuffer追加密码(密码采用MD5 32位 小写)
		sb.append("&pwd=3019654cd7d57f8a8464e2b63f8c923c");
		// 向StringBuffer追加手机号码
		sb.append("&mobile=" + phone);
		// 向StringBuffer追加消息内容转URL标准码
		sb.append("&content=" + URLEncoder.encode(content,"gbk"));
		BufferedReader in = null;
		URL url = null;
		HttpURLConnection connection = null;
		int result = 0;
		try {
			url = new URL(sb.toString());
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			in = new BufferedReader(new InputStreamReader(url.openStream()));
			result = Integer.parseInt(in.readLine());
		} catch (Exception e) {
			throw new Exception("发送短信失败",e);
		} finally {
			if (in != null) {
				in.close();
			}
			if (connection != null) {
				connection.disconnect();
			}
		}
		return result == SUCCESS_SMS;
	}

4、保存验证码到数据库

要点:
a、需要存的参数手机号、验证码、开始时间、结束时间

			public class SMSDto {

				/** 手机号码 */	
				private String phone;
				/** 短信验证码 */	
				private String sms_code;
				/** 开始时间(当前秒数) */	
				private String begin_time;
				/** 到期时间(当前秒数 + 有效期) */	
				private String end_time;
				
				/**
				 * 默认构造方法
				 */
				public SMSDto() {
					super();
				}
				
				/**
				 * 生成验证码
				 * @param phone		手机
				 * @param sms_code	验证码
				 */
				public SMSDto(String phone, String sms_code) {
					super();
					this.phone = phone;
					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;
		
		// 1.生成验证码
		String random = SMSUtil.getSMSCode();
		// 2.发送验证码
		if(SMSUtil.sendSMS(phone, random)){
			// 3.保存验证码
			SMSDto sms = new SMSDto(phone, random);
			SMSDto smsDto = smsUserDao.getSMSCode(phone);
			if (smsDto == null) {
				// 新增验证码
				smsUserDao.addUserSMS(sms);
			} else {
				// 修改验证码
				smsUserDao.updateUserSMS(sms);
			}
			
			resultVO = new ResultVO();
		} else {
			resultVO = new ResultVO(GlobalMessage.MSG_07);
		}
		log.info(GlobalContract.LOG_END);
		return resultVO;
	}
SMSUtil类关键代码:

	public class SMSUtil {
	
		/** 短信模板 */
		private static final String CONTENT_0 = "(验证码)感谢您的支持,祝您生活愉快!【xxx】";
		/** SMS发送成功 */
		public static final int SUCCESS_SMS = 100;
		
	//	public static void main(String[] args) throws Exception {
	//		System.out.println(sendSMS("18018025014", "123456"));
	//	}
		
		/**
		 * 发送验证码
		 * @param phone		手机
		 * @param random	验证码
		 * @return
		 */
		public static boolean sendSMS(String phone, String random) throws Exception {
			
			return send(phone, random.concat(CONTENT_0));
		}
		
		/**
		 * 生成验证码
		 * @return
		 */
		public static String getSMSCode() {
			
			return String.valueOf((int)(Math.random() * 9000) + 1000);
		}
		
		/**
		 * 发送短信
		 * @param phone		手机号码
		 * @param content	发送内容
		 * @return
		 */
		private static boolean send(String phone, String content) throws Exception {
			
			// 创建StringBuffer对象用来操作字符串
			StringBuffer sb = new StringBuffer("http://http.yunsms.cn/tx/?");
			// 向StringBuffer追加用户名
			sb.append("uid=56262");
			// 向StringBuffer追加密码(密码采用MD5 32位 小写)
			sb.append("&pwd=3019654cd7d57f8a8464e2b63f8c923c");
			// 向StringBuffer追加手机号码
			sb.append("&mobile=" + phone);
			// 向StringBuffer追加消息内容转URL标准码
			sb.append("&content=" + URLEncoder.encode(content,"gbk"));
			BufferedReader in = null;
			URL url = null;
			HttpURLConnection connection = null;
			int result = 0;
			try {
				url = new URL(sb.toString());
				connection = (HttpURLConnection) url.openConnection();
				connection.setRequestMethod("POST");
				in = new BufferedReader(new InputStreamReader(url.openStream()));
				result = Integer.parseInt(in.readLine());
			} catch (Exception e) {
				throw new Exception("发送短信失败",e);
			} finally {
				if (in != null) {
					in.close();
				}
				if (connection != null) {
					connection.disconnect();
				}
			}
			return result == SUCCESS_SMS;
		}
		
	}


  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值