Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The WeChat program API modifies the authorization information for the small program in the settings interface


May 19, 2021 WeChat Mini Program Development Document


Table of contents


wx.authorize(OBJECT)


Base library 1.2.0 starts to support, and low versions need to be compatible

Some interfaces need to be approved before they can be called. W hen such an interface is called, if the user is not authorized, the user is asked by the window, and the user clicks on consent before calling the interface. I f the user points to Reject, the call does not appear in the short term, but goes directly to the fail callback. U sers can modify authorization information for a small program in the program settings interface. T his interface is used to initiate authorization to the user in advance, and immediately after the call, the window asks the user if he or she consents to the program using a feature or obtains some of the user's data, but does not actually call the interface. If the user has previously agreed, there will be no e-windows and the direct return will be successful.

OBJECT parameter description:

The name of the argument Type Required Description
scope String Is Scopes that require permission can be found in the scope list
success Function Whether The interface calls a successful callback function
fail Function Whether The interface calls the failed callback function
complete Function Whether Callback function at end of interface call (call succeeds, fails are executed)

Success returns a description of the parameters:

The name of the argument Type Description
errMsg String The result of the call

Example code:

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

Scope list

scope The corresponding interface Describe
scope.userInfo wx.getUserInfo User information
scope.userLocation wx.getLocation, wx.chooseLocation Location
scope.address wx.chooseAddress The mailing address
scope.record wx.startRecord Recording function
scope.writePhotosAlbum wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum Save to album