<script> export default { data() { return { //验证码地址 codeUrl: '', form: { username: '', password: '', verifyCode: "", //验证码 verifyKey: "", } } }, // onShow: function() { // this.getCodeUrl() // }, created() { this.getCodeUrl() }, methods: { // 获取验证码 getCodeUrl() { uni.request({ url: this.$globalVar + '/captcha/get', success: res => { if (res.data.code === 0) { this.codeUrl = res.data.data.base64stringC; this.form.verifyKey = res.data.data.idKeyC; } }, fail: err => { console.log(err) } }) }, //登陆方法 login() { uni.showLoading({ title: '登录中...', mask: true }) uni.request({ url: this.$globalVar + '/system/login', method: 'POST', data: this.form, success(res) { if (res.data.code === 0) { uni.showToast({ title: "登录成功", duration: 2000, }); uni.hideLoading() uni.setStorageSync('token', res.data.data.token); uni.reLaunch({ url: '/pages/index/index' }) } else { uni.hideLoading() if (res.data.msg === '验证码错误') { setTimeout(() => { this.getCodeUrl() }, 2000) } else { uni.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }); } } }, fail(res) { uni.hideLoading() uni.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }); } }) }, // 去注册 gotoRegister() { uni.navigateTo({ url: '/pages/register/register' }) } } } </script> 当登录失败报错验证码错误时 刷新验证码 这是我代码 帮我改一下
时间: 2024-04-28 16:22:29 浏览: 202
vue+element-ui集成随机验证码+用户名+密码的form表单验证功能
5星 · 资源好评率100%
你可以将以下代码替换到你的代码中,这样当登录失败且错误为验证码错误时,就会自动刷新验证码:
```
// 登录方法
login() {
uni.showLoading({
title: '登录中...',
mask: true
})
uni.request({
url: this.$globalVar + '/system/login',
method: 'POST',
data: this.form,
success(res) {
if (res.data.code === 0) {
uni.showToast({
title: "登录成功",
duration: 2000,
});
uni.hideLoading()
uni.setStorageSync('token', res.data.data.token);
uni.reLaunch({
url: '/pages/index/index'
})
} else {
uni.hideLoading()
if (res.data.msg === '验证码错误') {
setTimeout(() => {
this.getCodeUrl()
}, 2000)
} else {
uni.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
});
}
}
},
fail(res) {
uni.hideLoading()
uni.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
});
}
})
},
```
阅读全文