一.前端发送验证码的准备工作
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的查询)