You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zhishifufei_uniapp/pages/index/getLocation.js

181 lines
5.0 KiB

import permision from '../../libs/permission.js'
// 主函数
export const getLocation = async (callback) => {
return new Promise((resolve, reject) => {
uni.getSystemInfo({
success: res => {
// uni-app 运行平台
let uniPlatform = res.uniPlatform;
// 微信小程序是否开启位置信息按钮
let locationEnabled = res.locationEnabled;
// 微信小程序是否位置信息授权
let locationAuthorized = res.locationAuthorized;
let osName = res.osName;
console.log(uniPlatform, locationEnabled, locationAuthorized);
if (uniPlatform == "app") {
// 获取当前手机是否开启或关闭了定位服务
let isLocationPermision = permision.checkSystemEnableLocation();
if (!isLocationPermision)
callback();
// mShowModel("手机定位服务未开启,请到设置界面开启", '');
if (isLocationPermision) {
let permisionStr = osName == "ios" ? "location" : "android.permission.ACCESS_FINE_LOCATION";
// 验证app是否被授权了获取位置权限
let result = osName == "ios" ? iosPermision(permisionStr) :
androidPermision(permisionStr).then(res => {
if (res == -1)
callback();
// mShowModel("你已拒绝获取位置信息授权,请到权限管理进行授权", "授权");
if (res == 0)
callback();
// mShowModel("应用未获取位置信息授权,请到权限管理进行授权", "授权");
if (res == 1) getCoordinate().then(res => {
resolve(res);
})
})
if (result == true) getCoordinate().then(res => {
resolve(res);
})
}
}
if (uniPlatform == "mp-weixin") weiXinPermision(locationEnabled,
locationAuthorized, callback).then(res => {
console.log(res, 38)
if (res == true) getCoordinate().then(res => {
resolve(res);
})
});
}
})
})
}
const iosPermision = (permisionStr) => {
let result = permision.judgeIosPermission(permisionStr);
if (!result) mShowModel("应用未获取位置信息授权,请到权限管理进行授权", "授权");
return result
}
const androidPermision = async (permisionStr) => {
return new Promise((resolve, reject) => {
let result = permision.requestAndroidPermission(permisionStr);
// if (result == -1) mShowModel("您已拒绝获取位置信息授权,是否授权", "授权");
// if (result == 0) mShowModel("应用未获取位置信息授权,是否授权", "授权");
resolve(result);
})
}
const weiXinPermision = async (locationEnabled, locationAuthorized, callback) => {
return new Promise((resolve, reject) => {
if (locationEnabled == false) {
// mShowModel("手机定位服务未开启,请到设置界面进行开启", '');
callback();
} else {
// 手机定位服务(GPS)已授权
uni.authorize({
scope: "scope.userLocation",
success: res => {
console.log(res, 72);
resolve(true);
},
fail: err => {
uni.showModal({
content: "需要授权位置信息",
confirmText: "确认授权",
success: res => {
if (res.confirm) {
uni.openSetting({success: res => {
if (res.authSetting["scope.userLocation"]) {
mShowToast("授权成功");
resolve(true);
} else {
mShowToast("授权失败,请重新授权")
uni.showModal({
title: "授权",
content: "获取授权失败,是否前往授权设置?",
success: res => {
console.log(res, 91);
if (res.confirm) uni.openSetting();
},
fail: err => {
mShowToast("系统错误")
}
})
}
}
})
} else {
// mShowToast("你拒绝了授权,无法获取位置信息")
callback();
}
}
})
},
// complete: res => {
// console.log(res, 108);
// if (res.errMsg == "authorize:ok") {
// resolve(true);
// } else {
// uni.showModal({
// title: "授权",
// content: "获取授权失败,是否前往授权设置?",
// success: res => {
// if (res.confirm) uni.openSetting();
// },
// fail: err => {
// mShowToast("系统错误!")
// }
// })
// }
// }
})
}
})
}
const getCoordinate = async (options) => {
// 获取坐标
return new Promise((resolve, reject) => {
uni.getLocation({
type: "wgs84",
success: (res) => {
resolve({
lat: res.latitude,
lng: res.longitude
});
}
})
})
}
const mShowModel = (content, confirmText) => {
console.log(content, confirmText)
const params = {};
uni.showModal({
content: content,
showCancel: !!confirmText,
confirmText: confirmText || '取消',
success: res => {
if (res.confirm) {
if (confirmText) {
permision.gotoAppPermissionSetting();
}
} else {
}
},
fail: (e) => {
console.log(e);
},
})
}
const mShowToast = (message, options = {}) => {
uni.showToast({
title: message || "提交成功!",
icon: options.icon || "none"
})
}