小程序 有的一些 需要 用戶 先 給授權 才能調用其方法,比較 錄音,保存圖,位置信息等
wx.getSetting({ success(res) { if (!res.authSetting['scope.camera']) { //獲取攝像頭權限 wx.authorize({ scope:'scope.camera', success() { console.log('授權成功') }, fail() { wx.showModal({ title: '提示', content: '尚未進行授權,部分功能將無法使用', showCancel: false, success(res) { if (res.confirm) { console.log('用戶點擊確定') wx.openSetting({ //這里的方法是調到一個添加權限的頁面,可以自己嘗試 success: (res) => { if (!res.authSetting['scope.camera']) { wx.authorize({ scope: 'scope.camera', success() { console.log('授權成功') }, fail() { console.log('用戶點擊取消') } }) } }, fail: function () { console.log("授權設置錄音失敗"); } }) } else if (res.cancel) { console.log('用戶點擊取消') } } }) } }) }; if (!res.authSetting['scope.record']) { //獲取錄音權限 wx.authorize({ scope: 'scope.record', success() { console.log('授權成功') }, fail() { wx.showModal({ title: '提示', content: '尚未進行授權,部分功能將無法使用', showCancel: false, success(res) { if (res.confirm) { wx.openSetting({ success: (res) => { if (!res.authSetting['scope.record']) { wx.authorize({ scope: 'scope.record', success() { console.log('授權成功') }, fail() { console.log('用戶點擊取消') } }) } }, fail: function () { console.log("授權設置錄音失敗"); } }) } else if (res.cancel) { console.log('用戶點擊取消') } } }) } }) } }, fail(res){ } })
重點是openSetting,getSetting的使用,可以 看微信平台這方法的。 這里是最原始的獲取權限的代碼,完全有可優化的空間。 拋磚引玉。