溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue如何使用Google Recaptcha驗(yàn)證

發(fā)布時(shí)間:2021-08-23 09:15:37 來源:億速云 閱讀:327 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹vue如何使用Google Recaptcha驗(yàn)證,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

我們最近的項(xiàng)目中需要使用谷歌機(jī)器人驗(yàn)證,這個(gè)最主要的就是要有vpn,還需要有公司申請的google賬號(hào)(自己申請的沒用)用于商用的,利用這個(gè)賬號(hào)去申請秘鑰,然后有了秘鑰之后就可以了。
部分代碼如下:

1.首先正是我封裝的google機(jī)器人驗(yàn)證組件:

<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的驗(yàn)證情況,
        * 成功返回字符串
        * 失敗不返回字符串
        * 所以根據(jù)字符串判斷驗(yàn)證情況
        */
        callback: res => {// true  - 驗(yàn)證成功 // false - 驗(yàn)證失敗 
    
            res.length > 0 ? this.$emit("getValidateCode", false) :  his.$emit("getValidateCode", true)
        }
      });
      // grecaptcha.render('robot', {
      //     sitekey: this.sitekey,
      //     /**
      //     * res返回的是goole的驗(yàn)證情況,
      //     * 成功返回字符串
      //     * 失敗不返回字符串
      //     * 所以根據(jù)字符串判斷驗(yàn)證情況
      //     */
      //     theme: 'light', //主題顏色,有l(wèi)ight與dark兩個(gè)值可選
      //     size: 'normal',//尺寸規(guī)則,有normal與compact兩個(gè)值可選
      //     callback: res => {
      //         res.length > 0 ?  this.$emit("getValidateCode", true) :  this.$emit("getValidateCode", false)
      //         // true  - 驗(yàn)證成功 // false - 驗(yàn)證失敗 
      //     }
      // }); 
     
    }
  },
};
</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 href="http://www.jp-night.com/terms.html" rel="external nofollow" >利用規(guī)約</a>· <a href="http://www.jp-night.com/privacy.html" rel="external nofollow" >プライバシ一ポリシ一</a>に同意の上 に同意の上でお進(jìn)みください </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('請進(jìn)行人機(jī)驗(yàn)證'));
      } 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.就完成啦,谷歌機(jī)器人就可以使用啦。

示意圖:

vue如何使用Google Recaptcha驗(yàn)證

以上是“vue如何使用Google Recaptcha驗(yàn)證”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI