1. 获取用户授权信息
<button open-type='getUserInfo' bindgetuserinfo="getUserInfo">获取用户授权</button>
当前未授权的状态下每次点击都会调起授权弹窗,getUserInfo函数为弹窗点击的回调
2. 获取地理位置信息
<button open-type='openSetting' bindopensetting="getOpenSetting">获取地理位置授权</button>
当前未授权的状态下每次点击都会调起授权弹窗,getOpenSetting函数为用户进入设置页面后的回调,地理位置已授权才能获取经纬度信息。
3. JS文件
//获取用户授权
getUserInfo:function(e){
wx.getSetting({
success: (res) => {
console.log('是否授权', res.authSetting["scope.userInfo"])
if (res.authSetting["scope.userInfo"]) {
console.log('用户信息',e.detail.userInfo)
}
}
})
},
//获取地理位置授权
getOpenSetting(e) {
if (e.type === "opensetting") {
//地理位置授权
if (e.detail.authSetting["scope.userLocation"]) {
//获取经纬度信息 注:wx.getLocation只能调起一次用户授权,拒绝后不会再次调用
wx.getLocation({
type: "wgs84",
success: function (res) {
that.setData({
latitude: res.latitude,
longitude: res.longitude
});
},
fail: function (res) {
console.log(res, "地理位置失败");
}
});
} else {
wx.showToast({
title: "授权失败",
icon: "none",
duration: 1000
});
}
}
}
wx.getsetting 可获取当前授权状态
wx.getSetting({
success (res) {
console.log(res.authSetting)
res.authSetting = {
"scope.userInfo": true,//用户信息表示已授权
"scope.userLocation": true//地理位置已授权
}
}
})