


Detailed explanation of how to implement the QR code login function on the Vue PC side
This article brings you relevant knowledge about Vue. It mainly introduces the principle of implementing code scanning on the PC side? How to generate a QR code image? How to use Vue to implement front-end QR code login? For those who are interested, let’s take a look below. I hope it will be helpful to everyone.
Requirement Description
Currently, most PC applications have supporting mobile APPs, such as WeChat, Taobao, etc. By using the scan on the mobile APP The scan function is used to scan the QR code image on the page to log in, making the user login operation more convenient, safe and fast.
Idea Analysis
Principle of PC scanning code?
The scan code login function involves the web page, the server and the mobile phone. The general steps of interaction between the three ends are as follows:
The web page displays the QR code and continuously Send a request to the server to inquire about the status of the QR code;
The mobile phone scans the QR code. After successfully reading the QR code, it jumps to the confirmation login page. If the user After confirming the login, the server will modify the QR code status and return the user login information;
The web page will jump to the post-login page after receiving the server-side QR code status change;
If the user does not operate for a certain period of time, the QR code on the web page will become invalid, and a new QR code needs to be refreshed and generated.
Front-end function implementation
How to generate a QR code image?
- The content of the QR code is a string, you can use uuid as the unique identifier of the QR code;
- Use the qrcode plug-in import QRCode from 'qrcode'; to change the uuid into two The QR code is displayed to the user
import {v4 as uuidv4} from "uuid" import QRCode from "qrcodejs2" let timeStamp = new Date().getTime() // 生成时间戳,用于后台校验有效期 let uuid = uuidv4() let content = `uid=${uid}&timeStamp=${timeStamp}` this.$nextTick(()=> { const qrcode = new QRCode(this.$refs.qrcode, { text: content, width: 180, height: 180, colorDark: "#333333", colorlight: "#ffffff", correctLevel: QRCode.correctLevel.H, render: "canvas" }) qrcode._el.title = ''
How to control the timeliness of the QR code?
Use the front-end timer setInterval, initialize the effective time effectiveTime, and refresh the QR code after the countdown expires
export default { name: "qrCode", data() { return { codeStatus: 1, // 1- 未扫码 2-扫码通过 3-过期 effectiveTime: 30, // 有效时间 qrCodeTimer: null // 有效时长计时器 uid: '', time: '' }; }, methods: { // 轮询获取二维码状态 getQcodeStatus() { if(!this.qsCodeTimer) { this.qrCodeTimer = setInterval(()=> { // 二维码过期 if(this.effectiveTime <=0) { this.codeStatus = 3 clearInterval(this.qsCodeTimer) this.qsCodeTimer = null return } this.effectiveTime-- }, 1000) } }, // 刷新二维码 refreshCode() { this.codeStatus = 1 this.effectiveTime = 30 this.qsCodeTimer = null this.generateORCode() } },
How does the front-end obtain the status of the server QR code?
The front end sends a QR code status query request to the server, usually using polling
- 定时轮询:间隔1s 或特定时段发送请求,通过调用setInterval(), clearInterval()来停止;
- 长轮询:前端判断接收到的返回结果,若二维码仍未被扫描,则会继续发送查询请求,直至状态发生变化(失效或扫码成功)
- Websocket:前端在生成二维码后,会与后端建立连接,一旦后端发现二维码状态变化,可直接通过建立的连接主动推送信息给前端。
使用长轮询实现:
// 获取后台状态 async checkQRcodeStatus() { const res = await checkQRcode({ uid: this.uid, time: this.time }) if(res && res.code == 200) { let codeStatus - res.codeStatus this.codeStatus = codeStatus let loginData = res.loginData switch(codeStatus) { case 3: console.log("二维码过期") clearInterval(this.qsCodeTimer) this.qsCodeTimer = null this.effectiveTime = 0 break; case 2: console.log("扫码通过") clearInterval(this.qsCodeTimer) this.qsCodeTimer = null this.$emit("login", loginData) break; case 1: console.log("未扫码") this.effectiveTime > 0 && this.checkQRcodeStatus() break; default: break; } } },
推荐学习:《vue.js视频教程》
The above is the detailed content of Detailed explanation of how to implement the QR code login function on the Vue PC side. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP and Vue: a perfect pairing of front-end development tools. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

With the development of Internet technology, front-end development has become increasingly important. Especially the popularity of mobile devices requires front-end development technology that is efficient, stable, safe and easy to maintain. As a rapidly developing programming language, Go language has been used by more and more developers. So, is it feasible to use Go language for front-end development? Next, this article will explain in detail how to use Go language for front-end development. Let’s first take a look at why Go language is used for front-end development. Many people think that Go language is a

As a C# developer, our development work usually includes front-end and back-end development. As technology develops and the complexity of projects increases, the collaborative development of front-end and back-end has become more and more important and complex. This article will share some front-end and back-end collaborative development techniques to help C# developers complete development work more efficiently. After determining the interface specifications, collaborative development of the front-end and back-end is inseparable from the interaction of API interfaces. To ensure the smooth progress of front-end and back-end collaborative development, the most important thing is to define good interface specifications. Interface specification involves the name of the interface

Django is a web application framework written in Python that emphasizes rapid development and clean methods. Although Django is a web framework, to answer the question whether Django is a front-end or a back-end, you need to have a deep understanding of the concepts of front-end and back-end. The front end refers to the interface that users directly interact with, and the back end refers to server-side programs. They interact with data through the HTTP protocol. When the front-end and back-end are separated, the front-end and back-end programs can be developed independently to implement business logic and interactive effects respectively, and data exchange.

As a fast and efficient programming language, Go language is widely popular in the field of back-end development. However, few people associate Go language with front-end development. In fact, using Go language for front-end development can not only improve efficiency, but also bring new horizons to developers. This article will explore the possibility of using the Go language for front-end development and provide specific code examples to help readers better understand this area. In traditional front-end development, JavaScript, HTML, and CSS are often used to build user interfaces

Golang can be used as a front-end. Golang is a very versatile programming language that can be used to develop different types of applications, including front-end applications. By using Golang to write the front-end, you can get rid of a series of problems caused by languages such as JavaScript. For example, problems such as poor type safety, low performance, and difficult to maintain code.

Methods for implementing instant messaging include WebSocket, Long Polling, Server-Sent Events, WebRTC, etc. Detailed introduction: 1. WebSocket, which can establish a persistent connection between the client and the server to achieve real-time two-way communication. The front end can use the WebSocket API to create a WebSocket connection and achieve instant messaging by sending and receiving messages; 2. Long Polling, a technology that simulates real-time communication, etc.
