扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章主要介绍Java如何实现发送手机短信语音验证功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
目前成都创新互联公司已为近1000家的企业提供了网站建设、域名、网站空间、网站托管、服务器租用、企业网站设计、黑龙江网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
利用第三方平台可以实现发送手机短信验证码和语音验证码的功能,本文使用框架是struts2+spring+hibernate,现就action层给出核心代码功能。
public class VerifyAction extends BaseAction{ 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如何实现发送手机短信语音验证功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流