gps.js 3.6 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// #ifdef APP-PLUS
import permision from "./wa-permission/permission.js"
// #endif
class Gps {
	constructor(arg) {
		this.lock = false //锁防止重复请求
	}
	async getLocation(param = {
		type: 'wgs84'
	}) {
		return new Promise(async (callback) => {
			if (this.lock) {
				// console.log('已锁,已有另一个请求正在执行。无需重复请求');
				callback(false);
				return false
			}
			this.lock = true //加锁防止重复的请求
			uni.getLocation({
				...param,
				success: res => {
					this.lock = false //成功后解锁
					//console.log(res);
					callback(res)
				},
				fail: async (err) => {
					uni.showToast({
						title: '定位获取失败',
						icon: 'none'
					});
雪洛's avatar
雪洛 已提交
30 31
					console.error(err)
					callback(false)
DCloud_JSON's avatar
DCloud_JSON 已提交
32 33 34
					
					// #ifdef APP-PLUS
					await this.checkGpsIsOpen()
雪洛's avatar
雪洛 已提交
35 36
					// #endif
					
DCloud_JSON's avatar
DCloud_JSON 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
					
					// #ifdef MP-WEIXIN
					if (err.errMsg == 'getLocation:fail auth deny') {
						uni.showModal({
							content: '应用无定位权限',
							confirmText: '前往设置',
							complete: (e) => {
								if (e.confirm) {
									uni.openSetting({
										success(res) {
											console.log(res.authSetting)
										}
									});
雪洛's avatar
雪洛 已提交
50
								}
DCloud_JSON's avatar
DCloud_JSON 已提交
51 52 53
								this.lock = false //解锁让回到界面重新获取
							}
						});
雪洛's avatar
雪洛 已提交
54 55 56 57 58 59 60 61
					}
					if (err.errMsg == 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') {
						uni.showModal({
							content: '未开启定位权限,请前往手机系统设置中开启',
							showCancel: false,
							confirmText:"知道了"
						});
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
62 63 64 65
					// #endif
				}
			});
		})
雪洛's avatar
雪洛 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	}
	// #ifdef APP-PLUS
	async checkGpsIsOpen() {
		this.lock = true //加锁防止重复的请求
		console.log('检查定位设置开启问题', permision.checkSystemEnableLocation());
		if (!permision.checkSystemEnableLocation()) {
			plus.nativeUI.confirm("手机定位权限没有开启,是否去开启?", (e) => {
				this.lock = false
				if (e.index == 0) {
					if (uni.getSystemInfoSync().platform == "ios") {
						plus.runtime.openURL("app-settings://");
					} else {
						var main = plus.android.runtimeMainActivity(); //获取activity
						var Intent = plus.android.importClass('android.content.Intent');
						var Settings = plus.android.importClass('android.provider.Settings');
						var intent = new Intent(Settings
						.ACTION_LOCATION_SOURCE_SETTINGS); //可设置表中所有Action字段  
						main.startActivity(intent);
					}
				} else {
					uni.showToast({
						title: '设备无定位权限',
						icon: 'none'
					});
					callback(false)
				}
			}, {
				"buttons": ["去设置", "不开启"],
				"verticalAlign": "center"
			});
			return false
		}
		let checkAppGpsRes = await this.checkAppGps()
		console.log(checkAppGpsRes, 'checkAppGpsRes');
		if (!checkAppGpsRes) {
			plus.nativeUI.confirm("应用定位权限没有开启,是否去开启?", (e) => {
				this.lock = false
				if (e.index == 0) {
					permision.gotoAppPermissionSetting()
					callback(false)
				} else {
					uni.showToast({
						title: '应用无定位权限',
						icon: 'none'
					});
					return false
				}
			}, {
				"buttons": ["去设置", "不开启"],
				"verticalAlign": "center"
			});
		} else {
			this.lock = false
		}
	}
	async checkAppGps() {
		if (uni.getSystemInfoSync().platform == "ios" && !permision.judgeIosPermission("location")) {
			return false
		}
		if (uni.getSystemInfoSync().platform != "ios" && await permision.requestAndroidPermission(
				"android.permission.ACCESS_FINE_LOCATION") != 1) {
			return false
		}
		return true
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
131 132
	// #endif
}
雪洛's avatar
雪洛 已提交
133
export default Gps