发送短信验证码

<script type="text/javascript">
    var wait=60;
    function time(o) {
        if (wait == 0) {
            //o.attr("background-color", "#fff");
            o.removeAttribute("disabled");
            o.value="获取验证码";
            wait = 60;
        } else {
            $(o).attr("disabled",true);
            o.value="重新发送(" + wait + ")";
            wait--;
            setTimeout(function() {
                        time(o)
                    },
                    1000)
        }
    }
    function send(obj) {
        var phone =$('#phone').val(); //获取手机号码
        $.ajax({
            url:"",
            type: "post/get",
            data: {"pnone":phone},
            dataType: "json",
            success:function(data){
                if(data.status==1){
                    $(obj).attr("disabled",true);
                }else{
                    layer.alert(data.msg);
                };
            }
        });
    }
document.getElementById("btn1").onclick=function(){send(this);time(this);} ;
</script>


发送短信验证码可以使用阿里云的短信服务,以下是使用Spring Boot集成阿里云短信服务发送短信验证码的简单步骤: 1. 在阿里云控制台开通短信服务并获取AccessKey和AccessSecret。 2. 引入阿里云短信服务SDK依赖,例如: ``` <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.0.0</version> </dependency> ``` 3. 编写发送短信验证码的代码,例如: ``` @RestController public class SmsController { @Autowired private AliyunSmsConfig aliyunSmsConfig; // 阿里云短信服务配置 @PostMapping("/sms/send") public String sendSms(@RequestParam String mobile) throws ClientException { // 生成6位随机验证码 String code = String.valueOf((int)((Math.random()*9+1)*100000)); // 发送短信验证码 DefaultProfile profile = DefaultProfile.getProfile("default", aliyunSmsConfig.getAccessKey(), aliyunSmsConfig.getAccessSecret()); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); request.putQueryParameter("PhoneNumbers", mobile); request.putQueryParameter("SignName", aliyunSmsConfig.getSignName()); request.putQueryParameter("TemplateCode", aliyunSmsConfig.getTemplateCode()); request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}"); CommonResponse response = client.getCommonResponse(request); // 输出发送结果 System.out.println(response.getData()); return code; } } ``` 其中,`AliyunSmsConfig`是阿里云短信服务的配置类,需要配置AccessKey、AccessSecret、短信签名和短信模板编号等信息。 4. 在前端页面中添加发送短信验证码的按钮,并调用发送短信验证码的接口。 以上就是使用Spring Boot集成阿里云短信服务发送短信验证码的简单步骤,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值