/** * 位置管理工具类 * 用于处理位置权限请求和实时位置获取 */ import permission from './voiceCallByTX/TrtcCloud/permission.js' // 平台判断 const isIos = () => { // #ifdef APP-PLUS return plus.os.name === "iOS"; // #endif return false; }; // 位置更新回调函数 let locationUpdateCallback = null; // 位置监听器ID let locationWatcherId = null; /** * 请求位置权限 * @returns {Promise} 是否获取到权限 */ export const requestLocationPermission = async () => { return new Promise(async (resolve) => { // #ifdef APP-PLUS if (isIos()) { // iOS平台 const hasPermission = permission.judgeIosPermission('location'); if (hasPermission) { resolve(true); } else { // iOS需要提示用户手动开启权限 uni.showModal({ title: '提示', content: '应用需要获取位置信息权限,请在设置中开启', confirmText: '去设置', cancelText: '取消', success: (res) => { if (res.confirm) { permission.gotoAppPermissionSetting(); resolve(false); } else { resolve(false); } } }); } } else { // Android平台 const result = await permission.requestAndroidPermission('android.permission.ACCESS_FINE_LOCATION'); if (result === 1) { resolve(true); } else if (result === 0) { // 拒绝本次申请 resolve(false); } else if (result === -1) { // 永久拒绝 uni.showModal({ title: '提示', content: '应用需要获取位置信息权限,请在设置中开启', confirmText: '去设置', cancelText: '取消', success: (res) => { if (res.confirm) { permission.gotoAppPermissionSetting(); resolve(false); } else { resolve(false); } } }); } } // #endif // #ifdef H5 // H5平台使用浏览器原生API请求权限 if (navigator.geolocation) { navigator.permissions.query({ name: 'geolocation' }).then((result) => { if (result.state === 'granted') { resolve(true); } else if (result.state === 'prompt') { // 还未请求过权限,会在getLocation时提示 resolve(true); } else { resolve(false); } }).catch(() => { resolve(false); }); } else { resolve(false); } // #endif // #ifdef MP // 小程序平台 uni.getSetting({ success: (res) => { if (res.authSetting['scope.userLocation']) { resolve(true); } else { uni.authorize({ scope: 'scope.userLocation', success: () => { resolve(true); }, fail: () => { uni.showModal({ title: '提示', content: '应用需要获取位置信息权限,请在设置中开启', confirmText: '去设置', cancelText: '取消', success: (res) => { if (res.confirm) { uni.openSetting(); resolve(false); } else { resolve(false); } } }); } }); } } }); // #endif }); }; /** * 获取当前位置信息 * @returns {Promise} 位置信息对象 */ export const getCurrentLocation = () => { return new Promise((resolve, reject) => { uni.getLocation({ type: 'gcj02', altitude: true, success: (res) => { resolve({ latitude: res.latitude, longitude: res.longitude, altitude: res.altitude, accuracy: res.accuracy, speed: res.speed, verticalAccuracy: res.verticalAccuracy, horizontalAccuracy: res.horizontalAccuracy }); }, fail: (err) => { console.error('获取位置失败:', err); reject(err); } }); }); }; /** * 开始实时位置监听 * @param {Function} callback 位置更新回调函数 * @param {Object} options 监听选项 * @returns {number} 监听器ID */ export const startLocationWatcher = (callback, options = {}) => { if (locationWatcherId) { stopLocationWatcher(); } locationUpdateCallback = callback; locationWatcherId = uni.startLocationUpdate({ success: () => { console.log('开始位置监听成功'); }, fail: (err) => { console.error('开始位置监听失败:', err); // 如果是因为权限问题,重新请求权限 if (err.errCode === 12002) { requestLocationPermission(); } } }); // 监听位置更新 uni.onLocationChange((res) => { const locationInfo = { latitude: res.latitude, longitude: res.longitude, altitude: res.altitude, accuracy: res.accuracy, speed: res.speed, verticalAccuracy: res.verticalAccuracy, horizontalAccuracy: res.horizontalAccuracy, timestamp: res.timestamp }; if (locationUpdateCallback) { locationUpdateCallback(locationInfo); } }); return locationWatcherId; }; /** * 停止位置监听 */ export const stopLocationWatcher = () => { if (locationWatcherId) { uni.stopLocationUpdate({ success: () => { console.log('停止位置监听成功'); }, fail: (err) => { console.error('停止位置监听失败:', err); } }); locationWatcherId = null; } // 移除位置更新监听 uni.offLocationChange(); locationUpdateCallback = null; }; /** * 检查系统定位服务是否开启 * @returns {Promise} 是否开启 */ export const checkSystemLocationEnabled = async () => { return new Promise((resolve) => { // #ifdef APP-PLUS const enabled = permission.checkSystemEnableLocation(); resolve(enabled); // #endif // #ifdef H5 resolve(navigator.geolocation ? true : false); // #endif // #ifdef MP uni.getSystemInfo({ success: (res) => { // 小程序无法直接检查系统定位服务是否开启 resolve(true); }, fail: () => { resolve(false); } }); // #endif }); }; export default { requestLocationPermission, getCurrentLocation, startLocationWatcher, stopLocationWatcher, checkSystemLocationEnabled };