From 95a88200d304b58d54789de3a8cfed7f364e34e4 Mon Sep 17 00:00:00 2001 From: SwTt29 <2055018491@qq.com> Date: Thu, 25 Dec 2025 09:40:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E4=B8=93=E9=97=A8=E7=9A=84?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E6=9B=B4=E6=96=B0API=E7=AB=AF=E7=82=B9?= =?UTF-8?q?=EF=BC=8C=E8=A7=A3=E5=86=B3=E4=BD=8D=E7=BD=AE=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E5=86=99=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/profile/index.js | 48 ++++++++++++++++++++-------------- server-example/server-mysql.js | 44 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 20 deletions(-) diff --git a/pages/profile/index.js b/pages/profile/index.js index c29ccce..1c2e3c1 100644 --- a/pages/profile/index.js +++ b/pages/profile/index.js @@ -779,7 +779,7 @@ Page({ // 构造上传的数据,包含authorized_region字段和必要的phoneNumber let userInfo = { - authorized_region: JSON.stringify(locationData) // 直接在前端转换为JSON字符串,确保数据格式正确 + authorized_region: locationData // 直接传递对象,由API层处理序列化 }; // 确保包含phoneNumber字段(服务器接口要求) @@ -827,26 +827,34 @@ Page({ // 调用API上传位置数据 const api = require('../../utils/api.js'); - api.uploadUserInfo({ - ...userInfo, - openid: openid // 明确传递openid - }).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端点 + 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 + }); }); - }); }, fail() { wx.hideLoading(); diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index 47f3768..46f488d 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -1185,6 +1185,50 @@ async function createUserAssociations(user) { // API路由 +// 专门用于更新用户位置信息的API端点 +app.post('/api/user/update-location', async (req, res) => { + try { + const { openid, latitude, longitude } = req.body; + + if (!openid) { + return res.json({ success: false, message: '缺少openid' }); + } + + if (!latitude || !longitude) { + return res.json({ success: false, message: '缺少位置信息' }); + } + + const locationData = { latitude, longitude }; + const locationJson = JSON.stringify(locationData); + + console.log('更新用户位置信息:', { openid, locationJson }); + + const updateResult = await User.update( + { authorized_region: locationJson, updated_at: getBeijingTime() }, + { where: { openid } } + ); + + console.log('位置更新结果:', updateResult); + + if (updateResult[0] > 0) { + // 查询更新后的用户数据 + const updatedUser = await User.findOne({ where: { openid }, attributes: ['authorized_region'] }); + console.log('更新后的位置信息:', updatedUser.authorized_region); + + return res.json({ + success: true, + message: '位置信息更新成功', + data: { authorized_region: updatedUser.authorized_region } + }); + } else { + return res.json({ success: false, message: '用户不存在或位置信息未更新' }); + } + } catch (error) { + console.error('更新位置信息失败:', error); + res.json({ success: false, message: '更新位置信息失败', error: error.message }); + } +}); + // 上传用户信息 app.post('/api/user/upload', async (req, res) => { try {