1、前端傳入手機(jī)號(hào)參數(shù)并做驗(yàn)證碼倒計(jì)時(shí)
-
/**
-
* 重新獲取驗(yàn)證碼倒計(jì)時(shí)
-
* @returns
-
*/
-
reGetSMS : function () {
-
var obj = $('#btn_getCode');
-
// 重新發(fā)送倒計(jì)時(shí)
-
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);
-
}
-
/**
-
*參數(shù)是手機(jī)號(hào)碼和由驗(yàn)證碼組成的字符串
-
*/
-
private static boolean send(String phone, String content) throws Exception {
-
-
// 創(chuàng)建StringBuffer對(duì)象用來(lái)操作字符串
-
StringBuffer sb = new StringBuffer("http://http.yunsms.cn/tx/?");
-
// 向StringBuffer追加用戶名
-
sb.append("uid=56262");
-
// 向StringBuffer追加密碼(密碼采用MD5 32位 小寫)
-
sb.append("&pwd=3019654cd7d57f8a8464e2b63f8c923c");
-
// 向StringBuffer追加手機(jī)號(hào)碼
-
sb.append("&mobile=" + phone);
-
// 向StringBuffer追加消息內(nèi)容轉(zhuǎn)URL標(biāo)準(zhǔn)碼
-
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("發(fā)送短信失敗",e);
-
} finally {
-
if (in != null) {
-
in.close();
-
}
-
if (connection != null) {
-
connection.disconnect();
-
}
-
}
-
return result == SUCCESS_SMS;
-
}
要點(diǎn): a、需要存的參數(shù)手機(jī)號(hào)、驗(yàn)證碼、開始時(shí)間、結(jié)束時(shí)間
-
public class SMSDto {
-
-
/** 手機(jī)號(hào)碼 */
-
private String phone;
-
/** 短信驗(yàn)證碼 */
-
private String sms_code;
-
/** 開始時(shí)間(當(dāng)前秒數(shù)) */
-
private String begin_time;
-
/** 到期時(shí)間(當(dāng)前秒數(shù) + 有效期) */
-
private String end_time;
-
-
/**
-
* 默認(rèn)構(gòu)造方法
-
*/
-
public SMSDto() {
-
super();
-
}
-
-
/**
-
* 生成驗(yàn)證碼
-
* @param phone 手機(jī)
-
* @param sms_code 驗(yàn)證碼
-
*/
-
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、驗(yàn)證碼驗(yàn)證
// 1.驗(yàn)證【驗(yàn)證碼】 SMSDto smsDto = smsUserDao.getSMSCode(phone); a、驗(yàn)證驗(yàn)證碼是否正確 sms_code.equals(smsDto.getSms_code()) b、驗(yàn)證驗(yàn)證碼是否過(guò)期 if (((long) (System.currentTimeMillis() / 1000)) < Long.parseLong(smsDto.getEnd_time())) { //未過(guò)期 }else{ //已過(guò)期 }
實(shí)現(xiàn)層關(guān)鍵代碼:
-
//準(zhǔn)備驗(yàn)證碼
-
private ResultVO sendSmsCode(String phone) throws Exception{
-
log.info(GlobalContract.LOG_BEGIN);
-
ResultVO resultVO = null;
-
-
// 1.生成驗(yàn)證碼
-
String random = SMSUtil.getSMSCode();
-
// 2.發(fā)送驗(yàn)證碼
-
if(SMSUtil.sendSMS(phone, random)){
-
// 3.保存驗(yàn)證碼
-
SMSDto sms = new SMSDto(phone, random);
-
SMSDto smsDto = smsUserDao.getSMSCode(phone);
-
if (smsDto == null) {
-
// 新增驗(yàn)證碼
-
smsUserDao.addUserSMS(sms);
-
} else {
-
// 修改驗(yàn)證碼
-
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 = "(驗(yàn)證碼)感謝您的支持,祝您生活愉快!【xxx】";
-
/** SMS發(fā)送成功 */
-
public static final int SUCCESS_SMS = 100;
-
-
// public static void main(String[] args) throws Exception {
-
// System.out.println(sendSMS("18018025014", "123456"));
-
// }
-
-
/**
-
* 發(fā)送驗(yàn)證碼
-
* @param phone 手機(jī)
-
* @param random 驗(yàn)證碼
-
* @return
-
*/
-
public static boolean sendSMS(String phone, String random) throws Exception {
-
-
return send(phone, random.concat(CONTENT_0));
-
}
-
-
/**
-
* 生成驗(yàn)證碼
-
* @return
-
*/
-
public static String getSMSCode() {
-
-
return String.valueOf((int)(Math.random() * 9000) + 1000);
-
}
-
-
/**
-
* 發(fā)送短信
-
* @param phone 手機(jī)號(hào)碼
-
* @param content 發(fā)送內(nèi)容
-
* @return
-
*/
-
private static boolean send(String phone, String content) throws Exception {
-
-
// 創(chuàng)建StringBuffer對(duì)象用來(lái)操作字符串
-
StringBuffer sb = new StringBuffer("http://http.yunsms.cn/tx/?");
-
// 向StringBuffer追加用戶名
-
sb.append("uid=56262");
-
// 向StringBuffer追加密碼(密碼采用MD5 32位 小寫)
-
sb.append("&pwd=3019654cd7d57f8a8464e2b63f8c923c");
-
// 向StringBuffer追加手機(jī)號(hào)碼
-
sb.append("&mobile=" + phone);
-
// 向StringBuffer追加消息內(nèi)容轉(zhuǎn)URL標(biāo)準(zhǔn)碼
-
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("發(fā)送短信失敗",e);
-
} finally {
-
if (in != null) {
-
in.close();
-
}
-
if (connection != null) {
-
connection.disconnect();
-
}
-
}
-
return result == SUCCESS_SMS;
-
}
-
-
}