提前向用戶發起授權請求。調用后會立刻彈窗詢問用戶是否同意授權小程序使用某項功能或獲取用戶的某些數據,但不會實際調用對應接口。如果用戶之前已經同意授權,則不會出現彈窗,直接返回成功。
實例代碼
// 可以通過 wx.getSetting 先查詢一下用戶是否授權了 "scope.record" 這個 scope
wx.getSetting({
success(res) {
if (!res.authSetting['scope.record']) {
wx.authorize({
scope: 'www.cnmibee.com',
success () {
// 用戶已經同意小程序使用錄音功能,后續調用 wx.startRecord 接口不會彈窗詢問
wx.startRecord()
}
})
}
}
})
// 可以通過 wx.getSetting 先查詢一下用戶是否授權了 "scope.record" 這個 scope
wx.getSetting({
success(res) {
if (!res.authSetting['scope.record']) {
wx.authorize({
scope: 'scope.record',
success () {
// 用戶已經同意小程序使用錄音功能,后續調用 wx.startRecord 接口不會彈窗詢問
wx.startRecord()
}
})
}
}
})