您好,登錄后才能下訂單哦!
這篇文章主要介紹如何使用Java實現發送手機短信語音驗證功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
利用第三方平臺可以實現發送手機短信驗證碼和語音驗證碼的功能,本文使用框架是struts2+spring+hibernate,現就action層給出核心代碼功能。
public class VerifyAction extends BaseAction<VerifyRequest> { private static final long serialVersionUID = 1L; private CommonConfigService commonConfigService; private UserPhoneVerifyService userPhoneVerifyService; private UserVerifyService userVerifyService; private PhoneDetectService phoneDetectService; private AccountService accountService; // 發送短信驗證碼 public String sensSms() { try { result.setResult(JsonResult.SUCCESS); if (model.getType() == PhoneVerifyType.REGISTER) { if (userVerifyService.checkRegisterPhone(model.getPhone())) { result.setErrorMessage("該手機號已注冊"); result.setResult(JsonResult.ERROR); return SUCCESS; } } if (!phoneDetectService.check(model.getPhone())) { result.setErrorMessage("該手機號異常,不能進行驗證操作"); result.setResult(JsonResult.ERROR); return SUCCESS; } Dh4TongSmsOperator dh4TongSmsOperator = new Dh4TongSmsOperator( commonConfigService.getString("dh4tong", "account"), DigestUtil.md5(commonConfigService.getString("dh4tong", "password"))); String code = RandomUtil.randomNum(6); if (dh4TongSmsOperator.send(UUIDGenerator.generate(), model.getPhone(), String.format("尊敬的用戶,您的驗證碼是:%s", code), "【XX系統名】")) { userPhoneVerifyService.record(model.getPhone(), model.getType(), code, model.getUserId()); } else { result.setErrorMessage("很抱歉,發送短信失敗,請稍后再試"); result.setResult(JsonResult.ERROR); return SUCCESS; } result.setErrorMessage( "短信驗證碼發送成功,請在5分鐘內及時使用,謝謝!"); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } // 發送語音驗證碼 public String sensVoice() { try { result.setResult(JsonResult.SUCCESS); if (model.getType() == PhoneVerifyType.REGISTER) { if (userVerifyService.checkRegisterPhone(model.getPhone())) { result.setErrorMessage("該手機號已注冊"); result.setResult(JsonResult.ERROR); return SUCCESS; } } if (!phoneDetectService.check(model.getPhone())) { result.setErrorMessage("該手機號異常,不能進行驗證操作"); result.setResult(JsonResult.ERROR); return SUCCESS; } String code = RandomUtil.randomNum(6); Dh4TongVoiceOperator dh4TongVoiceOperator = new Dh4TongVoiceOperator( commonConfigService.getString("dh4tong", "account"), DigestUtil.md5(commonConfigService.getString("dh4tong", "password"))); if (dh4TongVoiceOperator.send(UUIDGenerator.generate(), request.getPhone(), code)) { userPhoneVerifyService.record(request.getPhone(), request.getType(), code, request.getUserId()); } else { result.setErrorMessage("很抱歉,發送語音失敗,請稍后再試"); result.setResult(JsonResult.ERROR); return SUCCESS; } result.setErrorMessage("語音驗證碼發送成功,請在5分鐘內及時使用,謝謝!"); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } public void setCommonConfigService(CommonConfigService commonConfigService) { this.commonConfigService = commonConfigService; } public void setUserPhoneVerifyService(UserPhoneVerifyService userPhoneVerifyService) { this.userPhoneVerifyService = userPhoneVerifyService; } public void setUserVerifyService(UserVerifyService userVerifyService) { this.userVerifyService = userVerifyService; } public void setPhoneDetectService(PhoneDetectService phoneDetectService) { this.phoneDetectService = phoneDetectService; } public void setAccountService(AccountService accountService) { this.accountService = accountService; }}
以上是“如何使用Java實現發送手機短信語音驗證功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。