Java 手機驗證碼生成
阿新 • • 發佈:2019-02-16
List:
1.需求
2.實現
3.總結
……1.需求……
使用者提交訂單申請,需要輸入手機號,驗證二維碼。然後再判斷使用者輸入的手機號是否正確,正確則生成訂單資訊入庫。
……2.實現……
實現的思路是,1.傳送二維碼 2.驗證二維碼和redis中存放的是否一致 3.生成訂單入庫
1.傳送二維碼,其實是Java程式碼生成的隨機數字,再用httpclient請求發簡訊的服務,輸入使用者名稱密碼等引數資訊,第一步就完成了。需要將驗證碼存入redis中,在第二步驗證中用得到。
controller:
工具類:// http://managerapi.cuco.cn/order/getVerificationCode // {"memberMobile":"15633296156"} //獲取驗證碼--zhouzhou--2017年5月6日14:21:14 @API(value = "獲取驗證碼") @RequestMapping(value = "/v1/order/getVerificationCode", method = RequestMethod.POST) public Object getVerificationCode(@RequestBody Order order) { ParamVerifyUtils.paramNotNull(order.getMemberMobile(), "獲取驗證碼中手機號沒有填寫"); System.out.println("手機號為:" + order.getMemberMobile()); if (!PhoneFormatCheckUtils.isPhoneLegal(order.getMemberMobile())) { ResponseUtil.toFailureBody(ServerStatus.SHOW_ERROR, "請填寫真實手機號。"); } Member member = new Member(); member.setMobile(order.getMemberMobile()); Member memberinfo = this.memberService.getMemberByMobile(member.getMobile()); if (memberinfo != null) { ExceptionUtil.throwWarn("您已成功提交申請,銷售人員稍後會和您聯絡。"); } String identifyCode = SMSUtils.getFourRandom(); SMSUtils.sendMessage(member.getMobile(), identifyCode); JedisUtils.set("identifyCode" + order.getMemberMobile(), identifyCode, 120); System.out.println("生成驗證碼--" + identifyCode); return identifyCode; }
get請求記得utf-8轉碼喲,否則手機會收到一堆小方格格。
public class SMSUtils { protected Logger logger = Logger.getLogger(this.getClass()); public static boolean sendMessage(String mobile, String identifyCode) { boolean result = false; String userId = "馬賽克"; String password = "馬賽克"; String pszMobis = mobile; String pszMsg = "正式完成簡訊驗證" + "【" + identifyCode + "】";// + String iMobiCount = "1"; String pszSubPort = "*"; String MsgId = RandomStringUtils.randomNumeric(19); try { String url = "是一串URL馬賽克"// + "userId=" + userId// + "&password=" + password// + "&pszMobis=" + pszMobis// + "&pszMsg=" + pszMsg// + "&iMobiCount=" + iMobiCount// + "&pszSubPort=" + pszSubPort// + "&MsgId=" + MsgId; // RelyHttpClient.sendGet(url, null, "UTF-8"); // String responseBody = HttpClientUtils.sendGet(url, null,"UTF-8"); String responseBody = HttpClientUtils.sendGet(url, null,"UTF-8"); if (StringUtils.isNotBlank(responseBody)) { String sub = responseBody.substring(74, responseBody.lastIndexOf("</string>")); System.out.println("擷取返回值=================" + sub); if (sub.length() > 15) {// 返回值長度大於15則表示成功 result = true; System.out.println("驗證碼發至{}送成功" + mobile); } else { System.out.println("驗證碼發至{}失敗{}" + mobile + responseBody); } } } catch (Exception e) { System.out.println("驗證碼發至{}異常{}" + mobile + e.getMessage()); } return result; } /** * 產生4位隨機數(0000-9999) * * @return 4位隨機數 */ public static String getFourRandom() { Random random = new Random(); String fourRandom = random.nextInt(10000) + ""; int randLength = fourRandom.length(); if (randLength < 4) { for (int i = 1; i <= 4 - randLength; i++) fourRandom = "0" + fourRandom; } return fourRandom; }
2.驗證二維碼和redis中存放的是否一致
@API("提交訂單") @RequestMapping(value = "/v1/commit_order/pre_create",method = RequestMethod.POST) public Object preCreateOrder(@RequestBody Order order){ //1.判斷是否為空 ParamVerifyUtils.paramNotNull(order, "訂單不能為空"); ParamVerifyUtils.paramNotNull(order.getMemberName(), "會員姓名不能為空"); ParamVerifyUtils.paramNotNull(order.getMemberMobile(), "會員手機號不能為空"); ParamVerifyUtils.paramNotNull(order.getStatus(), "訂單狀態不能為空"); ParamVerifyUtils.paramNotNull(order.getOrderFrom(), "訂單來源不能為空"); ParamVerifyUtils.paramNotNull(order.getVerificationCode(), "驗證碼不能為空"); if (!PhoneFormatCheckUtils.isPhoneLegal(order.getMemberMobile())) { ResponseUtil.toFailureBody(ServerStatus.SHOW_ERROR, "請填寫真實手機號。"); } String identifyCode = JedisUtils.get("identifyCode" + order.getMemberMobile()); if (StringUtils.isEmpty(identifyCode) || !identifyCode.equals(order.getVerificationCode())) { ResponseUtil.toFailureBody(ServerStatus.SHOW_ERROR, "驗證碼不正確,請重新獲得驗證碼"); } //2.呼叫service插入 return orderService.createOrder(order); }
3.生成訂單入庫
//建立訂單--zhouzhou--2017年5月6日15:21:14
@Override
public Order createOrder(Order order) {
// 會員姓名、會員手機號、訂單狀態(1.待處理)、remark、order_from、訂單編號(年月日+8位隨機數)
//1.判斷是否為空
ParamVerifyUtils.paramNotNull(order, "訂單不能為空");
ParamVerifyUtils.paramNotNull(order.getMemberName(), "會員姓名不能為空");
ParamVerifyUtils.paramNotNull(order.getMemberMobile(), "會員手機號不能為空");
ParamVerifyUtils.paramNotNull(order.getStatus(), "訂單狀態不能為空");
ParamVerifyUtils.paramNotNull(order.getOrderFrom(), "訂單來源不能為空");
//2.生成訂單編號
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Date date =new Date();
String str_date = sdf.format(date);
StringBuilder str=new StringBuilder();//定義變長字串
Random random=new Random();
//隨機生成數字,並新增到字串
for(int i=0;i<8;i++){
str.append(random.nextInt(10));
}
//將字串轉換為數字並輸出
int num=Integer.parseInt(str.toString());
String order_num=str_date+num;
System.out.println(order_num);
//3.set 訂單編號
order.setOrderNum(order_num);
order.setCreated(date);
//4.插入
orderMapper.insertSelective(order);
return order;
}
效果,哦,就是我手機上的簡訊==
……3.總結……
實現很簡單,就是用了個httpclient請求埠。然後儲存在redis中,方便獲取。提高獲取的效率。多實踐,多總結。