


Detailed explanation of practical cases of on-demand authorization login for Vue WeChat project
This time I will bring you VueWeChat projectA detailed explanation of the actual case of authorized login on demand. What are the precautions to implement on-demand authorized login of Vue WeChat project. The following is a practical case. , let’s take a look.
The project uses Vue as the development framework. There are two situations when users browse the page:
One requires users to log in before they can continue browsing;
The other is that users can browse at will without logging in.
Pages that do not require user login may contain operations that require user information. In this case, the user needs to log in before subsequent operations can be performed. Therefore, authorization login policies need to be distinguished.
Thinking
1. Generally speaking, the H5 page we develop for WeChat requires authentication when entering the page, requiring the user to You must log in to continue browsing. However, due to product requirements, for this project we need to divide the authentication strategies of different pages and design them according to general and special:
2. In general, users are required to authorize login as soon as they enter the page. According to the routine WeChat authorized login process. After logging in, the user continues browsing.
3. In special cases, configure a whitelist for pages that do not require user login. Just enter the route that exists in the whitelist and do not enter the function that detects user login status , render the page directly.
For operations that require user information when the user is not logged in, according to my current understanding, even if it is a silent authorization based on WeChat, the page must be refreshed, and it is impossible to achieve truly senseless authorization and continue the user operation. Therefore, I chose to give the user a more friendly prompt at the front-end level to let the user understand the authorization process. The disadvantage is that the previous operation only triggered the authorized login. After the authorized login, the user needs to perform the operation again.
// routerRule.js export default function routerRule (router, whiteList = []) { // other codes... router.beforeEach( (to, from, next ) => { // 因为授权登录涉及异步操作,因此使用promise,成功的回调中调用next函数 new Promise((resolve, rejects) => { if ( whiteListRouter.indexOf(to.path) !== -1 ) { resolve() return } // 常规页面授权登录过程 if (hasToken()) { // codes,获取用户信息并且跳转所需跳转的页面 } else { // 判断用户是否已经进行微信授权 if (hasAuthed()) { // 进行过微信授权之后,重定向回来的url中包含了微信的授权信息,可以将url上截取的参数发送到服务器,换取用户的token,随后进入上述有token时候的步骤 getWechatUserInfo().then(res => { resolve() }) } else { // 用户尚未进行微信授权,则调用微信授权的方法,进行授权登录。 getWechatAuth() } } }).then( res => { next() }) }) router.afterEach(( to, from ) => { wxShare({ title: to.meta.title, desc: to.meta.shareDesc, link: to.meta.shareLink, logo: to.meta.shareLogo}) }) }
This project is to bind the user's WeChat information with the user information of this site when the user first binds to WeChat. Therefore, after obtaining the user's WeChat authorization information, the user can be obtained token to obtain the user’s other user information on this site.
Processing operations that require permissions without logging in to the page
According to the above logic, after entering the whitelist, the entire function has been If you return it, you will not enter the following authentication process. However, if you perform operations that require permissions on such a page, you need to trigger the authorization login process, and after authorization, user information must be obtained together.
// checkLogin.js export function checkLogin({ redirectUrl, wxAuthLoading, wxAuthLoaded, callback } = {}) { if (getToken()) { // ... callback && callback() } else { // 提示用户正在授权中 wxAuthLoading && wxAuthLoading() getWechatAuth( redirectUrl || window.location.href ).then( res => { // 授权完毕,提示用户授权成功 wxAuthLoaded && wxAuthLoaded() }) } }
At the same time, we need to add some operations to the routing whitelist
// routerRule.js export default function routerRule (router, whiteList = []) { // other codes... router.beforeEach( (to, from, next ) => { // 因为授权登录涉及异步操作,因此使用promise,成功的回调中调用next函数 new Promise((resolve, rejects) => { if ( whiteListRouter.indexOf(to.path) !== -1 ) { // 如果已经进行微信授权但是没有token值的,就调用以下函数获取token值 if ( !hasToken() && hasAuthed() ) { getWechatUserInfo().then(res => { resolve() }) } resolve() return } // 常规页面授权登录过程 if (hasToken()) { // codes,获取用户信息并且跳转所需跳转的页面 } else { // 判断用户是否已经进行微信授权 if (hasAuthed()) { // 进行过微信授权之后,重定向回来的url中包含了微信的授权信息,可以将url上截取的参数发送到服务器,换取用户的token,随后进入上述有token时候的步骤 getWechatUserInfo().then(res => { resolve() }) } else { // 用户尚未进行微信授权,则调用微信授权的方法,进行授权登录。 getWechatAuth() } } }).then( res => { next() }) }) // other codes... }
Pits and imperfections
1. In this solution, after user authorization, user information must be obtained before routing jump, otherwise the WeChat authorization information on the URL will be lost and obtaining user information will fail.
2. The disadvantage of this solution is that the developer needs to add checkLogin to all operations that require permissions on the login-free page. Since this permission-requiring operation generally involves sending asynchronous requests, if you do not consider reducing unnecessary asynchronous requests, you can set an interceptor on the request method to determine the code returned by the backend. If the returned code is If the user has not logged in to the code, WeChat authorization will be performed. This approach is more convenient in the development process, but it will send some unnecessary requests to the backend when the user is not logged in, which is not good.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to package the vue project to the server
How to use webstorm to add *.vue files
The above is the detailed content of Detailed explanation of practical cases of on-demand authorization login for Vue WeChat project. For more information, please follow other related articles on the PHP Chinese website!

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

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

Dreamweaver Mac version
Visual web development tools
