探秘小程序(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   攝像頭

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM