用户模块一(发送手机验证码功能的实现)

本文介绍了用户模块中发送手机验证码功能的实现过程,包括前端如何在点击按钮后进行倒计时,并验证手机号码在数据库中的存在情况。前端使用了失去焦点事件@blur来触发手机号验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.前端发送验证码的准备工作
1.点击发送验证码按钮之后,按钮倒计时功能的实现

 <!--buttom标签在from表单内部,默认提交时summmit提交(写一个type属性),单独在外面才是一个普通按钮-->
     <button class="btn" v-html="htmlValue" type="button" :disabled="disabled"
     href="javascript:void(0);" @click="sendMobileCode" id="sendMobileCode">获取</button>

 			//获取验证码按钮注册事件
            sendMobileCode() {
                //手机号码必填
                if (!this.user.phone) {
                    alert("手机号码必填!!");
                    return;
                }
                //开启禁用按钮
                this.disabled = true;
                //设置初始值 60s
                this.htmlValue = 60;
                //用一个变量接受vue的this,方便使用
                let that = this;
                //定时器
                let time = setInterval(function () {
                    that.htmlValue--;
                    if (!that.htmlValue) {
                        that.htmlValue = "获取";
                        //关闭禁用按钮
                        this.disabled = false;
                        //删除定时器;
                        clearInterval(time);
                    }
                }, 1000);
                //准备电话号码
                let param = {"phone": this.user.phone};
                //发送验证码请求
                this.$http.post("http://localhost/user/RequestCode",param).then((res=>{
                    console.debug(res);
                }));

2.验证手机号码是否在数据库已经存在
前台代码: 在手机号码标签添加了一个 失去焦点事件@blur

 <div class="user-phone">
 <label for="phone"><i class="am-icon-mobile-phone am-icon-md"></i></label>
 <input type="tel" name="" @blur="checkPhone" v-model="user.phone" id="phone" placeholder="请输入手机号"> </div>

			checkPhone() {
                //准备参数
                let param = {"phone": this.user.phone};
                this.$http.post("http://localhost/user/checkPhone", param).then((res => {
                    let {success, msg} = res.data;
                    //后台响应success为false,就证明手机号已被注册
                    if (!success) {
                        //启动禁用按钮
                        this.disabled = true;
                        //给错误信息赋值
                        this.errorMsg = msg;
                    } else {
                        //关闭禁用按钮
                        this.disabled = false;
                        this.errorMsg = "";
                    }
                }));
            },

后台代码(略):简单 (就是一个普通的controller–service–mapper–mapper.xml的查询)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值