From 163d33f3d78c1e04e87a79169dbf3eb24b521726 Mon Sep 17 00:00:00 2001 From: SwTt29 <2055018491@qq.com> Date: Thu, 25 Dec 2025 10:12:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9authorized=5Fregion=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=AD=98=E5=82=A8=E5=AE=9E=E9=99=85=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E8=80=8C=E9=9D=9E=E7=BB=8F=E7=BA=AC=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/profile/index.js | 156 +++++++-------------------------- server-example/server-mysql.js | 9 +- 2 files changed, 40 insertions(+), 125 deletions(-) diff --git a/pages/profile/index.js b/pages/profile/index.js index ce4b0d8..9ca2fdb 100644 --- a/pages/profile/index.js +++ b/pages/profile/index.js @@ -11,13 +11,11 @@ Page({ onLoad() { this.loadUserInfo(); - this.loadLocationInfo(); this.checkLocationAuth(); }, onShow() { this.loadUserInfo(); - this.loadLocationInfo(); this.checkLocationAuth(); // 更新自定义tabBar状态 if (typeof this.getTabBar === 'function' && this.getTabBar()) { @@ -30,27 +28,6 @@ Page({ app.updateCurrentTab('profile'); }, - // 加载位置信息 - loadLocationInfo() { - // 首先从本地存储获取保存的地址信息 - const savedAddress = wx.getStorageSync('locationInfo'); - if (savedAddress) { - this.setData({ locationInfo: savedAddress }); - console.log('从本地存储加载地址信息:', savedAddress); - return; - } - - // 如果本地没有,检查是否有经纬度信息可以进行逆地理编码 - const savedLocation = wx.getStorageSync('userLocation'); - if (savedLocation && savedLocation.latitude && savedLocation.longitude) { - console.log('从本地存储加载经纬度信息:', savedLocation); - this.reverseGeocode(savedLocation.latitude, savedLocation.longitude); - return; - } - - console.log('没有保存的位置信息'); - }, - // 加载用户信息 loadUserInfo() { console.log('开始加载用户信息') @@ -788,38 +765,9 @@ Page({ success(res) { const latitude = res.latitude; const longitude = res.longitude; - // 调用逆地理编码API获取详细地址 - that.reverseGeocode(latitude, longitude); - // 将位置信息存储到本地 wx.setStorageSync('userLocation', { latitude, longitude }); - // 立即将位置数据上传到数据库users表的authorized_region字段 - const locationData = { - latitude: latitude, - longitude: longitude - }; - - // 构造上传的数据,包含authorized_region字段和必要的phoneNumber - let userInfo = { - authorized_region: locationData // 直接传递对象,由API层处理序列化 - }; - - // 确保包含phoneNumber字段(服务器接口要求) - let phoneNumber = wx.getStorageSync('phoneNumber'); - if (!phoneNumber) { - // 尝试从用户信息中获取phoneNumber - const globalUserInfo = wx.getStorageSync('userInfo'); - phoneNumber = globalUserInfo?.phoneNumber; - } - - // 如果找到phoneNumber,添加到上传数据中 - if (phoneNumber) { - userInfo.phoneNumber = phoneNumber; - } else { - console.warn('位置上传警告: 未找到phoneNumber,可能导致上传失败'); - } - // 检查openid是否存在 let openid = wx.getStorageSync('openid'); if (!openid) { @@ -831,6 +779,7 @@ Page({ // 确保openid存在 if (!openid) { console.error('位置上传失败: 未找到openid'); + wx.hideLoading(); wx.showToast({ title: '位置上传失败,请先登录', icon: 'none', @@ -839,45 +788,8 @@ Page({ return; } - console.log('位置上传前检查openid:', openid); - console.log('位置数据对象:', locationData); - console.log('位置数据类型:', typeof locationData); - console.log('是否有经纬度:', locationData.latitude && locationData.longitude); - console.log('准备上传的完整数据:', { - ...userInfo, - openid: openid - }); - - // 调用API上传位置数据 - const api = require('../../utils/api.js'); - - // 使用专门的位置更新API端点 - const locationUpdateData = { - openid: openid, - latitude: latitude, - longitude: longitude - }; - - console.log('调用位置更新API:', locationUpdateData); - - api.request('/api/user/update-location', 'POST', locationUpdateData) - .then(res => { - console.log('位置数据上传成功:', res); - // 显示上传成功提示 - wx.showToast({ - title: '位置更新成功', - icon: 'success', - duration: 1500 - }); - }).catch(err => { - console.error('位置数据上传失败:', err); - // 显示上传失败提示 - wx.showToast({ - title: '位置上传失败', - icon: 'none', - duration: 2000 - }); - }); + // 调用逆地理编码API获取详细地址,然后统一上传 + that.reverseGeocode(latitude, longitude, openid); }, fail() { wx.hideLoading(); @@ -895,8 +807,8 @@ Page({ }); }, - // 逆地理编码获取详细地址 - reverseGeocode(latitude, longitude) { + // 逆地理编码获取详细地址并上传到数据库 + reverseGeocode(latitude, longitude, openid) { const that = this; wx.request({ url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77`, @@ -909,7 +821,35 @@ Page({ wx.setStorageSync('locationInfo', address); // 保存地址信息到数据库 - that.saveAddressToDatabase(latitude, longitude, address); + const api = require('../../utils/api.js'); + const locationUpdateData = { + openid: openid, + latitude: latitude, + longitude: longitude, + address: address + }; + + console.log('保存地址到数据库:', locationUpdateData); + + // 调用API保存地址信息 + api.request('/api/user/update-location', 'POST', locationUpdateData) + .then(res => { + console.log('地址保存成功:', res); + // 显示上传成功提示 + wx.showToast({ + title: '位置更新成功', + icon: 'success', + duration: 1500 + }); + }).catch(err => { + console.error('地址保存失败:', err); + // 显示上传失败提示 + wx.showToast({ + title: '位置上传失败', + icon: 'none', + duration: 2000 + }); + }); } else { wx.showToast({ title: '获取地址失败', @@ -927,34 +867,6 @@ Page({ }); }, - // 将地址信息保存到数据库 - saveAddressToDatabase(latitude, longitude, address) { - const api = require('../../utils/api.js'); - const openid = wx.getStorageSync('openid'); - - if (!openid) { - console.error('保存地址失败:用户未登录'); - return; - } - - const addressData = { - openid: openid, - latitude: latitude, - longitude: longitude, - address: address - }; - - console.log('保存地址到数据库:', addressData); - - // 调用API保存地址信息 - api.request('/api/user/update-location', 'POST', addressData) - .then(res => { - console.log('地址保存成功:', res); - }).catch(err => { - console.error('地址保存失败:', err); - }); - }, - // 更新用户名称 updateName(newName) { return new Promise((resolve, reject) => { diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index 20c6aae..d92c922 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -1205,13 +1205,16 @@ app.post('/api/user/update-location', async (req, res) => { // 准备要更新的数据 const updateData = { - authorized_region: locationJson, updated_at: getBeijingTime() }; - // 如果有地址信息,保存到detailedaddress字段 + // 如果有地址信息,将地址存储到authorized_region字段,满足需求 if (address) { - updateData.detailedaddress = address; + updateData.authorized_region = address; + updateData.detailedaddress = address; // 同时保持detailedaddress字段也存储地址,确保兼容性 + } else { + // 如果没有地址信息,才存储经纬度 + updateData.authorized_region = locationJson; } const updateResult = await User.update(