时间:2021-05-19
这篇文章主要介绍了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; } Dh3TongSmsOperator dh3TongSmsOperator = new Dh3TongSmsOperator( commonConfigService.getString("dh3tong", "account"), DigestUtil.md5(commonConfigService.getString("dh3tong", "password"))); String code = RandomUtil.randomNum(6); if (dh3TongSmsOperator.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); Dh3TongVoiceOperator dh3TongVoiceOperator = new Dh3TongVoiceOperator( commonConfigService.getString("dh3tong", "account"), DigestUtil.md5(commonConfigService.getString("dh3tong", "password"))); if (dh3TongVoiceOperator.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; }}以上是干货分享,已测试验证过!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了java实现手机短信验证的具体代码,供大家参考,具体内容如下整体流程:客户填入手机号,通过客户端点击获取验证码按钮,验证手机号是否有效,有效
通过ICQ网关发送手机短信的php源程序复制代码代码如下:
本文实例讲述了js实现倒计时重新发送短信验证码功能的方法。分享给大家供大家参考,具体如下:js-手机发送短信倒计时button{width:100px;heig
相关阅读:基于jQuery实现发送短信验证码后的倒计时功能(无视页面关闭)下面一段代码是小编给大家带来的js发送短信验证码后实现倒计时功能,代码简单易懂。具体代
本文实例讲述了Java随机生成手机短信验证码的方法。分享给大家供大家参考,具体如下:/***创建指定数量的随机字符串*@paramnumberFlag是否是数字