Java实现发送手机短信语音验证功能代码实例

网友投稿 261 2022-12-27


Java实现发送手机短信语音验证功能代码实例

这篇文章主要介绍了java实现发送手机短信语音验证功能代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

利用第三方平台可以实现发送手机短信验证码和语音验证码的功能,本文使用框架是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;

}

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.getPhUGNKAydone(),

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;

}

}

以上是干货分享,已测试验证过!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:影视接口测试工具下载安装(影视接口测试工具下载安装手机版)
下一篇:ui测试和api测试工具(在线api测试工具)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~