Browse Source

修复位置信息无法写入数据库authorized_region字段的问题

pull/1/head
SwTt29 2 months ago
parent
commit
85371a617b
  1. 2
      pages/profile/index.js
  2. 34
      server-example/server-mysql.js

2
pages/profile/index.js

@ -779,7 +779,7 @@ Page({
// 构造上传的数据,包含authorized_region字段和必要的phoneNumber // 构造上传的数据,包含authorized_region字段和必要的phoneNumber
let userInfo = { let userInfo = {
authorized_region: JSON.stringify(locationData) // 将位置数据转换为JSON字符串存储 authorized_region: locationData // 直接传递对象,由API层处理JSON序列化
}; };
// 确保包含phoneNumber字段(服务器接口要求) // 确保包含phoneNumber字段(服务器接口要求)

34
server-example/server-mysql.js

@ -1213,28 +1213,36 @@ app.post('/api/user/upload', async (req, res) => {
}); });
if (user) { if (user) {
// 更新用户信息 // 更新用户信息,确保authorized_region字段正确处理
await User.update( const updateData = { ...userData, updated_at: getBeijingTime() };
{
...userData, // 特别处理authorized_region字段,如果是对象则转换为JSON字符串
updated_at: getBeijingTime() if (updateData.authorized_region && typeof updateData.authorized_region === 'object') {
}, updateData.authorized_region = JSON.stringify(updateData.authorized_region);
{ }
where: { openid: userData.openid }
} await User.update(updateData, {
); where: { openid: userData.openid }
});
user = await User.findOne({ where: { openid: userData.openid } }); user = await User.findOne({ where: { openid: userData.openid } });
// 使用统一的关联记录创建函数 // 使用统一的关联记录创建函数
await createUserAssociations(user); await createUserAssociations(user);
} else { } else {
// 创建新用户 // 创建新用户,确保authorized_region字段正确处理
user = await User.create({ const createData = {
...userData, ...userData,
notice: 'new', // 创建用户时固定设置notice为new notice: 'new', // 创建用户时固定设置notice为new
created_at: getBeijingTime(), created_at: getBeijingTime(),
updated_at: getBeijingTime() updated_at: getBeijingTime()
}); };
// 特别处理authorized_region字段,如果是对象则转换为JSON字符串
if (createData.authorized_region && typeof createData.authorized_region === 'object') {
createData.authorized_region = JSON.stringify(createData.authorized_region);
}
user = await User.create(createData);
// 使用统一的关联记录创建函数 // 使用统一的关联记录创建函数
await createUserAssociations(user); await createUserAssociations(user);

Loading…
Cancel
Save