// 显示隐私协议showPrivacy() { uni.navigateTo({ url: '/pages/agreement/privacy' });},// 获取当前位置async getCurrentLocation() { if (!this.agreePrivacy) { uni.showToast({ title: '请先阅读并同意隐私协议', icon: 'none' }); return; } try { // 检查权限 const settingRes = await uni.getSetting(); if (!settingRes.authSetting['scope.userLocation']) { // 申请权限 try { await uni.authorize({ scope: 'scope.userLocation' }); } catch (authError) { // 授权失败,引导用户去设置页面 this.guideUserToOpenPermission(); return; } } // 获取位置 uni.showLoading({ title: '定位中...' }); uni.getLocation({ type: 'wgs84', altitude: true, success: (res) => { uni.hideLoading(); this.locationResult = res; uni.showToast({ title: '定位成功', icon: 'success' }); }, fail: (err) => { uni.hideLoading(); this.handleLocationError(err); } }); } catch (error) { uni.hideLoading(); console.log('获取位置失败', error); uni.showToast({ title: '获取位置失败,请稍后重试', icon: 'none' }); }},// 选择位置async chooseLocation() { if (!this.agreePrivacy) { uni.showToast({ title: '请先阅读并同意隐私协议', icon: 'none' }); return; } try { // 检查权限 const settingRes = await uni.getSetting(); if (!settingRes.authSetting['scope.userLocation']) { // 申请权限 try { await uni.authorize({ scope: 'scope.userLocation' }); } catch (authError) { // 授权失败,引导用户去设置页面 this.guideUserToOpenPermission(); return; } } // 选择位置 uni.chooseLocation({ success: (res) => { this.locationResult = res; uni.showToast({ title: '选择位置成功', icon: 'success' }); }, fail: (err) => { console.log('选择位置失败', err); uni.showToast({ title: '选择位置失败', icon: 'none' }); } }); } catch (error) { console.log('选择位置失败', error); uni.showToast({ title: '选择位置失败,请稍后重试', icon: 'none' }); }},// 引导用户开启权限guideUserToOpenPermission() { uni.showModal({ title: '权限提示', content: '需要获取您的地理位置信息才能使用此功能,是否前往设置页面开启权限?', confirmText: '去设置', cancelText: '取消', success: (res) => { if (res.confirm) { uni.openSetting({ success: (res) => { console.log('设置页面操作结果', res.authSetting); if (res.authSetting['scope.userLocation']) { uni.showToast({ title: '权限开启成功', icon: 'success' }); } } }); } } });},// 处理定位错误handleLocationError(err) { console.log('定位错误', err); switch (err.errCode) { case 1: case 12: // 用户拒绝授权 this.guideUserToOpenPermission(); break; case 2: // 位置信息不可用 uni.showToast({ title: '无法获取位置信息,请检查定位服务是否开启', icon: 'none' }); break; case 3: // 定位超时 uni.showToast({ title: '定位超时,请检查网络连接', icon: 'none' }); break; case 13: // 系统拒绝授权 uni.showToast({ title: '系统拒绝授权,请检查系统定位权限设置', icon: 'none' }); break; default: // 其他错误 uni.showToast({ title: '定位失败,请稍后重试', icon: 'none' }); break; }}