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

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

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; 
		} 
		 
	}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/13897.html

(0)
上一篇 2021年7月19日 16:19
下一篇 2021年7月19日 16:19

相关推荐

发表回复

登录后才能评论