Home >WeChat Applet >Mini Program Development >Example analysis of WeChat applet implementing synchronous request authorization

Example analysis of WeChat applet implementing synchronous request authorization

黄舟
黄舟Original
2017-09-12 11:38:563254browse

This article mainly introduces the relevant information on the detailed explanation of the WeChat applet's synchronous request authorization. When the applet is opened for the first time, I need to request multiple permissions at the same time, and the user authorizes them one by one. To realize such a requirement, it is necessary Friends can refer to the following

Detailed explanation of WeChat applet synchronization request authorization

Requirement analysis:

1. When the mini program is opened for the first time, I need to request multiple permissions at the same time, and the user authorizes them one by one.


([‘scope.userInfo',‘scope.userLocation',‘scope.address',‘scope.record',‘scope.writePhotosAlbum'])

Problem analysis:

1. The wx.authorize interface is called at the same time, requesting multiple permissions , due to asynchronous reasons, the authorization request is sent together, which obviously does not meet the requirements.

2. Promise can solve the problem very well. I tried it. The following code is divided into two files.


// scope.js
import es6 from '../helpers/es6-promise'

// 获取用户授权
function getScope(scopeName) {
 return new es6.Promise(function (resolve, reject) {
  // 查询授权
  wx.getSetting({
   success(res) {
    if (!res.authSetting[scopeName]) {
     // 发起授权
     wx.authorize({
      scope: scopeName,
      success() {
       resolve(0)
      }, fail() {
       resolve(1)
      }
     })
    }
   }
  })
 })
}

module.exports = { getScope: getScope }


// index.js
import scope from "../../service/scope"
Page({
onShow() {
  let list = ["scope.userInfo", "scope.userLocation", "scope.address", "scope.record"];
  // 记录请求结果
  let num = 0;
  // 问题1:怎么改成循环方式?
  scope.getScope(list[0]).then(function (res) {
   num += res;
   scope.getScope(list[1]).then(function (res) {
    num += res;
    scope.getScope(list[2]).then(function (res) {
     num += res;
     scope.getScope(list[3]).then(function (res) {
      num += res;
      // 调起设置界面
      if (num) {
       wx.openSetting({
        success(res) {
         // 允许获取用户信息
         if (res.authSetting["scope.userInfo"])
          userService.login()
        }
       })
      } else {
       userService.login()
      }
     })
    })
   })
  })
})

Analysis and solution:

1. Problem 1 in the code The writing method is too stupid, but I try to call the writing method through a loop, and I don’t know how to deal with the callback problem.

2.wx.authorize interface, the official explanation of the success parameter is (callback function for successful interface call), but this is not the case. In fact, the interface call is successful and the permissions specified by the scope are obtained.

The above is the detailed content of Example analysis of WeChat applet implementing synchronous request authorization. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn