WeChat Mini Program WeChat Authorized Login

https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.pluginLogin.html

WeChat applet authorization login process:
WeChat applet login interface
1. Login
**wx.pluginLogin(Object args)** This interface can only be called in the applet plug-in, and the plug-in user logo credential (code) can be obtained by calling the interface. The plug-in can exchange this credential for the identifier openpid used to identify the user. In the case of different users, different host applets or different plug-ins, the IDs are different, that is, if and only when the same user uses the same plug-in in the same host applet, the openpid will be the same.
**wx.login(Object object) calls the interface to obtain the login credentials (code). Use the credentials to exchange for user login status information, including the user's unique identifier (openid) in the current Mini Program, the unique identifier under the WeChat Open Platform account (unionid, if the current Mini Program has been bound to the WeChat Open Platform account) and this login The session key (session_key), etc. The encryption and decryption communication of user data needs to rely on the session key to complete.
wx.checkSession(Object object) checks whether the login status has expired. The user login status obtained through the wx.login interface has certain timeliness. The longer the user does not use the Mini Program, the more likely the user login status will become invalid. Conversely, if the user has been using the applet, the user login status remains valid. The specific aging logic is maintained by WeChat and is transparent to developers. Developers only need to call the wx.checkSession interface to check whether the current user login status is valid. After the login status expires, the developer can call wx.login to obtain a new user login status. A successful call indicates that the current session_key has not expired, and a failed call indicates that the session_key has expired.
2. Account information
Object wx.getAccountInfoSync()

3. User information
wx.getUserProfile(Object object)**Get user information. It can only be called after the page generates a click event (for example, in the callback of bindtap on the button), and an authorization window will pop up for each request, and userInfo will be returned after the user agrees. This interface is used to replace wx.getUserInfo.
4. WeChat authorizes
wx.authorizeForMiniProgram(Object object)
**wx.authorize(Object object)** to initiate an authorization request to the user in advance. Immediately after the call, a window will pop up asking the user whether to authorize the applet to use a certain function or obtain some user data, but the corresponding interface will not be actually called. If the user has agreed to the authorization before, no pop-up window will appear, and success will be returned directly.
5. Set
**wx.openSetting(Object object)** to invoke the setting interface of the client applet, and return the operation result set by the user. Only the permissions that the applet has requested from the user will appear on the setting interface.
**wx.getSetting(Object object)** Get the current setting of the user. Only the permissions that the applet has requested from the user will appear in the return value.
6. Simple implementation
In the WeChat developer tools, add a button to the .wxml corresponding to the authorization page

<button bindtap="onWXClick">授权登录</button>

Add the onWXlick function in the corresponding .js file, and specify in the function to jump to the page after the authorization is successful.

onWXClick(event) {
    
    
        var that = this;
        console.log("微信授权登录被点击");
        wx.getUserProfile({
    
    
          desc: "完善用户资料",
          success: (res) => {
    
    
            console.log("授权成功");
            wx.switchTab({
    
    
              url: "/pages/index/index",
            });
          },
          fail: (res) => {
    
    
            console.log("授权失败");
          },
        });
      },

Logout is similar, just add a button, and then jump to the authorization page.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_42370166/article/details/125032349