• 探秘小程序(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-马士兵设计模式学习笔记-迭代器模式-模仿Collectin ArrayList LinckedList
    Java-马士兵设计模式学习笔记-装饰者模式
    1072 Gas Station (30)(30 分)
    1034 Head of a Gang (30)(30 分)
    poj 3723 Conscription
    qduoj 218 签到题
    1045 Favorite Color Stripe (30)(30 分)
    1068 Find More Coins (30)(30 分)
    1057 Stack (30)(30 分)
  • 原文地址:https://www.cnblogs.com/codeww/p/9022238.html
Copyright © 2020-2023  润新知