Home >WeChat Applet >Mini Program Development >Summarize and organize WeChat applet permission interfaces
This article brings you related issues about WeChat Mini Program. It mainly introduces the relevant content of the permission interface in the WeChat Mini Program, including the user authorization interface and obtaining user permission settings. interface, opening the user permission setting interface, etc. Let’s take a look at it together. I hope it will be helpful to everyone.
【Related learning recommendations: 小program learning tutorial】
Some interfaces are required It can be called only after user authorization. We divide these interfaces into multiple scopes according to their usage scope. The user chooses to authorize the scope. After authorizing a scope, all its corresponding interfaces can be used directly. When such interfaces are called:
The corresponding relationship between the object scope fields in the permissions of this type of interface and the interface is as shown in the following table.
scope | Corresponding interface | Description | |||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
scope .userLocation | wx.getLocation, wx.chooseLocation, wx.startLocationUpdate | Geolocation | |||||||||||||||||||||||||||||||||||||||||||
wx. startLocationUpdateBackground | Background location | ||||||||||||||||||||||||||||||||||||||||||||
wx.startRecord, wx.joinVoIPChat, RecorderManager.start | Microphone | ||||||||||||||||||||||||||||||||||||||||||||
camera component, wx.createVKSession | Camera | ||||||||||||||||||||||||||||||||||||||||||||
wx.openBluetoothAdapter, wx.createBLEPeripheralServer | Bluetooth | ||||||||||||||||||||||||||||||||||||||||||||
wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum | Add to album | ||||||||||||||||||||||||||||||||||||||||||||
wx.wx.addPhoneContact | Add to contact | ||||||||||||||||||||||||||||||||||||||||||||
wx.addPhoneRepeatCalendar, wx.addPhoneCalendar | Add to calendar | ||||||||||||||||||||||||||||||||||||||||||||
wx. getWeRunData | WeChat exercise steps | ||||||||||||||||||||||||||||||||||||||||||||
wx.chooseAddress | Correspondence address (authorization has been cancelled, you can directly Call the corresponding interface) | ||||||||||||||||||||||||||||||||||||||||||||
wx.chooseInvoiceTitle | Invoice header (authorization has been cancelled, you can directly call the corresponding interface) | ||||||||||||||||||||||||||||||||||||||||||||
wx.chooseInvoice | Get the invoice (authorization has been cancelled, you can directly call the corresponding interface) | ||||||||||||||||||||||||||||||||||||||||||||
wx.getUserInfo | User information (the mini program has been recycled, please fill it in with your avatar nickname, and the mini game can continue to be called) |
Attribute | Type | Default value | Required | Description |
---|---|---|---|---|
scope | #string | is the scope that | needs to obtain permissions for. See the scope list for details | |
success | function | No | The interface call was successful Callback function | |
fail | function | Callback function that fails to call the interface | ||
function | The callback function that ends the interface call (call Both success and failure will be executed) |
// 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scopewx.getSetting({ success(res) { if (!res.authSetting['scope.record']) { wx.authorize({ scope: 'scope.record', success () { // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问 wx.startRecord() } }) } }})1.2 Get user permission setting interface
Type | Default value | Required | Description | Minimum version | |
---|---|---|---|---|---|
Boolean | false | No | Whether to obtain the subscription status of the user's subscription message at the same time, it is not obtained by default. Note: withSubscriptions only returns subscription messages for which the user has checked "Always keep the above selection and never ask again" in the subscription panel. | 2.10.1 | |
function | ##No | Callback function for successful interface call ||||
No | Callback function for failed interface call
##complete |
||||
No |
The callback function at the end of the interface call (will be executed if the call is successful or failed) |
| object.success callback function is as follows:
Description | Minimum version | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
User authorization result |
subscriptionsSetting |
||||||||||||||||||||||||||||||||||||||||||||
User subscription message settings, the interface parameter | withSubscriptionswill be returned only when the value is | true. 2.10.1
|
miniprogramAuthSetting | ||||||||||||||||||||||||||||||||||||||||||
When called in the plug-in, the user authorization result of the current host applet |
官网示例代码: wx.getSetting({ success (res) { console.log(res.authSetting) // res.authSetting = { // "scope.userInfo": true, // "scope.userLocation": true // } }}) 1.3 打开用户权限设置接口
|
属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|---|
withSubscriptions | Boolean | false | 否 | 是否同时获取用户订阅消息的订阅状态,默认不获取。注意:withSubscriptions 只返回用户勾选过订阅面板中的“总是保持以上选择,不再询问”的订阅消息。 | 2.10.3 |
success | function | 否 | 接口调用成功的回调函数 | ||
fail | function | 否 | 接口调用失败的回调函数 | ||
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
object.success回调函数如下:
属性 | 类型 | 说明 | 最低版本 |
---|---|---|---|
authSetting | AuthSetting | 用户授权结果 | |
subscriptionsSetting | SubscriptionsSetting | 用户订阅消息设置,接口参数withSubscriptions 值为true 时才会返回。 |
2.10.3 |
官网示例代码:
wx.openSetting({ success (res) { console.log(res.authSetting) // res.authSetting = { // "scope.userInfo": true, // "scope.userLocation": true // } }})
本例使用获取地理位置接口wx.getLocation()
和开始录音接口wx.startRecord()
进行相关操作,而这两个接口都需要设置操作权限。
Setting.wxml代码如下:
<!--index.wxml--><view>获取地理位置</view><view>{{context}}</view><view>开始录音</view>
Setting.js代码如下:
//index.js//获取应用实例const app = getApp()Page({ data: { }, onLoad: function () { context:'' }, location1:function(){ //获取地理位置 var that=this wx.getSetting({ //获取用户权限设置接口 success(res) { console.log(res) if (!res.authSetting['scope.userLocation']) { wx.authorize({ //授权 scope: 'scope.userLocation', //地理位置权限,看线上面的scope对应的参数 success() { wx.getLocation({ //获取当前的地理位置 success: function(res) { console.log(res) that.setData({ context: "你所在的经度是" + res.latitude+"你所在的纬度是"+res.longitude}) }, }) } }) } } }) }, location2: function () { //录音 var that = this wx.getSetting({ success(res) { console.log(res.authSetting) if (!res.authSetting['scope.record']) { wx.openSetting({ //打开用户权限设置界面 success(res) { console.log(res) wx.startRecord({ //开始录音 success(res) { const tempFilePath = res.tempFilePath console.log("录音结束") } }) } }) }} }) }})
location()1函数实现获取地理位置的功能,该函数先调用wx.getSetting()接口获取权限状态,然后调用wx.authorize()接口修改地理位置权限scope.userLocation。location2()函数实现录音功能,该函数先调用wx.getSetting()接口获取权限状态,然后调用wx.openSetting()接口打开录音权限设置界面来修改录音权限。从本例可以看出设置权限的时候应该先调用wx.getSetting()接口来修改权限状态,在没有权限打开的情况下可以调用wx.authorize()接口或者wx.openSetting()接口来修改权限状态,wx.authorize()接口不出现修改权限的操作权限,而wx.openSetting()接口会出现修改权限的操作界面。
案例效果如下:
点击获取地理位置:
点击允许之后会显示当前所在的经纬度。
点击开始录音按钮出现麦克风授权:
【相关学习推荐:小程序学习教程】
The above is the detailed content of Summarize and organize WeChat applet permission interfaces. For more information, please follow other related articles on the PHP Chinese website!