Vue中添加手机验证码组件功能操作方法(2)
通过控制 父div 的显示状态来控制子组件的显示状态,
methods:{ // 手机号验证 verifyPhone(){ this.verifyShow=!this.verifyShow; }, },
在验证组件中的逻辑控制如下:
<script> // 引入弹窗组件 import { Toast } from 'mint-ui'; export default { data(){ return { phoneNum:"", //手机号 verifyNum:"", //验证码 btnContent:"获取验证码", //获取验证码按钮内文字 time:0, //发送验证码间隔时间 disabled:false //按钮状态 } }, created(){ }, methods:{ // 获取验证码 sendSmsCode(){ var reg=11&& /^((13|14|15|17|18)[0-9]{1}\d{8})$/;//手机号正则验证 var phoneNum = this.phoneNum; if(!phoneNum){//未输入手机号 Toast("请输入手机号码"); return; } if(!reg.test(phoneNum)){//手机号不合法 Toast("您输入的手机号码不合法,请重新输入"); } this.time = 60; this.timer(); // 获取验证码请求 var url = 'http://bosstan.asuscomm.com/api/common/sendSmsCode'; this.$http.post(url,{username:phoneNum},{emulateJSON:true}).then((response)=>{ console.log(response.body); }); }, timer(){ if(this.time>0){ this.time--; this.btnContent = this.time+"s后重新获取"; this.disabled = true; var timer = setTimeout(this.timer,1000); }else if(this.time == 0){ this.btnContent = "获取验证码"; clearTimeout(timer); this.disabled = false; } }, // 验证验证码 verificationCode(){ var phoneNum = this.phoneNum;//手机号 var verifyNum = this.verifyNum;//验证码 var url = 'http://bosstan.asuscomm.com/api/common/verificationCode'; this.$http.post(url,{ username:phoneNum, code:verifyNum },{ emulateJSON:true }).then((response)=>{ console.log(response.body); }); }, fillContent(){ // console.log("fillContent"); } } } </script>
其中,获取验证码和验证短信验证码的逻辑还没有写入。
PS:下面给大家补充一段vue短信验证码组件实例代码:
Vue.component('timerBtn',{ template: '<button v-on:click="run" :disabled="disabled || time > 0">{{ text }}</button>', props: { second: { type: Number, default: 60 }, disabled: { type: Boolean, default: false } }, data:function () { return { time: 0 } }, methods: { run: function () { this.$emit('run'); }, start: function(){ this.time = this.second; this.timer(); }, stop: function(){ this.time = 0; this.disabled = false; }, setDisabled: function(val){ this.disabled = val; }, timer: function () { if (this.time > 0) { this.time--; setTimeout(this.timer, 1000); }else{ this.disabled = false; } } }, computed: { text: function () { return this.time > 0 ? this.time + 's 后重获取' : '获取验证码'; } } });
内容版权声明:除非注明,否则皆为本站原创文章。