zoukankan      html  css  js  c++  java
  • 探秘小程序(4):授权

    1.场景:某些微信的api(比如获取用户信息,全部权限请参看文章结尾)需要获得用户权限才可查询,因此就需要有授权。

    2.主要内容:授权相关api只有三个:

      ①wx.getSetting:

      作用:获取用户已经授权权限

      示例:

     1  wx.getSetting({//获取系统设置过的权限
     2       success: (res) => {
     3        console.info('getSetting suc',res);
     4       /*
     5       * res.authSetting = {
     6       *   "scope.userInfo": true,
     7       *   "scope.userLocation": true
     8       * }
     9       */
    10       },
    11       fail: (res)=>{
    12         console.info('getSetting fail', res);
    13       }
    14     })

      ② wx.openSetting

      作用:打开设置界面,引导用户开启授权。

      示例:

     1   wx.openSetting({//调起客户端小程序设置界面,返回用户设置的操作结果。
     2       success: (res) => {
     3         /*
     4          * res.authSetting = {
     5          *   "scope.userInfo": true,
     6          *   "scope.userLocation": true
     7          * }
     8          */
     9         console.info('openSetting Suc', res);
    10       },
    11       fail: (res) => {
    12         console.info('openSetting fail', res);
    13       }
    14     })

      ③wx.authorize

      作用:在调用需授权api前,提前请求用户授权

      示例:

     1   wx.getSetting({
     2       success(res){
     3         if(!res.authSetting['scope.record']){
     4           wx.authorize({
     5             scope: 'scope.record',
     6             success(){
     7               wx.startRecord();
     8             }
     9           })
    10         }
    11       }
    12     })

    3.相关需授权api:

    scope对应接口描述
    scope.userInfo【废弃】 wx.getUserInfo 用户信息
    scope.userLocation wx.getLocation, wx.chooseLocation 地理位置
    scope.address wx.chooseAddress 通讯地址
    scope.invoiceTitle wx.chooseInvoiceTitle 发票抬头
    scope.werun wx.getWeRunData 微信运动步数
    scope.record wx.startRecord 录音功能
    scope.writePhotosAlbum wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum 保存到相册
    scope.camera   摄像头
  • 相关阅读:
    java面试②基础部分
    java面试①整体流程
    Mysql加锁过程详解(6)-数据库隔离级别(2)-通过例子理解事务的4种隔离级别
    Mysql加锁过程详解(6)-数据库隔离级别(1)
    CV3——学习笔记-实战项目(上):如何搭建和训练一个深度学习网络
    CV2——学习笔记-图像分类
    CV1——学习笔记
    思无邪
    C++学习笔记
    操作系统——学习笔记
  • 原文地址:https://www.cnblogs.com/codeww/p/9022238.html
Copyright © 2011-2022 走看看