Simple steps to implement H5 WeChat public account authorization

Simple steps to implement H5 WeChat public account authorization

Preface

Yesterday, there was a project that required the implementation of h5 WeChat authorization. So it took two hours to complete this function.​

Preparation before starting work

Process description [process communicated in advance]

  1. WeChat authorization is time-limited. Once authorized within a period of time, there is no need to click to confirm again. If you uninstall WeChat and reinstall it, you still need to reconfirm the authorization.
  2. Whether it is the first time to confirm the authorization or after authorization, you can use the WeChat server to authorize the callback to our backend interface callback.
  3. After WeChat authorization callback, the code & state parameters will be returned. The backend can obtain accessToken through code, and then obtain user information through accessToken.
  4. After the backend receives the server callback, there are two main fields when calling back to the front end: isAuth represents whether it is authorized, and isBindFlag represents whether it has been registered and logged in in our system. Here, because our current system requires user authorization registration, these two fields exist.

Domain name, port

  • Prepared domain name - Domain name registered with the Ministry of Public Security
  • The port number is 80.

The domain name and port number are required because the domain name and port 80 are required for WeChat public account configuration and WeChat server callback.

Here, the same domain name and port are adapted to the front-end and back-end IP addresses, and are processed through the nginx unified proxy.

Ready to work

  • Domain name: http.xxx.cn
  • Front-end resource deployment: http.xxx.cn
  • Backend callback interface: http.xxx.cn/api/auth

Configure WeChat public account

Domain name configuration

Upload the verification file to the server root path, otherwise the domain name configuration cannot be saved.

Whitelist configuration

Writing code

import React, { useEffect } from "react";
import { View } from "@tarojs/components";

export default () => {
  useEffect(() => {
    // The path format of the backend callback: http://xxx.cn/#/pages/webAuthorization?bindFlag=0&openid=xxxxxxxxxxx&unionid=null&isAuth=true
    
    var isBindFlag = false, isAuth = false, opendId = '', paramsArray = [];


    /*
     * Omitted code: address determination, parameter processing and assignment to isAuth, isBindFlag, openId
     */ 

    if (!isAuth) { // Unauthorized window.location.href=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${'xxxxxxx'}&redirect_uri=http://xxxxx/api/auth?response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect`;
    } else if (!isBindFlag) { // Not registered window.location.href = '#/pages/login'
    } else { // Login window.location.href = '#/pages/index'
    }
  }, []);

  return (
    <View>
    </View>
  );
};

Summarize

This is the end of this article about H5 WeChat public account authorization. For more relevant WeChat public account authorization 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:
  • WeChat Official Account-Steps for Obtaining User Information (Web Authorization Obtaining)
  • WeChat public account realizes scanning code to obtain WeChat user information (web page authorization)
  • WeChat public account web page authorization login and code been used solution detailed explanation
  • A Brief Analysis of WeChat Official Account OAuth2.0 Web Authorization Issues

<<:  Summary of the data storage structure of the nginx http module

>>:  MySQL trigger definition and usage simple example

Recommend

Solution to the problem of failure to insert emoji expressions into MySQL

Preface I always thought that UTF-8 was a univers...

How to automatically back up the mysql database regularly

We all know that data is priceless. If we don’t b...

Detailed explanation of MySQL InnoDB index extension

Index extension: InnoDB automatically extends eac...

Detailed explanation of asynchronous iterators in nodejs

Table of contents Preface What are asynchronous i...

A brief discussion on the solution of Tomcat garbled code and port occupation

Tomcat server is a free and open source Web appli...

Summary of Linux file directory management commands

touch Command It has two functions: one is to upd...

Example code for implementing dynamic skinning with vue+element

Sometimes the theme of a project cannot satisfy e...

CSS3 flexible box flex to achieve three-column layout

As the title says: The height is known, the width...

Eight common SQL usage examples in MySQL

Preface MySQL continued to maintain its strong gr...

Common browser compatibility issues (summary)

Browser compatibility is nothing more than style ...

A brief discussion on common operations of MySQL in cmd and python

Environment configuration 1: Install MySQL and ad...

How to use and limit props in react

The props of the component (props is an object) F...

Basic usage of wget command under Linux

Table of contents Preface 1. Download a single fi...

Detailed steps for configuring Tomcat server in IDEA 2020

The steps for configuring Tomcat in IDEA 2020 are...