uniapp图形验证码
时间: 2023-09-06 17:08:27 浏览: 186
图形验证码
uniapp图形验证码是通过引入云对象"uni-captcha-co"并定义getVerifyImage()函数来获取的。在登录页面中,如果验证码输入错误,点击"登录"后会显示"验证码错误"。当应用场景发生改变时,需要重新获取验证码。可以通过修改代码来实现重新获取验证码的功能。代码示例如下:
```javascript
<script>
const uniCaptchaDemoCo = uniCloud.importObject("uni-captcha-demo"); //索引
let index = 0;
export default {
data() {
return {
formData: {
captcha: "",
scene: "login"
},
rules: {
//...
captcha: {
rules: [
{
required: true,
errorMessage: '请输入验证码'
},
{
validateFunction: (rule, value, data, callback) => {
return new Promise((resolve, reject) => {
uniCaptchaDemoCo.verify(this.formData).then(e => {
resolve();
}).catch(e => {
//修改场景值,重新获取验证码
this.formData['scene'] = 'login' + (index);
reject(new Error(e.errMsg));
});
})
}
}
]
},
},
//rules end
}
},
methods: {
//...
}
}
</script>
```
在以上代码中,通过uniCaptchaDemoCo对象的verify方法来验证验证码,如果验证失败,则通过修改场景值重新获取验证码。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [uniCloud实现图形验证码(uni-captcha)](https://blog.csdn.net/jiciqiang/article/details/129254510)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文