微信授權(quán)
問題
小程序有時(shí)需要調(diào)用一些權(quán)限才能使用兜喻。比如用戶信息互订,獲取地理位置,獲取錄音等涛漂,這時(shí)就會(huì)涉及用戶體驗(yàn)等
有些授權(quán)并不是剛需的篇梭,所以也給出未授權(quán)的格式就可以了氢橙,但是有些授權(quán)是剛需的,也就需要用戶打開授權(quán)恬偷。
不管是不是剛需悍手,如果需要再重新授權(quán)就比較麻煩了。所以一下就是解決如果用戶點(diǎn)擊拒絕的情況下袍患,某些功能需要再次授權(quán)
image.png
wx.openSetting()
調(diào)起客戶端小程序設(shè)置頁面坦康,返回用戶設(shè)置的操作結(jié)果,設(shè)置頁面只會(huì)出現(xiàn)小程序已經(jīng)向用戶請求過得權(quán)限
當(dāng)出現(xiàn)授權(quán)頁面用戶點(diǎn)擊拒絕之后诡延,剛需的直接給用戶提示滞欠,并提醒用戶開啟權(quán)限,否則功能不能用肆良,使用openSeting 就會(huì)出現(xiàn)
image.png
當(dāng)點(diǎn)擊確定的時(shí)候會(huì)跳轉(zhuǎn)頁面
image.png
wx.getSetting({
//getSetting不管授權(quán)還是未授權(quán)都會(huì)進(jìn)入success 但是返回的值會(huì)是false
success(res) {
if (!res.authSetting['scope.record']) { // 未授權(quán)錄音
wx.authorize({
scope: 'scope.record',
success(res) {
that.startRecording();// 這個(gè)是調(diào)用成功后的函數(shù)
},
fail(res) {
// INTERACTION.showToast('請授權(quán)錄音功能');
that.openShowSetting() //再次調(diào)用openSetting去打開設(shè)置
}
});
}else{
// 授權(quán)錄音
that.startRecording()
}
}
});
openShowSetting:function(){
var that = this;
wx.showModal({
title: '提示',
content: '小程序需要獲取錄音權(quán)限才能使用錄音筛璧,請點(diǎn)擊確認(rèn)',
success:function(res){
console.log("openseting",res)
if(res.confirm){
wx.openSetting({
success: (res) => {
console.log("openssssssss", res)
var userInfo = res.authSetting['scope.record'];
if (!userInfo) {
wx.authorize({
scope: 'scope.record',
success(res) {
console.log("111", res)
that.startRecording();
},
fail(res) {
// INTERACTION.showToast('請授權(quán)錄音功能');
that.openShowSetting()
}
});
} else {
that.startRecording()
}
}
})
}else{
console.log("點(diǎn)擊取消了")
}
}
})
},
image.png