TencentCaptcha web端接入


 

 1 import { Toast } from 'vant'
 2 
 3 let scriptId = 0
 4 let prefix = '__import__'
 5 const loadJs = async (url) => {
 6     let script = document.createElement('script')
 7     let elId = prefix + scriptId
 8 
 9     return new Promise((resolve, reject) => {
10         script.onload = (res) => {
11             scriptId++
12             resolve(0)
13             Toast.clear()
14         }
15         if (!document.querySelector(`#${elId}`)) {
16             Toast.loading({
17                 message: '加载中...',
18                 forbidClick: true,
19             })
20             script.id = elId
21             script.src = url
22             script.async = true
23             document.head.appendChild(script)
24         } else {
25             resolve(0)
26         }
27     })
28 }
29 // appid 
30 // App Secret Key  
31 const TencentVerify = {
32     async open(appid = '', callback, opts = { needFeedBack: false }) {
33         if (!window.TencentCaptcha) {
34             await loadJs('https://ssl.captcha.qq.com/TCaptcha.js')
35         }
36         return new Promise((resolve, reject) => {
37             let captcha = new TencentCaptcha(appid, (res) => {
38                 if (res.ret === 0) {
39                     let result = { success: true, msg: '验证成功', params: res }
40                     callback && callback(result)
41                     resolve(result)
42                 }
43                 reject({ success: false, msg: '验证失败' })
44                 captcha.destroy()
45             }, opts)
46             captcha.show()
47         })
48     }
49 }
50 
51 export default TencentVerify

使用

TencentVerify.open()
        .then((res) => {
            console.log(res)
        }).catch((err) => {
            console.log(err)
        })

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM