1. 獲取微信收貨地址(處理用戶允許授權、拒絕授權的所有情況)
getAddress: function () { var t = this; wx.getSetting({ success(res) { console.log("vres.authSetting['scope.address']:", res.authSetting['scope.address']) var openid = wx.getStorageSync("openid"); console.log(openid); //根據用戶當前設置中的用戶授權結果,判斷是否包含收貨地址授權 //用戶已授權使用收貨地址 if (res.authSetting['scope.address']) { console.log("111") //選擇收貨地址 wx.chooseAddress({ success(res) { app.util.request({ url: "entry/wxapp/Member_address", header: { "content-type": "application/x-www-form-urlencoded" }, data: { username: res.userName, postalcode: res.postalCode, provincename: res.provinceName, cityname: res.cityName, countyname: res.countyName, detailinfo: res.detailInfo, telnumber: res.telNumber, openid: openid }, success: function (res) { console.log('成功2', res); t.setData({ noAddress: false }) wx.setStorage({ key: 'noAddress', data: t.data.noAddress, }) } }); } }) // 用戶未同意小程序使用通訊地址時 } else { console.log('222'); //取消過收貨地址授權,調用wx.openSetting(),調起客戶端小程序設置界面讓用戶去打開授權 if (res.authSetting['scope.address'] == false) { wx.openSetting({ success(res) { console.log('333'); console.log(res.authSetting) } }) } else { //用戶當前設置不包含收貨地址授權(說明是第一次打開獲取用戶收貨地址信息的授權),調用wx.chooseAddress(),獲取用戶收貨地址 console.log('444'); wx.chooseAddress({ success(res) { app.util.request({ url: "entry/wxapp/Member_address", header: { "content-type": "application/x-www-form-urlencoded" }, data: { username: res.userName, postalcode: res.postalCode, provincename: res.provinceName, cityname: res.cityName, countyname: res.countyName, detailinfo: res.detailInfo, telnumber: res.telNumber, openid: openid }, success: (res)=> { console.log('成功1',res); t.setData({ noAddress: false }) wx.setStorage({ key: 'noAddress', data: t.data.noAddress, }) } }); } }) } } } }) }
2. 獲取攝像頭授權(處理用戶允許、拒絕的情況)
實現效果:
(1)首次進入時,調起授權詢問框
(2)若用戶拒絕,顯示警告框
(3)若用戶授權,調起客戶端小程序設置界面
wxml代碼
<camera wx:if="{{camera}}" device-position="back" flash="off" binderror="error" class='camera_box'></camera>
js代碼
//用戶拒絕授權攝像頭 error: function(){ wx.showModal({ title: '警告', content: '若不授權使用攝像頭,將無法使用拍照識別功能!', cancelText: '不授權', cancelColor: '#1ba9ba', confirmText: '授權', confirmColor: '#1ba9ba', success(res) { //允許打開授權頁面 if (res.confirm) { //調起客戶端小程序設置界面,返回用戶設置的操作結果 wx.openSetting({ success(res) { res.authSetting = { "scope.camera": true } }, fail(err) { console.log('err', err); } }) } else if (res.cancel) {//拒絕打開授權頁面 wx.redirectTo({ url: '../selcat/selcat', }) } } }) }
補充:項目中,重新調起攝像頭授權不能與獲取收貨地址使用相同邏輯,主要是因為wx.openSetting API必須通過點擊才能調用
詳情,可搜索小程序“垃圾該去哪”