222 lines
6.2 KiB
JavaScript
222 lines
6.2 KiB
JavaScript
|
|
/**
|
|||
|
|
* 定位服务检测工具
|
|||
|
|
* 功能:检测用户是否开启定位权限/服务
|
|||
|
|
* 支持平台:APP(Android/iOS)、微信小程序、H5
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
export default {
|
|||
|
|
/**
|
|||
|
|
* 检查定位服务是否可用
|
|||
|
|
* @returns {Promise<boolean>} 返回Promise,true表示定位可用
|
|||
|
|
*/
|
|||
|
|
isLocationEnabled() {
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
// APP端处理
|
|||
|
|
// #ifdef APP-PLUS
|
|||
|
|
this._checkAppLocation().then(resolve).catch(reject);
|
|||
|
|
// #endif
|
|||
|
|
|
|||
|
|
// 微信小程序处理
|
|||
|
|
// #ifdef MP-WEIXIN
|
|||
|
|
this._checkWxLocation().then(resolve).catch(reject);
|
|||
|
|
// #endif
|
|||
|
|
|
|||
|
|
// H5处理
|
|||
|
|
// #ifdef H5
|
|||
|
|
this._checkH5Location().then(resolve).catch(reject);
|
|||
|
|
// resolve(false);
|
|||
|
|
// #endif
|
|||
|
|
|
|||
|
|
// 默认处理(非上述平台)
|
|||
|
|
// #ifndef APP-PLUS || MP-WEIXIN || H5
|
|||
|
|
resolve(false);
|
|||
|
|
console.warn('当前平台不支持定位检测');
|
|||
|
|
// #endif
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* APP端检测定位
|
|||
|
|
* @private
|
|||
|
|
*/
|
|||
|
|
_checkAppLocation() {
|
|||
|
|
return new Promise((resolve) => {
|
|||
|
|
plus.geolocation.getCurrentPosition(
|
|||
|
|
() => resolve(true),
|
|||
|
|
(err) => {
|
|||
|
|
// 错误码参考:https://www.html5plus.org/doc/zh_cn/geolocation.html#plus.geolocation.GeolocationError
|
|||
|
|
console.log(err,"---------------------")
|
|||
|
|
if (err.code === 2 || err.code === 12) {
|
|||
|
|
resolve(false); // 权限被拒绝或定位服务未开启
|
|||
|
|
}else {
|
|||
|
|
resolve(false); // 其他错误也视为不可用
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
{ provider: 'system', enableHighAccuracy: false }
|
|||
|
|
);
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 微信小程序检测定位
|
|||
|
|
* @private
|
|||
|
|
*/
|
|||
|
|
_checkWxLocation() {
|
|||
|
|
return new Promise((resolve) => {
|
|||
|
|
wx.getSetting({
|
|||
|
|
success(res) {
|
|||
|
|
const authSetting = res.authSetting || {};
|
|||
|
|
// undefined表示尚未询问过权限,也视为可用
|
|||
|
|
resolve(authSetting['scope.userLocation'] !== false);
|
|||
|
|
},
|
|||
|
|
fail() {
|
|||
|
|
resolve(false);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* H5端检测定位
|
|||
|
|
* @private
|
|||
|
|
*/
|
|||
|
|
_checkH5Location() {
|
|||
|
|
return new Promise((resolve) => {
|
|||
|
|
// 现代浏览器 Permissions API
|
|||
|
|
if (navigator.permissions && navigator.permissions.query) {
|
|||
|
|
navigator.permissions.query({ name: 'geolocation' })
|
|||
|
|
.then(permissionStatus => {
|
|||
|
|
resolve(permissionStatus.state !== 'denied');
|
|||
|
|
})
|
|||
|
|
.catch(() => {
|
|||
|
|
resolve('geolocation' in navigator);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
// 传统浏览器检测
|
|||
|
|
else {
|
|||
|
|
resolve('geolocation' in navigator);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 请求定位权限
|
|||
|
|
* @returns {Promise<boolean>} 返回是否成功获取权限
|
|||
|
|
*/
|
|||
|
|
requestLocationPermission() {
|
|||
|
|
return new Promise((resolve) => {
|
|||
|
|
uni.authorize({
|
|||
|
|
scope: 'scope.userLocation',
|
|||
|
|
success: () => resolve(true),
|
|||
|
|
fail: () => resolve(false)
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 打开系统定位设置页面
|
|||
|
|
*/
|
|||
|
|
openSystemLocationSetting() {
|
|||
|
|
// APP端
|
|||
|
|
// #ifdef APP-PLUS
|
|||
|
|
if (plus.os.name === 'Android') {
|
|||
|
|
const Intent = plus.android.importClass('android.content.Intent');
|
|||
|
|
const Settings = plus.android.importClass('android.provider.Settings');
|
|||
|
|
const main = plus.android.runtimeMainActivity();
|
|||
|
|
const intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
|||
|
|
main.startActivity(intent);
|
|||
|
|
} else if (plus.os.name === 'iOS') {
|
|||
|
|
const UIApplication = plus.ios.importClass('UIApplication');
|
|||
|
|
const NSURL = plus.ios.importClass('NSURL');
|
|||
|
|
const url = NSURL.URLWithString('App-Prefs:root=Privacy&path=LOCATION');
|
|||
|
|
const sharedApplication = UIApplication.sharedApplication();
|
|||
|
|
plus.ios.invoke(sharedApplication, 'openURL:', url);
|
|||
|
|
}
|
|||
|
|
// #endif
|
|||
|
|
|
|||
|
|
// 微信小程序
|
|||
|
|
// #ifdef MP-WEIXIN
|
|||
|
|
wx.openSetting();
|
|||
|
|
// #endif
|
|||
|
|
|
|||
|
|
// H5无法直接打开系统设置
|
|||
|
|
// #ifdef H5
|
|||
|
|
console.warn('H5端无法直接打开系统定位设置');
|
|||
|
|
// #endif
|
|||
|
|
},
|
|||
|
|
/**
|
|||
|
|
* 跳转到当前应用的系统设置页面
|
|||
|
|
*/
|
|||
|
|
openAppSettings() {
|
|||
|
|
// APP端处理
|
|||
|
|
// #ifdef APP-PLUS
|
|||
|
|
if (plus.os.name === 'Android') {
|
|||
|
|
const Intent = plus.android.importClass('android.content.Intent');
|
|||
|
|
const Settings = plus.android.importClass('android.provider.Settings');
|
|||
|
|
const Uri = plus.android.importClass('android.net.Uri');
|
|||
|
|
const main = plus.android.runtimeMainActivity();
|
|||
|
|
|
|||
|
|
const intent = new Intent();
|
|||
|
|
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
|||
|
|
const uri = Uri.fromParts("package", main.getPackageName(), null);
|
|||
|
|
intent.setData(uri);
|
|||
|
|
main.startActivity(intent);
|
|||
|
|
}
|
|||
|
|
else if (plus.os.name === 'iOS') {
|
|||
|
|
var app = plus.ios.invoke('UIApplication', 'sharedApplication');
|
|||
|
|
var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
|
|||
|
|
plus.ios.invoke(app, 'openURL:options:completionHandler:', setting);
|
|||
|
|
plus.ios.deleteObject(setting);
|
|||
|
|
plus.ios.deleteObject(app);
|
|||
|
|
}
|
|||
|
|
// #endif
|
|||
|
|
|
|||
|
|
// 微信小程序处理
|
|||
|
|
// #ifdef MP-WEIXIN
|
|||
|
|
wx.openSetting({
|
|||
|
|
success(res) {
|
|||
|
|
console.log('已打开小程序设置页面');
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
// #endif
|
|||
|
|
|
|||
|
|
// H5处理
|
|||
|
|
// #ifdef H5
|
|||
|
|
console.warn('H5端无法直接打开应用设置');
|
|||
|
|
alert('请在浏览器设置中修改权限');
|
|||
|
|
// #endif
|
|||
|
|
},
|
|||
|
|
/**
|
|||
|
|
* 获取当前位置(封装了权限检测)
|
|||
|
|
* @param {Object} options 同uni.getLocation参数
|
|||
|
|
* @returns {Promise<Object>} 返回位置信息
|
|||
|
|
*/
|
|||
|
|
async getCurrentPosition(options = {}) {
|
|||
|
|
const isEnabled = await this.isLocationEnabled();
|
|||
|
|
if (!isEnabled) {
|
|||
|
|
// uni.showModal({
|
|||
|
|
// title: '提示',
|
|||
|
|
// content: '您的系统位置权限未开启,无法为您提供服务。',
|
|||
|
|
// success: (res)=> {
|
|||
|
|
// if (res.confirm) {
|
|||
|
|
// this.openSystemLocationSetting()
|
|||
|
|
// } else if (res.cancel) {
|
|||
|
|
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// })
|
|||
|
|
return isEnabled;
|
|||
|
|
throw new Error('定位服务未开启或没有权限');
|
|||
|
|
}
|
|||
|
|
return isEnabled;
|
|||
|
|
// return new Promise((resolve, reject) => {
|
|||
|
|
// uni.getLocation({
|
|||
|
|
// type: 'gcj02',
|
|||
|
|
// geocode: true,
|
|||
|
|
// ...options,
|
|||
|
|
// success: resolve,
|
|||
|
|
// fail: reject
|
|||
|
|
// });
|
|||
|
|
// });
|
|||
|
|
}
|
|||
|
|
};
|