- 檢查授權(quán)情況
- false 需要打開授權(quán)頁面
- 這個授權(quán)頁面需要點擊事件才能觸發(fā)载慈,一般用小程序的 modal 確定事件代替,也可以自己寫一個button觸發(fā)珍手。
下面是定位授權(quán)的判斷办铡,wepy 例子
// 判斷授權(quán)
getSetting () {
let that = this;
wx.getSetting({
success (res) {
console.log('setting-suc', res.authSetting);
// 已授權(quán)或者未請求過授權(quán)
if (res.authSetting['scope.userLocation'] || res.authSetting['scope.userLocation'] === undefined) {
that.getLocation();
} else if (!res.authSetting['scope.userLocation']) {
// 拒絕授權(quán),showModal 為了觸發(fā)點擊事件琳要,里面的title,content自定義
wx.showModal({
title: '小程序授權(quán)',
content: '請授權(quán)定位功能寡具,否則無法順利體驗小程序功能',
success: res => {
if (res.confirm) {
that.openSetting();
}
}
});
}
}
});
}
// 打開授權(quán)
openSetting () {
wx.openSetting({
success (res) {
console.log('open-suc', res.authSetting);
},
fail (err) {
console.log('open-err', err);
}
});
}
// 獲取位置
getLocation () {
let that = this;
wx.getLocation({
type: 'wgs84',
success: function(res) {
console.log('location-suc', res);
that.latitude = res.latitude;
that.longitude = res.longitude;
that.$apply();
that.getCityList();
},
fail: function (err) {
console.log('location-err', err);
// 有些手機定位功能無法使用,例如華為手機稚补,需要打開手機定位童叠,據(jù)說是安全策略的設(shè)置。
if (err.errMsg.indexOf('ERROR_SERVER_NOT_LOCATION') > -1) {
wx.showToast({
title: '請打開手機定位功能课幕,下拉頁面重新獲取數(shù)據(jù)',
icon: 'none'
});
} else if (err.errMsg.indexOf('auth deny') > -1) {
wx.showToast({
title: '請打開授權(quán)定位功能厦坛,否則無法順利使用小程序',
icon: 'none'
});
}
}
});
}