Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are WeChat Mini Programs's permission interfaces and how to use them?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is WeChat Mini Programs's permission interface and how to use it?" the content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "what are WeChat Mini Programs's permission interfaces and how to use them?"

1. Permission API

Part of the    API needs to be authorized by the user before it can be called. We divide these APIs into multiple scope according to the scope of use. The user chooses to authorize the scope. When authorized to a scope, all the corresponding APIs can be used directly. When such APIs are called:

If the user does not accept or deny this permission, a pop-up window will ask the user, and the user can not call the API until he / she clicks Unification.

If the user is authorized, you can call the interface directly.

If the user has refused authorization, there will be no pop-up window, but will go directly to the API fail callback.

The corresponding relationship between the fields of the object scope in the permissions of this type of interface and the interface is shown in the following table.

Scope corresponding API description scope.userLocationwx.getLocation, wx.chooseLocation, wx.startLocationUpdate Geographic location scope.userLocationBackgroundwx.startLocationUpdateBackground background location scope.recordwx.startRecord, wx.joinVoIPChat, RecorderManager.start microphone scope.cameracamera components, wx.createVKSession camera scope.bluetoothwx.openBluetoothAdapter, wx.createBLEPeripheralServer Bluetooth scope.writePhotosAlbumwx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum add to album scope.addPhoneContactwx.wx.addPhoneContact add to contact scope.addPhoneCalendarwx.addPhoneRepeatCalendar Wx.addPhoneCalendar added to Calendar scope.werunwx.getWeRunData Wechat Movement steps scope.addresswx.chooseAddress mailing address (revoked You can call the corresponding API directly) scope.invoiceTitlewx.chooseInvoiceTitle invoice title (revoked, you can call the corresponding API directly) scope.invoicewx.chooseInvoice to obtain invoices (cancelled, you can call the corresponding API directly) scope.userInfowx.getUserInfo user information (Mini Program has been recycled, please use avatar nickname, Mini Game can continue to call)

   Mini Program provides three sets of interfaces to perform corresponding operations on interface permissions: wx.getSetting () to obtain the user's current authorization status; wx.openSetting () to open the settings interface to guide users to turn on authorization; and wx.authorize () to change the authorization status.

1.1 user Authorization Interface wx.authorize (Object object)

   initiates an authorization request to the user in advance. Immediately after the API is called, a pop-up window will ask the user whether they agree to authorize Mini Program to use a certain feature or obtain some user data, but the corresponding API will not be actually called. If the user has previously agreed to the authorization, no pop-up window will appear and the success will be returned directly. The properties are shown in the following table.

Attribute type default value is required description scopestring

Is the scope that needs to obtain permission. For more information, please see scope list successfunction

No API calls successful callback function failfunction

No API calls the failed callback function completefunction

No API calls the finished callback function (both successful and failed calls will be executed)

Sample code for    official website:

/ / you can use wx.getSetting to check whether the user has authorized the scopewx.getSetting "scope.record" ({success (res) {if (! res.authSetting ['scope.record']) {wx.authorize ({scope:' scope.record', success () {/ / users have agreed to use the recording feature on Mini Program). Subsequent calls to the wx.startRecord API will not pop up to ask wx.startRecord ()}) 1.2 get the user rights setting interface wx.getSetting (Object object)

   this interface gets the user's current settings. Only the permissions that Mini Program has requested from the user appear in the return value. The attributes are as follows:

The default value of attribute type is required to indicate whether the minimum version of withSubscriptionsBooleanfalse acquires the subscription status of user subscription messages at the same time. It is not available by default. Note: withSubscriptions only returns subscription messages in which the user checked "always keep the above choices, no more questions" in the subscription panel. 2.10.1successfunction

No API calls a successful callback function

Failfunction

No API calls failed callback function

Completefunction

No API calls the finished callback function (both successful and failed calls will be executed)

The object.success callback function is as follows:

The attribute type describes the user authorization result of the minimum version of authSettingAuthSetting

The subscriptionsSettingSubscriptionsSetting user subscribes to the message setting. The API parameter withSubscriptions is returned when the value is true. When 2.10.1miniprogramAuthSettingAuthSetting is called in the plug-in, the user authorization result of the current host Mini Program

Sample code of    official website:

Wx.getSetting ({success (res) {console.log (res.authSetting) / / res.authSetting = {/ / "scope.userInfo": true, / / "scope.userLocation": true / /}) 1.3 Open the user rights setting interface wx.openSetting (Object object)

   this API calls the Mini Program configuration interface of the client and returns the operation result set by the user. The settings interface will only show the permissions that Mini Program has already requested from the user.

The default value of attribute type is required to indicate whether the minimum version of withSubscriptionsBooleanfalse acquires the subscription status of user subscription messages at the same time. It is not available by default. Note: withSubscriptions only returns subscription messages in which the user checked "always keep the above choices, no more questions" in the subscription panel. 2.10.3successfunction

No API calls a successful callback function

Failfunction

No API calls failed callback function

Completefunction

No API calls the finished callback function (both successful and failed calls will be executed)

The object.success callback function is as follows:

The attribute type describes the user authorization result of the minimum version of authSettingAuthSetting

The subscriptionsSettingSubscriptionsSetting user subscribes to the message setting. The API parameter withSubscriptions is returned when the value is true. 2.10.3

Note: starting from version 2.3.0, users can only jump to open the settings page and manage authorization information after the user clicks.

Sample code of    official website:

Wx.openSetting ({success (res) {console.log (res.authSetting) / / res.authSetting = {/ / "scope.userInfo": true, / / "scope.userLocation": true / /}}) 2. Authorization case

   this example uses the geolocation API wx.getLocation () and the start recording API wx.startRecord () for related operations, both of which need to set operation permissions.

The Setting.wxml code is as follows:

Get geolocation {{context}} to start recording

The Setting.js code is as follows:

/ / index.js// gets the application instance const app = getApp () Page ({data: {}, onLoad: function () {context:''}) Location1:function () {/ / get geolocation var that=this wx.getSetting ({/ / API success (res) {console.log (res) if (! res.authSetting ['scope.userLocation']) {wx.authorize ({/ / authorize scope:' scope.userLocation', / / geolocation permission) Look at the parameter success () {wx.getLocation ({/ / get the current geographic location success: function (res) {console.log (res) that.setData ({context: "your longitude is" + res.latitude+ "and your latitude is" + res.longitude})} })}})} Location2: function () {/ / recording var that = this wx.getSetting ({success (res) {console.log (res.authSetting) if (! res.authSetting ['scope.record']) {wx.openSetting ({/ / Open the user rights settings interface) Success (res) {console.log (res) wx.startRecord ({/ / start recording success (res) {const tempFilePath = res.tempFilePath console.log ("end of recording")} })}))

The    location () 1 function realizes the function of obtaining the geographic location. This function first calls the wx.getSetting () API to obtain the permission status, and then calls the wx.authorize () API to modify the geographic location permission scope.userLocation. The location2 () function realizes the recording function, which first calls the wx.getSetting () API to obtain the permission status, and then calls the wx.openSetting () API to open the recording permission setting interface to modify the recording permission. It can be seen from this example that when setting permissions, you should first call the wx.getSetting () API to modify the permission status. If you do not have the permission to open, you can call the wx.authorize () API or the wx.openSetting () API to modify the permission status. The wx.authorize () API does not have the operation permission to modify the permission, while the wx.openSetting () API will have the operation interface to modify the permission.

Thank you for your reading. the above is the content of "what is WeChat Mini Programs's permission interface and how to use it?" after the study of this article, I believe you have a deeper understanding of what WeChat Mini Programs's permission interface has and how to use it. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report