From d9ce767351b80f297cc604197bc93050cb5aba6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Wed, 17 Dec 2025 17:36:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BD=8D=E7=BD=AE=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E5=8A=9F=E8=83=BD=E5=88=B0=E6=88=91=E7=9A=84=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.json | 7 ++- pages/profile/index.js | 116 ++++++++++++++++++++++++++++++++++++++- pages/profile/index.wxml | 17 ++++++ 3 files changed, 136 insertions(+), 4 deletions(-) diff --git a/app.json b/app.json index 13a2ed2..17d41a7 100644 --- a/app.json +++ b/app.json @@ -84,5 +84,10 @@ ] }, "style": "v2", - "sitemapLocation": "sitemap.json" + "sitemapLocation": "sitemap.json", + "permission": { + "scope.userLocation": { + "desc": "您的位置信息将用于提供本地化服务" + } + } } \ No newline at end of file diff --git a/pages/profile/index.js b/pages/profile/index.js index 11162c0..294ad2c 100644 --- a/pages/profile/index.js +++ b/pages/profile/index.js @@ -4,15 +4,19 @@ Page({ userInfo: {}, userType: '', userTags: [], - needPhoneAuth: false // 是否需要重新授权手机号 + needPhoneAuth: false, // 是否需要重新授权手机号 + locationInfo: '', // 位置信息 + hasLocationAuth: false // 是否已经授权位置 }, onLoad() { - this.loadUserInfo() + this.loadUserInfo(); + this.checkLocationAuth(); }, onShow() { - this.loadUserInfo() + this.loadUserInfo(); + this.checkLocationAuth(); // 更新自定义tabBar状态 if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ @@ -777,6 +781,112 @@ Page({ }); }, + // 检查位置授权状态 + checkLocationAuth() { + const that = this; + wx.getSetting({ + success(res) { + if (res.authSetting['scope.userLocation']) { + // 用户已经授权位置 + that.setData({ hasLocationAuth: true }); + that.getUserLocation(); + } else { + // 用户未授权位置 + that.setData({ hasLocationAuth: false }); + } + } + }); + }, + + // 请求位置授权 + requestLocationAuth() { + const that = this; + wx.authorize({ + scope: 'scope.userLocation', + success() { + // 授权成功 + that.setData({ hasLocationAuth: true }); + that.getUserLocation(); + }, + fail() { + // 授权失败 + wx.showToast({ + title: '您已拒绝位置授权', + icon: 'none' + }); + that.setData({ hasLocationAuth: false }); + // 引导用户打开设置页面 + wx.showModal({ + title: '位置授权', + content: '需要您的位置信息,请前往设置开启', + success(res) { + if (res.confirm) { + wx.openSetting({ + success(settingRes) { + if (settingRes.authSetting['scope.userLocation']) { + that.setData({ hasLocationAuth: true }); + that.getUserLocation(); + } + } + }); + } + } + }); + } + }); + }, + + // 获取用户当前位置 + getUserLocation() { + const that = this; + wx.showLoading({ title: '获取位置中...' }); + wx.getLocation({ + type: 'gcj02', + success(res) { + const latitude = res.latitude; + const longitude = res.longitude; + // 调用逆地理编码API获取详细地址 + that.reverseGeocode(latitude, longitude); + }, + fail() { + wx.hideLoading(); + wx.showToast({ + title: '获取位置失败', + icon: 'none' + }); + } + }); + }, + + // 逆地理编码获取详细地址 + reverseGeocode(latitude, longitude) { + const that = this; + wx.request({ + url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77`, + success(res) { + wx.hideLoading(); + if (res.data.status === 0) { + const address = res.data.result.address; + that.setData({ locationInfo: address }); + // 可以将位置信息保存到本地存储 + wx.setStorageSync('locationInfo', address); + } else { + wx.showToast({ + title: '获取地址失败', + icon: 'none' + }); + } + }, + fail() { + wx.hideLoading(); + wx.showToast({ + title: '获取地址失败', + icon: 'none' + }); + } + }); + }, + // 更新用户名称 updateName(newName) { return new Promise((resolve, reject) => { diff --git a/pages/profile/index.wxml b/pages/profile/index.wxml index 275ca05..1e180db 100644 --- a/pages/profile/index.wxml +++ b/pages/profile/index.wxml @@ -37,6 +37,23 @@ + + + 位置信息 + + + 当前位置:{{locationInfo || '获取中...'}} + + + + + 身份管理