vue使用Google Recaptcha驗證的實現(xiàn)示例
我們最近的項目中需要使用谷歌機器人驗證,這個最主要的就是要有vpn,還需要有公司申請的google賬號(自己申請的沒用)用于商用的,利用這個賬號去申請秘鑰,然后有了秘鑰之后就可以了。
部分代碼如下:
1.首先正是我封裝的google機器人驗證組件:
<template> <div ref="grecaptcha"> </div> <!-- <div id="robot"></div> --> </template> <script src="http://www.recaptcha.net/recaptcha/api.js?οnlοad=ReCaptchaLoaded&render=explicit&hl=en" async defer></script> <script> export default { props: ["sitekey"], // 所要傳的秘鑰 mounted() { window.ReCaptchaLoaded = this.loaded; var script = document.createElement("script"); script.src = "https://recaptcha.net/recaptcha/api.js?οnlοad=ReCaptchaLoaded&render=explicit"; document.head.appendChild(script); }, methods: { loaded() { window.grecaptcha.render(this.$refs.grecaptcha, { sitekey: this.sitekey, /** * res返回的是goole的驗證情況, * 成功返回字符串 * 失敗不返回字符串 * 所以根據(jù)字符串判斷驗證情況 */ callback: res => {// true - 驗證成功 // false - 驗證失敗 res.length > 0 ? this.$emit("getValidateCode", false) : his.$emit("getValidateCode", true) } }); // grecaptcha.render('robot', { // sitekey: this.sitekey, // /** // * res返回的是goole的驗證情況, // * 成功返回字符串 // * 失敗不返回字符串 // * 所以根據(jù)字符串判斷驗證情況 // */ // theme: 'light', //主題顏色,有l(wèi)ight與dark兩個值可選 // size: 'normal',//尺寸規(guī)則,有normal與compact兩個值可選 // callback: res => { // res.length > 0 ? this.$emit("getValidateCode", true) : this.$emit("getValidateCode", false) // // true - 驗證成功 // false - 驗證失敗 // } // }); } }, }; </script> <style> </style>
2.在需要的地方使用:
<template> <div> <div class="home-content"> <div class="home-content-img"> <!-- <div class="home-content-imgtop"> --> <img src="../../assets/image/202010_JP NIGHT 店舗掲載営業(yè)用資料.002.png" alt=""> <img src="../../assets/image/202010_JP NIGHT 店舗掲載営業(yè)用資料.003.png" alt=""> <!-- </div> --> <!-- <div class="home-content-imgbottom"> --> <img src="../../assets/image/202010_JP NIGHT 店舗掲載営業(yè)用資料.004.png" alt=""> <img src="../../assets/image/202010_JP NIGHT 店舗掲載営業(yè)用資料.005.png" alt=""> <!-- </div> --> </div> <div class="home-content-bottom"> <p> <a rel="external nofollow" >利用規(guī)約</a>· <a rel="external nofollow" >プライバシ一ポリシ一</a>に同意の上 に同意の上でお進みください </p> <div class="content-google"> <google-recaptcha ref="recaptcha" :sitekey="key" @getValidateCode='getValidateCode' v-model="validateCode"></google-recaptcha> </div> <div class="login"> <button :class="isNext ?'login-botton-background':'login-botton'" :disabled="isNext" @click="login()">店舗揭載を応募する</button> </div> </div> </div> </div> </template> <script> import GoogleRecaptcha from '../../common/module/GoogleRecaptcha' export default { data() { var checkCode = (rule, value, callback) => { if (value == false) { callback(new Error('請進行人機驗證')); } else { callback(); } }; return { key: '6Ld9xxgaAAAAAI4xn7seMCmqbxIpVsJ_dwrZu9z1', validateCode: false, isNext:false }; }, created(){ }, mounted(){ }, components:{ GoogleRecaptcha }, methods:{ login(){ sessionStorage.setItem('token',true) this.$router.push({ path: "/shops", query: { out: true } }) }, getValidateCode(value) { console.log(value); this.isNext = value }, } }; </script> <style lang="scss" scoped> @import "./css/pc.css"; @import "./css/ipad.css"; @import "./css/phone.css"; #rc-anchor-container { width: 335px; } </style>
3.就完成啦,谷歌機器人就可以使用啦。
示意圖:
到此這篇關(guān)于vue使用Google Recaptcha驗證的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)vue Google Recaptcha驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue響應(yīng)式原理Observer、Dep、Watcher理解
這篇文章主要介紹了Vue響應(yīng)式原理-理解Observer、Dep、Watcher,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06解決vue init webpack 下載依賴卡住不動的問題
這篇文章主要介紹了解決vue init webpack 下載依賴卡住不動的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11談?wù)剬ue響應(yīng)式數(shù)據(jù)更新的誤解
本篇文章主要介紹了談?wù)剬ue響應(yīng)式數(shù)據(jù)更新的誤解,深入了解了vue響應(yīng)式數(shù)據(jù),有興趣的可以了解一下2017-08-08