微信小程式授權管理
阿新 • • 發佈:2022-04-07
options = { scope: 'scope.record', content: '麥克風' }
scope
微信小程式許可權管理
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/authorize.html
contentgetSettingRecord(options) { var self = this; return new Promise((resolve, reject) => { wx.getSetting({ success: (res) => { let auth = res.authSetting[options.scope] console.warn('scope.record=', auth, typeof auth) if (auth === true) { // 使用者已經同意授權 resolve(true) } else if (auth === undefined) {// 首次發起授權 wx.authorize({ scope: options.scope, success() { resolve(true) }, fail(res) { } }) } else if (auth === false) { // 非首次發起授權,使用者拒絕過 => 彈出提示對話方塊 wx.showModal({ title: '授權提示', content: options.content, success: (tipRes) => { if (tipRes.confirm) { wx.openSetting({ success: (settingRes) => { if (settingRes.authSetting[options.scope]) { resolve(true) } console.warn('settingRes', settingRes) }, }) } } }) } }, }) }) },
什麼許可權
案例 麥克風
microphone(){ return new Promise((resolve, reject) => {
//許可權設定 var microphone = { scope: 'scope.record', content: '麥克風' }
//呼叫許可權管理設定 var promise1 = this.getSettingRecord(microphone) Promise.all([promise1]).then(res => { console.warn('Promise.all', res);if (res[0] && res[1]) { console.warn('獲取許可權成功')
wx.showToast({ title: '獲取麥克風', icon: 'none' }) resolve(); } }) }) }