Vue implementation example using Google Recaptcha verification

Vue implementation example using Google Recaptcha verification

In our recent project, we need to use Google robot verification. The most important thing for this is to have a VPN, and we also need a Google account applied for by the company (it is useless to apply for it yourself) for commercial use. Use this account to apply for a secret key, and then once you have the secret key, you can do it.
Some of the code is as follows:

1. First of all, it is the Google robot verification component that I encapsulated:

<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"], // The secret key to be transferred 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 returns the verification status of Google.
        * Return string if successful * No string is returned if failed * So the verification status is determined based on the string */
        callback: res => {// true - validation successful// false - validation failedres.length > 0 ? this.$emit("getValidateCode", false) : his.$emit("getValidateCode", true)
        }
      });
      // grecaptcha.render('robot', {
      // sitekey: this.sitekey,
      // /**
      // * res returns the verification status of Google.
      // * Return string if successful// * Do not return string if failed// * So judge the verification status based on the string// */
      // theme: 'light', // theme color, light and dark are available // size: 'normal', // size rule, normal and compact are available // callback: res => {
      // res.length > 0 ? this.$emit("getValidateCode", true) : this.$emit("getValidateCode", false)
      // // true - authentication succeeded // false - authentication failed // }
      // }); 
     
    }
  },
};
</script>
<style>

</style>

2. Use where needed:

<template>
  <div>
    <div class="home-content">
      <div class="home-content-img">
        <!-- <div class="home-content-imgtop"> -->
          <img src="../../assets/image/202010_JP NIGHT Store Promotional Materials.002.png" alt="">
          <img src="../../assets/image/202010_JP NIGHT Store Promotional Materials.003.png" alt="">
        <!-- </div> -->
        <!-- <div class="home-content-imgbottom"> -->
          <img src="../../assets/image/202010_JP NIGHT Store Promotional Materials.004.png" alt="">
          <img src="../../assets/image/202010_JP NIGHT Store Promotional Materials.005.png" alt="">
        <!-- </div> -->
      </div>
      <div class="home-content-bottom">
        <p> <a href="http://www.jp-night.com/terms.html" rel="external nofollow" >Terms of Use</a> and <a href="http://www.jp-night.com/privacy.html" rel="external nofollow" >Privacy Policy</a> Please agree to this.</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()">Shop announcement</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('Please perform human-machine verification'));
      } else {
        callback();
      }
    };
    return {
      key: '6Ld9xxgaAAAAAI4xn7seMCmqbxIpVsJ_dwrZu9z1',
      validateCode: false,
      isNext:false
    };
  },
  created(){
    
  },
  mounted(){
    
  },
  components:{
    Google Recaptcha
  },
  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. That’s it, the Google robot is ready to use.

Schematic diagram:

insert image description here

This is the end of this article about the implementation example of vue using Google Recaptcha verification. For more relevant vue Google Recaptcha verification content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Do you really know how to do Vue form validation? vue form validation (form) validate
  • Detailed explanation of how to use Vue Validator, a Vue form validation plugin
  • Problems encountered in Form validation of Vue ElementUI
  • VUE implements image verification code function
  • vue implements the registration function of sending SMS verification code through mobile phone
  • Vue generates a random verification code sample code
  • Element combined with Vue to solve the problem of error prompt when there is value in form validation
  • How to add mobile verification code component function in Vue

<<:  Summary of basic knowledge and operations of MySQL database

>>:  Use nginx + secondary domain name + https support

Recommend

Teach you a trick to achieve text comparison in Linux

Preface In the process of writing code, we will i...

Problems and solutions of using jsx syntax in React-vscode

Problem Description After installing the plugin E...

How to install git on linux

1. Introduction Git is a free, open source distri...

Detailed explanation of MySql 5.7.17 free installation configuration tutorial

1. Download the mysql-5.7.17-winx64.zip installat...

How to use html table (to show the visual effect of web page)

We know that when using HTML on NetEase Blog, we ...

Some CSS questions you may be asked during an interview

This article is just to commemorate those CSS que...

Docker deploys nginx and mounts folders and file operations

During this period of time, I was studying docker...

Implementation of iview permission management

Table of contents iview-admin2.0 built-in permiss...

CSS and CSS3 flexible box model to achieve element width (height) adaptation

1. CSS realizes fixed width on the left and adapt...

How to quickly use mysqlreplicate to build MySQL master-slave

Introduction The mysql-utilities toolset is a col...

Detailed explanation of CSS elastic box flex-grow, flex-shrink, flex-basis

The functions of the three attributes flex-grow, ...

Working principle and example analysis of Linux NFS mechanism

What is NFS? network file system A method or mech...

Solution to 404 Problem of Tomcat Installation in Docker

Find the containerID of tomcat and enter the toma...