小程序的授權


官方文檔

<https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/authorize.html>

https://developers.weixin.qq.com/miniprogram/dev/api/open-api/setting/wx.getSetting.html

小程序授權

1 因為部分功能需要用同意后才能使用。

2 wx.getSetting來判斷該用戶有沒有對接口授權,我判斷哪個接口,就必須給wx.getSetting傳對應的scope值
- 一個scope值對應這個一個或多個接口

3 如果我們從wx.getSetting中發現scope值是false,標識沒有授權,我們可以通過wx.authorize發起授權,對那個接口授權,就給wx.authorize傳對應scope值就可以了。如果用用戶同意授權,就可以直接使用對應的接口了。

4 但是scope.userInfo沒有辦法使用wx.authorize自動彈起彈框。必須要用戶手動點擊按鈕喚起授權彈框。
代碼格式:
	<button open-type="getUserInfo" bindgetuserinfo="user1">用戶信息</button>
	我們可以再響應函數的參數中獲取用戶信息。e.detail,這個和直接調用wx.getUserInfo獲取的內容一樣。



獲取用戶授權設置

開發者可以使用 wx.getSetting 獲取用戶當前的授權狀態。

提前發起授權請求

開發者可以使用 wx.authorize 在調用需授權 API 之前,提前向用戶發起授權請求。

scope 列表

scope 對應接口 描述
scope.userInfo wx.getUserInfo 用戶信息
scope.userLocation wx.getLocation, wx.chooseLocation 地理位置
scope.userLocationBackground wx.startLocationUpdateBackground 后台定位
scope.address wx.chooseAddress 通訊地址
scope.invoiceTitle wx.chooseInvoiceTitle 發票抬頭
scope.invoice wx.chooseInvoice 獲取發票
scope.werun wx.getWeRunData 微信運動步數
scope.record wx.startRecord 錄音功能
scope.writePhotosAlbum wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum 保存到相冊
scope.camera camera 組件 攝像頭

授權有效期

一旦用戶明確同意或拒絕過授權,其授權關系會記錄在后台,直到用戶主動刪除小程序。

注意事項

wx.authorize({scope: "scope.userInfo"})  用戶信息  的授權必須button用戶手動觸發彈窗,授權
其他錄音等授權,可以直接寫在生命周期中,自動彈窗,用戶點擊授權
  1. wx.authorize({scope: "scope.userInfo"}),不會彈出授權窗口,請使用 ``

    open-type="getUserInfo"

    <button bindgetuserinfo="user1" open-type="getUserInfo">個人信息1</button>
    
  2. 需要授權 scope.userLocationscope.userLocationBackground 時必須配置地理位置用途說明

后台定位

與其它類型授權不同的是,scope.userLocationBackground 不會彈窗提醒用戶。需要用戶在設置頁中,主動將“位置信息”選項設置為“使用小程序期間和離開小程序后”。開發者可以通過調用wx.openSetting,打開設置頁。

background-location

案例:個人信息getUserInfo

# wxml文件:
<button bindtap="lu">錄音</button>
<button bindtap="user" open-type="getUserInfo">個人信息</button>
<button bindgetuserinfo="user1" open-type="getUserInfo">個人信息1</button>


# js文件:
lu:function(){
    wx.getSetting({
      success(res){
        if (!res.authSetting['scope.record']) {
          wx.authorize({
            scope: 'scope.record',  // 授權的功能
            success() {
              // 用戶已經同意小程序使用錄音功能,后續調用 wx.startRecord 接口不會彈窗詢問
              wx.startRecord()  // 使用接口
            }, fail() {  // 用戶不同意進入fail回調
              console.log("你沒有授權")
            }
          })
        } else {
          wx.startRecord()  //使用接口
        }
      }
    })
  },
  user: function () {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userinfo',  // 授權的功能
            success() {
              console.log('進來了')
              wx.startRecord()  // 使用接口
            }
          })
        } else {
          console.log('已經授權了')
          wx.startRecord()  //使用接口
        }
      }
    })
  },

  user1:function(e){
    console.log('e',e.detail)
    wx.getSetting({
      success(res){
        if (res.authSetting['scope.userInfo']){
          wx.getUserInfo({
            success:(res) => {
              console.log('res',res)
            }
          })
        }
      }
    })
  },


錄音等,可以寫在onLaunch中

App({
  onLaunch: function () {
  // 獲取用戶信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.record']) {
          // 已經授權,可以直接調用 record 獲取頭像昵稱,不會彈框
          wx.getUserInfo({
            success: res => {
              // 可以將 res 發送給后台解碼出 unionId
              this.globalData.userInfo = res.record

              // 由於 record 是網絡請求,可能會在 Page.onLoad 之后才返回
              // 所以此處加入 callback 以防止這種情況
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })


免責聲明!

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



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