


Example analysis of WeChat applet implementing synchronous request authorization
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment