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)文章
解決vue2.0 element-ui中el-upload的before-upload方法返回false時submit(
這篇文章主要介紹了vue2.0 element-ui中el-upload的before-upload方法返回false時submit()不生效的解決方法,這里需要主要項目中用的element-ui是V1.4.3,感興趣的朋友參考下吧2018-08-08
vue使用天地圖、openlayers實現(xiàn)多個底圖疊加顯示效果
這篇文章主要介紹了vue使用天地圖、openlayers實現(xiàn)多個底圖疊加顯示,根據(jù)返回的經(jīng)緯度列表通過天地圖、openlayers實現(xiàn)底圖添加,本文通過示例代碼給大家介紹的非常詳細,需要的朋友參考下吧2022-04-04
Vue動態(tài)加載圖片在跨域時無法顯示的問題及解決方法
這篇文章主要介紹了解決VUE動態(tài)加載圖片在跨域時無法顯示的問題,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
vue中使用jquery滑動到頁面底部的實現(xiàn)方式
這篇文章主要介紹了vue中使用jquery滑動到頁面底部的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12

