欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现发送手机验证码功能(短信+语音)

程序员文章站 2022-10-06 09:55:24
利用第三方平台可以实现发送手机短信验证码和语音验证码的功能,本文使用框架是struts2+spring+hibernate,现就action层给出核心代码功能。 以上是干货分享,已测试验证过! Java相关学习视频资料分享: https://www.cnblogs.com/bingyimeiling ......

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

}

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

java相关学习视频资料分享:

https://www.cnblogs.com/bingyimeiling/p/10289805.html