Mini Program Study Notes --- Obtaining User Authorization

Obtaining user authorization is a very important step in applet development. By obtaining user authorization, we can obtain sensitive information such as the user's basic information and geographical location, so as to provide users with more personalized services. In this article, we will explore how to obtain user authorization in Mini Programs.

1. Authorization method

There are two ways for the applet to obtain user authorization: one is triggered by a button, and the other is automatically triggered when the applet starts.

The authorization method triggered by a button is more common. We can add a button on the applet page. When the user clicks the button, an authorization window will pop up to request the user's authorization. The code example is as follows:

wx.getSetting({
  success(res) {
    if (!res.authSetting['scope.userInfo']) {
      wx.authorize({
        scope: 'scope.userInfo',
        success() {
          // 用户已经同意授权
          wx.getUserInfo({
            success(res) {
              console.log(res.userInfo)
            }
          })
        }
      })
    }
  }
})

The authorization method that is automatically triggered when the applet is started is also relatively common. We can call the wx.getSetting() method in the app.js file of the applet to determine whether the user has been authorized. If not, call the wx.authorize() method to request authorization. The code example is as follows:

App({
  onLaunch: function () {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              // 用户已经同意授权
            }
          })
        }
      }
    })
  }
})

2. Authorization scope

When the applet obtains user authorization, it needs to specify the authorization scope. Different authorization scopes correspond to different sensitive information. Currently, the authorization scopes supported by Mini Programs are as follows:

- scope.userInfo: user information, including nickname, avatar, etc.;
- scope.userLocation: geographic location;
- scope.address: mailing address;
- scope.invoiceTitle: invoice title;
- scope.werun: WeChat exercise steps;
- scope .record: recording function;
- scope.writePhotosAlbum: save to album;
- scope.camera: camera.

3. User authorization process

The procedure for the Mini Program to obtain user authorization is as follows:

- Call the wx.getSetting() method to obtain the user's current authorization status;
- If the user is not authorized, call the wx.authorize() method to request authorization;
- After the user confirms the authorization, you can call the corresponding API interface to obtain the user's sensitive information.

When obtaining user authorization, we also need to pay attention to some details. For example, if the user has ever refused authorization, we need to remind the user to turn on the authorization switch; if the user selects the cancel button in the authorization window, we also need to deal with it accordingly.

Summarize

Through the introduction of this article, we can know that obtaining user authorization in the applet is a very important step. We can obtain user authorization through button triggering or automatic triggering. At the same time, we need to pay attention to some details in the authorization scope and user authorization process. In actual development, we should carefully handle user authorization issues to ensure user information security and a good user experience.

Guess you like

Origin blog.csdn.net/m0_72196169/article/details/130660172