想把wx.getSetting封装起来,方便后面调用,但是貌似不行呀
function isAuthSetting() {
wx.getSetting({
success: res=>{
if(res.authSetting['scope.userInfo']) {
return true
}else{
return false
}
},
fail: res=>{
return false
}
})
}
// console.log(this.isAuthSetting()) 返回undefined,这是为什么?
// 也不能用if(this.isAuthSetting())
// 有解决办法吗
isAuthSetting(){ return new Promise((resolve, reject) => { wx.getSetting({ success: res=>{ if(res.authSetting['scope.userInfo']) { resolve(true); }else{ reject(false); } }, fail: res=>{ reject(false); } }) }) } isAuthSetting().then((res) =>{ console.log('已授权'); }).catch((err) =>{ console.log('未授权'); })
async isAuthSetting(scope, withSubscriptions = false){ var scope = scope ? 'scope.'+scope.replace(/^scope\./, '') : '', { authSetting = false } = await wx.getSetting({ withSubscriptions }) return scope != '' && authSetting && authSetting[scope] ? !0 : !1 } // 写userInfo 或 scope.userInfo 都可以 isAuthSetting('userInfo').then(res =>{ console.log(res ? 'userInfo 已授权' : 'userInfo 未授权'); }).catch(e =>{ console.log('获取用户的当前设置出错', e); })
若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人