Browse Source

修复客服列表网络请求和位置信息写入数据库问题

pull/1/head
SwTt29 2 months ago
parent
commit
e0618ebe3b
  1. BIN
      images/轮播图1.jpg
  2. BIN
      images/轮播图2.jpg
  3. BIN
      images/轮播图3.jpg
  4. 58
      pages/profile/index.js
  5. 40
      server-example/server-mysql.js

BIN
images/轮播图1.jpg

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

BIN
images/轮播图2.jpg

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.8 MiB

BIN
images/轮播图3.jpg

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

58
pages/profile/index.js

@ -777,17 +777,69 @@ Page({
longitude: longitude longitude: longitude
}; };
// 构造上传的数据,包含authorized_region字段 // 构造上传的数据,包含authorized_region字段和必要的phoneNumber
const userInfo = { let userInfo = {
authorized_region: JSON.stringify(locationData) // 将位置数据转换为JSON字符串存储 authorized_region: JSON.stringify(locationData) // 将位置数据转换为JSON字符串存储
}; };
// 确保包含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) {
// 尝试从用户信息中获取openid
const globalUserInfo = wx.getStorageSync('userInfo');
openid = globalUserInfo?.openid;
}
// 确保openid存在
if (!openid) {
console.error('位置上传失败: 未找到openid');
wx.showToast({
title: '位置上传失败,请先登录',
icon: 'none',
duration: 2000
});
return;
}
console.log('位置上传前检查openid:', openid);
console.log('准备上传的位置数据:', userInfo);
// 调用API上传位置数据 // 调用API上传位置数据
const api = require('../../utils/api.js'); const api = require('../../utils/api.js');
api.uploadUserInfo(userInfo).then(res => { api.uploadUserInfo({
...userInfo,
openid: openid // 明确传递openid
}).then(res => {
console.log('位置数据上传成功:', res); console.log('位置数据上传成功:', res);
// 显示上传成功提示
wx.showToast({
title: '位置更新成功',
icon: 'success',
duration: 1500
});
}).catch(err => { }).catch(err => {
console.error('位置数据上传失败:', err); console.error('位置数据上传失败:', err);
// 显示上传失败提示
wx.showToast({
title: '位置上传失败',
icon: 'none',
duration: 2000
});
}); });
}, },
fail() { fail() {

40
server-example/server-mysql.js

@ -644,6 +644,10 @@ User.init({
partnerstatus: { partnerstatus: {
type: DataTypes.STRING(255) // 合作商状态 type: DataTypes.STRING(255) // 合作商状态
}, },
// 授权区域字段 - 用于存储用户位置信息
authorized_region: {
type: DataTypes.TEXT // 存储用户位置信息的JSON字符串
},
reasonforfailure: { reasonforfailure: {
type: DataTypes.TEXT // 审核失败原因 type: DataTypes.TEXT // 审核失败原因
}, },
@ -1188,12 +1192,17 @@ app.post('/api/user/upload', async (req, res) => {
console.log('收到用户信息上传请求:', userData); console.log('收到用户信息上传请求:', userData);
// 使用微信小程序拉取唯一电话号码的插件,不再需要检查手机号冲突 // 使用微信小程序拉取唯一电话号码的插件,不再需要检查手机号冲突
// 确保用户数据中包含手机号 // 检查用户是否已存在,如果是更新操作,不需要强制要求手机号
if (!userData.phoneNumber) { let existingUser = await User.findOne({
where: { openid: userData.openid }
});
// 只有在创建新用户时才强制要求手机号
if (!existingUser && !userData.phoneNumber) {
return res.json({ return res.json({
success: false, success: false,
code: 400, code: 400,
message: '缺少手机号信息', message: '创建新用户时必须提供手机号信息',
data: {} data: {}
}); });
} }
@ -1509,7 +1518,8 @@ app.post('/api/user/get', async (req, res) => {
const user = await User.findOne({ const user = await User.findOne({
where: { openid }, where: { openid },
include: [ include: [
{ model: Contact, {
model: Contact,
as: 'contacts', as: 'contacts',
attributes: ['id', 'name', 'phoneNumber', 'wechat', 'account', 'accountNumber', 'bank', 'address'] attributes: ['id', 'name', 'phoneNumber', 'wechat', 'account', 'accountNumber', 'bank', 'address']
}, },
@ -1859,13 +1869,13 @@ app.post('/api/products/upload', upload.array('images', 10), async (req, res) =>
return await handleAddImagesToExistingProduct(req, res, existingProductId, req.files || []); return await handleAddImagesToExistingProduct(req, res, existingProductId, req.files || []);
} }
let productData; let productData;
let uploadedFiles = req.files || []; // 修复:使用req.files而不是空数组 let uploadedFiles = req.files || []; // 修复:使用req.files而不是空数组
let imageUrls = []; let imageUrls = [];
let product; let product;
let tempFilesToClean = []; // 存储需要清理的临时文件路径 let tempFilesToClean = []; // 存储需要清理的临时文件路径
try { try {
// 【关键修复】首先检查是否是仅上传图片到已存在商品的请求 // 【关键修复】首先检查是否是仅上传图片到已存在商品的请求
const isAddImagesOnly = req.body.action === 'add_images_only' || req.body.isUpdate === 'true'; const isAddImagesOnly = req.body.action === 'add_images_only' || req.body.isUpdate === 'true';
const existingProductId = req.body.productId; const existingProductId = req.body.productId;
@ -3144,7 +3154,7 @@ try {
// 发送最终增强的响应 // 发送最终增强的响应
res.status(200).json(customResponseData); res.status(200).json(customResponseData);
} catch (err) { } catch (err) {
console.error('【错误】在添加商品时出错:', err); console.error('【错误】在添加商品时出错:', err);
res.status(500).json({ res.status(500).json({
success: false, success: false,
@ -3152,7 +3162,7 @@ try {
code: 500, code: 500,
error: err.message error: err.message
}); });
} }
}); });
// 【关键修复】在 handleAddImagesToExistingProduct 函数中加强图片合并逻辑 // 【关键修复】在 handleAddImagesToExistingProduct 函数中加强图片合并逻辑
@ -8292,10 +8302,10 @@ updateProductContacts().then(() => {
// 启动服务器监听 - 使用配置好的http server对象 // 启动服务器监听 - 使用配置好的http server对象
// 监听0.0.0.0以允许通过所有网络接口访问(包括IPv4地址) // 监听0.0.0.0以允许通过所有网络接口访问(包括IPv4地址)
// 启动连接监控 // 启动连接监控
startConnectionMonitoring(); startConnectionMonitoring();
console.log('连接监控服务已启动'); console.log('连接监控服务已启动');
server.listen(PORT, '0.0.0.0', () => { server.listen(PORT, '0.0.0.0', () => {
console.log(`\n🚀 服务器启动成功,监听端口 ${PORT}`); console.log(`\n🚀 服务器启动成功,监听端口 ${PORT}`);
console.log(`API 服务地址: http://localhost:${PORT}`); console.log(`API 服务地址: http://localhost:${PORT}`);
console.log(`API 通过IP访问地址: http://192.168.0.98:${PORT}`); console.log(`API 通过IP访问地址: http://192.168.0.98:${PORT}`);

Loading…
Cancel
Save