三 用Abp实现短信验证码免密登录:Vue网页端开发

文章目录
前端代码的框架采用vue.js +这套较为简单的方式实现 , 以及语法更方便阅读 。
首先来编写发送验证码函数 ,  登录 , 绑定 , 解绑的业务都需要发送验证码功能 , 通过 来区别当前验证码种类 。也就是在服务端的目的 。
可以为LOGIN , 或
async sendVerificationCode(phoneNumber, type) {this.currentVerifyingType = type;this.smsSendCd = 60;this.timer = setInterval(() => {this.smsSendCd--;if (this.smsSendCd <= 0) {clearInterval(this.timer);}}, 1000);await request(`${this.host}${this.prefix}/Captcha/Send`, "post", {userId: this.userInfo == null ? null : this.userInfo.id,phoneNumber: phoneNumber,type: type,}).catch((re) => {var res = re.response.data;this.errorMessage(res.error.message);}).then((re) => {var res = re.data.result;this.successMessage("发送验证码成功");});}
注意几个关键的全局变量
userInfo: null,//用户对象currentVerifyingType: null,//当前发送验证码的用途smsSendCd: 0,//发送验证码的冷却时间 , 默认60sloginForm: {//登录对象username: "",password: "",},token: null,//登录凭证TokenverifyNumber: null,//填写的验证码
登录功能
创建手机号输入控件
+86
创建验证码控件 , 并添加一个按钮用于发送验证码 , 点击后触发
0"@click="sendVerificationCode(loginForm.username, 'LOGIN')">{{smsSendCd == 0 ? "发送验证码" : smsSendCd + "后重试"}}
登录函数 , 将验证电话号码(即用户名)和验证码

三  用Abp实现短信验证码免密登录:Vue网页端开发

文章插图
async handleLogin() {this.loading = true;let phoneNumber = this.loginForm.username;let password = this.loginForm.password;phoneNumber = phoneNumber.trim();await request(`${this.host}api/TokenAuth/Authenticate`, "post", {phoneNumber,password,});}
登录完成后 , 将Token存入
.then(async (res) => {var data = http://www.kingceram.com/post/res.data.result;setToken(data.accessToken);
绑定/解绑功能
创建新手机号输入框 , 若没有绑定手机 , 附带绑定按钮 , 按下后将发送验证码;若已绑定手机 , 需要先解绑 , 才能绑定新号码 , 附带解绑按钮 , 按下后将发送验证码
0"@click="sendVerificationCode(userInfo.phoneNumber, 'BIND_PHONENUMBER')">{{ smsSendCd == 0 ? "验证手机号" : smsSendCd + "后重试" }} 0"@click="sendVerificationCode(userInfo.phoneNumber, 'UNBIND_PHONENUMBER')">{{ smsSendCd == 0 ? "解绑手机号" : smsSendCd + "后重试" }}
创建校验短信验证码控件
完成验证
创建校验短信验证码函数 , 
async verify() {var action = null;if (this.currentVerifyingType == "BIND_PHONENUMBER") {action = "Bind";} else if (this.currentVerifyingType == "UNBIND_PHONENUMBER") {action = "Unbind";} else {action = "Verify";}await request(`${this.host}${this.prefix}/Captcha/${action}`, "post", {token: this.verifyNumber,}).catch((re) => {var res = re.response.data;this.errorMessage(res.error.message);}).then((re) => {var res = re.data;if (res.success) {this.successMessage("绑定成功");window.location.reload()}});}