Vue realizes WeChat scan code login method

 

1. Preparatory work: 1. Wechat official account, scan code to log in 2. Domain name, that is, domain name resolution (public_domain) 3. Wechat login verification 4. Configure the code of WeChat scan code login page, with the above preparations, the following is It's time to start coding. 2. Development environment: 1. Vue project development environment 2. Vue component development environment: Vue project development tools 3. WeChat login interface: WeChat scan code login interface, WeChat QR code login interface (this needs to be defined by yourself) 3. Front-end implementation: 1. The input box of the scanning interface: implement the input box in vue, which needs to be defined by yourself, mainly through attributes. We need to add an attribute to the input box called v-input:`='' . This attribute allows us to put the user's information into an input box. Then use this attribute on our front-end page to realize user input information. 2. WeChat code scanning interface: use v-input on the front-end page to obtain user information, and here use the requests. loaderXML () method to obtain. It should be noted here that when we obtain user information, we cannot obtain it directly from the browser, because this will cause the browser to cache it in the database. We need to write the user information into a variable first, and then put it in requests .loaderXML() method. In this way, we can get the user's information from the database.

  • 1. WeChat scan code login verification: Create an interface in the WeChat official account, then use the devops component to configure the data of the interface, and then call the submit () method to obtain user information

    1. The WeChat scan code login interface is implemented through the tomcat module of the vue project. We first need to create a tomcat module, then store the data we just configured in the tomcat module, and then pass it to the submit () method of vue Get user information. We only need to write the user's information into vue. 2. WeChat QR code login interface: This is realized through the QR code in the WeChat official account. After scanning the code to log in, we need to send the user's information to the WeChat official account, and then the WeChat official account sends The WeChat background sends the corresponding user information. It should be noted here that when we send the QR code, we cannot directly obtain the user's information from the browser. We need to write the user's information into a variable before sending it to the WeChat official account.

    • 1. Create a tomcat module in the vue project, then write the user information to tomcat, and then pass in the submit () method to obtain the user information.

      After we write the user information into the tomcat module, we can send this user information to the WeChat official account. We only need to use the devops component to configure the data path. 3. WeChat scan code login interface: This is realized through the devops component. We only need to create an interface in the WeChat official account, then use the devops component to configure the data of the interface, and then call the submit () method to obtain user information. Then call the submit () method to get user information.

    • 2. Create a devops component in the Vue project, then pass the code of the WeChat scan code login interface to the devops component, and finally call the submit() method in the devops component to obtain user information.

      We write the user's information into variables in the browser, and then use the vue.ini file to pass it to the WeChat official account.

  • 2. WeChat QR code login verification: Configure this interface on the scan code login page, and then use the requests.loaderXML () method to obtain user information

    It should be noted here that if we simply scan the code to log in, we do not need to configure the requests. loaderXML () method, because we do not need to obtain user information from the database, because when we request the user, we will first send the user The information is stored in the database before being returned to the server. 1. Background implementation: mainly to do some operations on the front-end page, such as modifying the content of the WeChat official account, sending to Moments, etc. 2. Obtain user information in the background: When we configure this interface in the scan code login interface, we need to define this interface ourselves. When obtaining user information in the background, we need to use the requests. loaderXML () method. 5. The final output result: (we first put the user information into the variable and then get it). 6. Test: 1. Test effect: In this step, we need to call two interfaces at the same time for testing, one is the scan code login interface, and the other is the QR code login interface. In both interfaces, we need to obtain user information before sending it to the server. Among them, the scan code login interface and the QR code login interface are two independent interfaces, so we only need to use one. Now let's test whether these two interfaces can work normally. 2. Test results: 7. Summary: 1. It is very simple to implement the WeChat scan code login interface in Vue, as long as you configure this interface on the front-end page. But it should be noted that when we configure this interface, we must first obtain user information before proceeding to the next step. Because there is a lot of data on our background page that cannot be directly obtained from the database, we need to write the data into a variable first, and then use the requests. loaderXML () method to obtain user information. However, both scan code login and QR code login need to obtain user information before proceeding to the next step.

  • 3. Wechat scan code login result page: display the configured code on the WeChat scan code login page, realizing user scan code login

    1. Login interface 3. WeChat scan code login result page WeChat official account is currently the most widely used third-party development platform, so if we want to use it, we need to register it, and then we develop, that is, we need to obtain Domain name, configuration WeChat scan code login interface, etc. When wechat scan code login, we need to use scan code login interface, user information input box, etc. Finally, the user scan code login result is displayed on the front-end page. Six. My gains: 1. Learned some basic knowledge of Vue component development. 2. Learned how to configure the WeChat scan code login interface. 3. Learned how to use various attributes in vue. I have used all the knowledge I have learned in the past two days. By configuring the WeChat scan code login interface and the user information input box, I have realized the display of the WeChat scan code login result page, which is considered to be worthy of what I have learned.

The following is a simple Vue.js component for WeChat scan code login:
```
<template>
<div>
<div v-if="!isLogged">
<div ref="qrcode"></div>
</div>
<div v-else>
<p>Logged in</p>
</div>
</div>
</template>
<script>
import QRCode from 'qrcode'
export default { data() { return { isLogged : false,loginUrl: '',timer: null}},mounted() { this.getQrcode()},methods: { getQrcode() { // Send a request to get the URL of the login QR codeaxios.get('/api /getQrcode'). this.loginUrl = res.data.url// generate QR code
















QRCode.toCanvas(this.$refs.qrcode, this.loginUrl, { width: 200, height: 200 }) // Polling to check login status this.timer = setInterval(() => { this.checkLoginStatus() }, 2000) }) }, checkLoginStatus() { // Send request to check login status axios.get('/api/checkLoginStatus').then(res => { if (res.data.isLogged) { this.isLogged = true clearInterval (this.timer) } }) } } } </script> ``` This component will send a request in the `mounted` hook function to get the URL of the login QR code, and use the `qrcode` library to generate the QR code. It then sends a request to check the login status every 2 seconds until the user scans the QR code and completes the login. Once the user is logged in, the component displays the text "Logged in".






















Guess you like

Origin blog.csdn.net/qq_42751978/article/details/131030289