Small Authorization

Small Authorization

Authorize

Part of the interface requires authorized users agree to call. We put these into a plurality of interfaces by use of scopea user selection scopefor authorization, if authorized to one scopeafter all of the corresponding interfaces can be used directly.

Such interfaces are called when:

  • If the user has not accepted or refused this permission, it will pop ask the user before calling the interface after the user clicks consent;
  • If the user is authorized to directly call interface;
  • If the user has refused authorization, it will not appear pop, but directly into the interface to fail callback. Please Developers compatible user denying authorization scene.

Obtaining user authorization settings

Developers can use wx.getSetting obtain the user's current authorization status.

Open the settings interface

Users can set up a small program interface ( "top right corner" - "About" - "top right corner" - "Setup") authorized state control of the applets.

Developers can call wx.openSetting open the settings interface guides the user to open authorization.

Initiate advance authorization request

Developers can use wx.authorize before calling needs authorization API, launched in advance authorization request to the user.

scope list

scope The corresponding interface description
scope.userInfo wx.getUserInfo User Info
scope.userLocation wx.getLocation, wx.chooseLocation Location
scope.userLocationBackground wx.startLocationUpdateBackground Background positioning
scope.address wx.chooseAddress mailing address
scope.invoiceTitle wx.chooseInvoiceTitle Invoice
scope.invoice wx.chooseInvoice Get invoice
scope.werun wx.getWeRunData Movement micro-channel number of steps
scope.record wx.startRecord recording function
scope.writePhotosAlbum wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum save into the album
scope.camera camera assembly camera

Authorization is valid

Once the user's explicit consent or refused authorization, the delegation relationship will be recorded in the background until the user actively Remove applet.

Best Practices

When really need to use the authorization interface, it launched an application to the authorized user, and clearly explain the reasons to use this feature in the authorization application.

Precautions

  1. wx.authorize({scope: "scope.userInfo"}), The authorization does not pop up a window, use <\ button open-type = " getUserInfo" />
  2. Authorization is required scope.userLocation, scope.userLocationBackgroundwhen you must configure the location Instructions for use .



Sample Code 1

The definition of a record click the button

  lu:function(){
    // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.record']) {
          wx.authorize({
            scope: 'scope.record',
            success() {
              // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
              wx.startRecord()
            },fail(){
              console.log("你没有允许授权")
            }
          })
        }else{
          wx.startRecord({
            
          })
        }
      }
    })
  }

User Info

wxml

<button bindgetuserinfo="user1" open-type="getUserInfo">用户信息</button>

js

  user1: function () {
    // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
            }
          })
        }
      }
    })
  }
})


to sum up

1 因为部分功能需要用同意后才能使用。

2 wx.getSetting来判断该用户有没有对接口授权,我判断哪个接口,就必须给wx.getSetting传对应的scope值
- 一个scope值对应这个一个或多个接口

3 如果我们重wx.getSetting中发现scope值是false,标识没有授权,我们可以通过wx.authorize发起授权,对那个接口授权,就给wx.authorize传对应scope值就可以了。如果用用户同意授权,就可以直接使用对应的接口了。

4 但是scope.userInfo没有办法使用wx.authorize自动弹起弹框。必须要用户手动点击按钮唤起授权弹框。
代码格式:
    <button open-type="getUserInfo" bindgetuserinfo="user1">用户信息</button>
    我们可以再响应函数的参数中获取用户信息。e.detail,这个和直接调用wx.getUserInfo获取的内容一样。

Guess you like

Origin www.cnblogs.com/kai-/p/12481857.html