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);
-
}
-
}
-
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追加用戶名
-
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;
-
}
要點: 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);
-
}
-
}
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;
-
}
-
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;
-
}
-
-
}