From 7efdcfaddfe622c8466610ea24999fd7794535eb Mon Sep 17 00:00:00 2001 From: SwTt29 <2055018491@qq.com> Date: Thu, 25 Dec 2025 09:35:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E4=B8=80=E6=AD=A5=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E4=BF=A1=E6=81=AF=E5=86=99=E5=85=A5=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E7=A1=AE?= =?UTF-8?q?=E4=BF=9DJSON=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/profile/index.js | 10 ++++++-- server-example/server-mysql.js | 46 +++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/pages/profile/index.js b/pages/profile/index.js index 83b8d22..c29ccce 100644 --- a/pages/profile/index.js +++ b/pages/profile/index.js @@ -779,7 +779,7 @@ Page({ // 构造上传的数据,包含authorized_region字段和必要的phoneNumber let userInfo = { - authorized_region: locationData // 直接传递对象,由API层处理JSON序列化 + authorized_region: JSON.stringify(locationData) // 直接在前端转换为JSON字符串,确保数据格式正确 }; // 确保包含phoneNumber字段(服务器接口要求) @@ -817,7 +817,13 @@ Page({ } console.log('位置上传前检查openid:', openid); - console.log('准备上传的位置数据:', userInfo); + 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'); diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index 8093f3b..47f3768 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -1212,20 +1212,41 @@ app.post('/api/user/upload', async (req, res) => { where: { openid: userData.openid } }); + // 详细日志记录收到的位置数据 + console.log('处理位置数据前:', { + authorized_region: userData.authorized_region, + type: typeof userData.authorized_region, + isObject: typeof userData.authorized_region === 'object', + hasLatLng: userData.authorized_region && (userData.authorized_region.latitude || userData.authorized_region.longitude) + }); + if (user) { // 更新用户信息,确保authorized_region字段正确处理 const updateData = { ...userData, updated_at: getBeijingTime() }; - - // 特别处理authorized_region字段,如果是对象则转换为JSON字符串 + + // 特别处理authorized_region字段,只有当它是对象时才转换为JSON字符串 if (updateData.authorized_region && typeof updateData.authorized_region === 'object') { updateData.authorized_region = JSON.stringify(updateData.authorized_region); + } else if (updateData.authorized_region === null || updateData.authorized_region === undefined) { + // 如果是null或undefined,设置为空字符串 + updateData.authorized_region = ''; + } else if (typeof updateData.authorized_region === 'string' && updateData.authorized_region.trim() === '') { + // 如果是空字符串,保持为空字符串 + updateData.authorized_region = ''; + } else if (typeof updateData.authorized_region === 'string') { + // 如果已经是字符串,保持不变 + console.log('authorized_region已经是字符串,无需转换:', updateData.authorized_region); } - + + console.log('更新用户数据:', { authorized_region: updateData.authorized_region }); + await User.update(updateData, { where: { openid: userData.openid } }); user = await User.findOne({ where: { openid: userData.openid } }); + console.log('更新后用户数据:', { authorized_region: user.authorized_region }); + // 使用统一的关联记录创建函数 await createUserAssociations(user); } else { @@ -1236,14 +1257,27 @@ app.post('/api/user/upload', async (req, res) => { created_at: getBeijingTime(), updated_at: getBeijingTime() }; - - // 特别处理authorized_region字段,如果是对象则转换为JSON字符串 + + // 特别处理authorized_region字段,只有当它是对象时才转换为JSON字符串 if (createData.authorized_region && typeof createData.authorized_region === 'object') { createData.authorized_region = JSON.stringify(createData.authorized_region); + } else if (createData.authorized_region === null || createData.authorized_region === undefined) { + // 如果是null或undefined,设置为空字符串 + createData.authorized_region = ''; + } else if (typeof createData.authorized_region === 'string' && createData.authorized_region.trim() === '') { + // 如果是空字符串,保持为空字符串 + createData.authorized_region = ''; + } else if (typeof createData.authorized_region === 'string') { + // 如果已经是字符串,保持不变 + console.log('authorized_region已经是字符串,无需转换:', createData.authorized_region); } - + + console.log('创建用户数据:', { authorized_region: createData.authorized_region }); + user = await User.create(createData); + console.log('创建后用户数据:', { authorized_region: user.authorized_region }); + // 使用统一的关联记录创建函数 await createUserAssociations(user); }