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

Example analysis of WeChat applet implementing synchronous request authorization

Sep 12, 2017 am 11:38 AM
Applets Authorize ask

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'])
Copy after login

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 }
Copy after login


// 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()
      }
     })
    })
   })
  })
})
Copy after 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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to upgrade win10 enterprise version 2016 long-term service version to professional version How to upgrade win10 enterprise version 2016 long-term service version to professional version Jan 03, 2024 pm 11:26 PM

When we no longer want to continue using the current Win10 Enterprise Edition 2016 Long-Term Service Edition, we can choose to switch to the Professional Edition. The method is also very simple. We only need to change some contents and install the system image. How to change win10 enterprise version 2016 long-term service version to professional version 1. Press win+R, and then enter "regedit" 2. Paste the following path directly in the address bar above: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\CurrentVersion3 , then find the EditionID and replace the content with "professional" to confirm

Implement card flipping effects in WeChat mini programs Implement card flipping effects in WeChat mini programs Nov 21, 2023 am 10:55 AM

Implementing card flipping effects in WeChat mini programs In WeChat mini programs, implementing card flipping effects is a common animation effect that can improve user experience and the attractiveness of interface interactions. The following will introduce in detail how to implement the special effect of card flipping in the WeChat applet and provide relevant code examples. First, you need to define two card elements in the page layout file of the mini program, one for displaying the front content and one for displaying the back content. The specific sample code is as follows: <!--index.wxml-->&l

Alipay launched the 'Chinese Character Picking-Rare Characters' mini program to collect and supplement the rare character library Alipay launched the 'Chinese Character Picking-Rare Characters' mini program to collect and supplement the rare character library Oct 31, 2023 pm 09:25 PM

According to news from this site on October 31, on May 27 this year, Ant Group announced the launch of the "Chinese Character Picking Project", and recently ushered in new progress: Alipay launched the "Chinese Character Picking-Uncommon Characters" mini program to collect collections from the society Rare characters supplement the rare character library and provide different input experiences for rare characters to help improve the rare character input method in Alipay. Currently, users can enter the "Uncommon Characters" applet by searching for keywords such as "Chinese character pick-up" and "rare characters". In the mini program, users can submit pictures of rare characters that have not been recognized and entered by the system. After confirmation, Alipay engineers will make additional entries into the font library. This website noticed that users can also experience the latest word-splitting input method in the mini program. This input method is designed for rare words with unclear pronunciation. User dismantling

How uniapp achieves rapid conversion between mini programs and H5 How uniapp achieves rapid conversion between mini programs and H5 Oct 20, 2023 pm 02:12 PM

How uniapp can achieve rapid conversion between mini programs and H5 requires specific code examples. In recent years, with the development of the mobile Internet and the popularity of smartphones, mini programs and H5 have become indispensable application forms. As a cross-platform development framework, uniapp can quickly realize the conversion between small programs and H5 based on a set of codes, greatly improving development efficiency. This article will introduce how uniapp can achieve rapid conversion between mini programs and H5, and give specific code examples. 1. Introduction to uniapp unia

How to use Flask-Security to implement user authentication and authorization How to use Flask-Security to implement user authentication and authorization Aug 04, 2023 pm 02:40 PM

How to use Flask-Security to implement user authentication and authorization Introduction: In modern web applications, user authentication and authorization are essential functions. To simplify this process, Flask-Security is a very useful extension that provides a series of tools and functions to make user authentication and authorization simple and convenient. This article will introduce how to use Flask-Security to implement user authentication and authorization. 1. Install the Flask-Security extension: at the beginning

UniApp implements detailed analysis of user login and authorization UniApp implements detailed analysis of user login and authorization Jul 05, 2023 pm 11:54 PM

UniApp implements detailed analysis of user login and authorization. In modern mobile application development, user login and authorization are essential functions. As a cross-platform development framework, UniApp provides a convenient way to implement user login and authorization. This article will explore the details of user login and authorization in UniApp, and attach corresponding code examples. 1. Implementation of user login function Create login page User login function usually requires a login page, which contains a form for users to enter their account number and password and a login button

How to implement authentication and authorization in PHP applications using JWT How to implement authentication and authorization in PHP applications using JWT Aug 03, 2023 pm 10:17 PM

How to use JWT to implement authentication and authorization in PHP applications Introduction: With the rapid development of the Internet, authentication and authorization are becoming increasingly important in web applications. JSONWebToken (JWT) is a popular authentication and authorization mechanism that is widely used in PHP applications. This article will introduce how to use JWT to implement authentication and authorization in PHP applications, and provide code examples to help readers better understand the use of JWT. 1. Introduction to JWT JSONWebTo

How to get authorization for Douyin slices and goods? Is Douyin slicing easy to make? How to get authorization for Douyin slices and goods? Is Douyin slicing easy to make? Mar 07, 2024 pm 10:52 PM

Douyin, as a popular social media platform at the moment, not only provides people with a wealth of entertainment content, but has also become an important channel for many brands and merchants to promote products and achieve sales. Among them, Douyin’s slicing and selling products has become a novel and efficient marketing method. So, how do you get authorization for Douyin's sliced ​​products? 1. How do you get authorization for Douyin's sliced ​​products? Douyin's sliced ​​products decompose long videos into short video clips and embed product promotion information in them to attract viewers to buy. . When slicing and selling goods on Douyin, the first step is to obtain authorization from the original video. When looking for a suitable licensor, you can consider using various channels such as Douyin platform, social media and industry forums. Find creators or copyright holders with popular video content and actively connect with them,

See all articles