|
|
|
@ -89,6 +89,42 @@ app.use((req, res, next) => { |
|
|
|
// 中间件
|
|
|
|
app.use(bodyParser.json()); |
|
|
|
|
|
|
|
// 添加CORS头,解决跨域问题
|
|
|
|
app.use((req, res, next) => { |
|
|
|
res.setHeader('Access-Control-Allow-Origin', '*'); |
|
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); |
|
|
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); |
|
|
|
if (req.method === 'OPTIONS') { |
|
|
|
return res.sendStatus(200); |
|
|
|
} |
|
|
|
next(); |
|
|
|
}); |
|
|
|
|
|
|
|
// 测试接口 - 用于验证请求是否到达后端
|
|
|
|
app.get('/api/test', (req, res) => { |
|
|
|
res.json({ |
|
|
|
success: true, |
|
|
|
message: '后端服务正常运行', |
|
|
|
timestamp: new Date().toISOString(), |
|
|
|
headers: req.headers |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
// 测试POST接口
|
|
|
|
app.post('/api/test/post', (req, res) => { |
|
|
|
console.log('===== 测试POST接口被调用 ====='); |
|
|
|
console.log('1. 收到请求体:', JSON.stringify(req.body, null, 2)); |
|
|
|
console.log('2. 请求头:', req.headers); |
|
|
|
console.log('================================'); |
|
|
|
|
|
|
|
res.json({ |
|
|
|
success: true, |
|
|
|
message: 'POST请求成功接收', |
|
|
|
receivedData: req.body, |
|
|
|
timestamp: new Date().toISOString() |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
// 创建临时文件夹用于存储上传的文件
|
|
|
|
const uploadTempDir = path.join(__dirname, 'temp-uploads'); |
|
|
|
if (!fs.existsSync(uploadTempDir)) { |
|
|
|
@ -568,6 +604,11 @@ User.init({ |
|
|
|
avatarUrl: { |
|
|
|
type: DataTypes.TEXT |
|
|
|
}, |
|
|
|
nickName: { |
|
|
|
type: DataTypes.STRING(100), |
|
|
|
allowNull: false, // 数据库NOT NULL: 联系人
|
|
|
|
comment: '联系人' |
|
|
|
}, |
|
|
|
phoneNumber: { |
|
|
|
type: DataTypes.STRING(20), |
|
|
|
allowNull: false // 电话号码,必填
|
|
|
|
@ -584,15 +625,18 @@ User.init({ |
|
|
|
}, |
|
|
|
province: { |
|
|
|
type: DataTypes.STRING(50), |
|
|
|
allowNull: true // 省份,可选
|
|
|
|
allowNull: false, // 数据库NOT NULL: 省份
|
|
|
|
comment: '省份' |
|
|
|
}, |
|
|
|
city: { |
|
|
|
type: DataTypes.STRING(50), |
|
|
|
allowNull: true // 城市,可选
|
|
|
|
allowNull: false, // 数据库NOT NULL: 城市
|
|
|
|
comment: '城市' |
|
|
|
}, |
|
|
|
district: { |
|
|
|
type: DataTypes.STRING(255), |
|
|
|
allowNull: true // 区域,可选
|
|
|
|
allowNull: false, // 数据库NOT NULL: 区域
|
|
|
|
comment: '区域' |
|
|
|
}, |
|
|
|
detailedaddress: { |
|
|
|
type: DataTypes.STRING(255) // 详细地址
|
|
|
|
@ -623,22 +667,27 @@ User.init({ |
|
|
|
// 入驻相关字段
|
|
|
|
collaborationid: { |
|
|
|
type: DataTypes.TEXT, |
|
|
|
allowNull: true // 合作商身份,可选
|
|
|
|
allowNull: false, // 数据库NOT NULL: 合作商身份
|
|
|
|
comment: '合作商身份' |
|
|
|
}, |
|
|
|
cooperation: { |
|
|
|
type: DataTypes.STRING(255), |
|
|
|
allowNull: true // 合作模式,可选
|
|
|
|
allowNull: false, // 数据库NOT NULL: 合作模式
|
|
|
|
comment: '合作模式' |
|
|
|
}, |
|
|
|
businesslicenseurl: { |
|
|
|
type: DataTypes.TEXT, |
|
|
|
allowNull: true // 营业执照,可选
|
|
|
|
allowNull: false, // 数据库NOT NULL: 营业执照
|
|
|
|
comment: '营业执照' |
|
|
|
}, |
|
|
|
proofurl: { |
|
|
|
type: DataTypes.TEXT, |
|
|
|
allowNull: true // 证明材料,可选
|
|
|
|
allowNull: false, // 数据库NOT NULL: 证明材料
|
|
|
|
comment: '证明材料:鸡场--动物检疫合格证,贸易商--法人身份证' |
|
|
|
}, |
|
|
|
brandurl: { |
|
|
|
type: DataTypes.TEXT // 品牌授权链文件
|
|
|
|
type: DataTypes.TEXT, // 品牌授权链文件(可为空)
|
|
|
|
comment: '品牌授权链文件' |
|
|
|
}, |
|
|
|
// 合作状态相关字段
|
|
|
|
partnerstatus: { |
|
|
|
@ -1519,6 +1568,7 @@ app.post('/api/wechat/getOpenid', async (req, res) => { |
|
|
|
userId, |
|
|
|
session_key, |
|
|
|
name: '微信用户', // 临时占位,等待用户授权
|
|
|
|
nickName: '微信用户', // 数据库NOT NULL字段
|
|
|
|
phoneNumber: '', // 使用空字符串代替临时手机号,后续由微信小程序拉取的真实手机号更新
|
|
|
|
type: userType, // 使用客户端传入的类型或默认买家身份
|
|
|
|
province: '', // 默认空字符串
|
|
|
|
@ -6024,6 +6074,13 @@ app.get('/api/settlement/status/:userId', async (req, res) => { |
|
|
|
// 提交入驻申请
|
|
|
|
app.post('/api/settlement/submit', async (req, res) => { |
|
|
|
try { |
|
|
|
// 【调试】详细记录API接收到的原始数据
|
|
|
|
console.log('===== 入驻API调试开始 ====='); |
|
|
|
console.log('1. 原始请求体:', JSON.stringify(req.body, null, 2)); |
|
|
|
console.log('2. 请求头:', req.headers); |
|
|
|
console.log('3. 请求方法:', req.method); |
|
|
|
console.log('4. 请求URL:', req.url); |
|
|
|
|
|
|
|
const { openid, |
|
|
|
collaborationid, |
|
|
|
cooperation, |
|
|
|
@ -6038,10 +6095,33 @@ app.post('/api/settlement/submit', async (req, res) => { |
|
|
|
brandurl |
|
|
|
} = req.body; |
|
|
|
|
|
|
|
console.log('收到入驻申请:', req.body); |
|
|
|
// 【调试】验证解构后的数据值
|
|
|
|
console.log('5. 解构后的数据验证:'); |
|
|
|
console.log(' - openid:', openid, typeof openid); |
|
|
|
console.log(' - collaborationid:', collaborationid, typeof collaborationid); |
|
|
|
console.log(' - cooperation:', cooperation, typeof cooperation); |
|
|
|
console.log(' - company:', company, typeof company); |
|
|
|
console.log(' - phoneNumber:', phoneNumber, typeof phoneNumber); |
|
|
|
console.log(' - province:', province, typeof province); |
|
|
|
console.log(' - city:', city, typeof city); |
|
|
|
console.log(' - district:', district, typeof district); |
|
|
|
console.log(' - detailedaddress:', detailedaddress, typeof detailedaddress); |
|
|
|
console.log(' - businesslicenseurl:', businesslicenseurl, typeof businesslicenseurl); |
|
|
|
console.log(' - proofurl:', proofurl, typeof proofurl); |
|
|
|
console.log(' - brandurl:', brandurl, typeof brandurl); |
|
|
|
|
|
|
|
// 验证必填字段
|
|
|
|
if (!openid || !collaborationid || !cooperation || !company || !phoneNumber || !province || !city || !district) { |
|
|
|
console.error('6. 必填字段验证失败:', { |
|
|
|
openid: !!openid, |
|
|
|
collaborationid: !!collaborationid, |
|
|
|
cooperation: !!cooperation, |
|
|
|
company: !!company, |
|
|
|
phoneNumber: !!phoneNumber, |
|
|
|
province: !!province, |
|
|
|
city: !!city, |
|
|
|
district: !!district |
|
|
|
}); |
|
|
|
return res.status(400).json({ |
|
|
|
success: false, |
|
|
|
code: 400, |
|
|
|
@ -6049,8 +6129,19 @@ app.post('/api/settlement/submit', async (req, res) => { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
console.log('6. 必填字段验证通过'); |
|
|
|
|
|
|
|
// 查找用户信息
|
|
|
|
const user = await User.findOne({ where: { openid } }); |
|
|
|
console.log('7. 查找用户结果:', user ? { |
|
|
|
userId: user.userId, |
|
|
|
openid: user.openid, |
|
|
|
name: user.name, |
|
|
|
collaborationid: user.collaborationid, |
|
|
|
cooperation: user.cooperation, |
|
|
|
partnerstatus: user.partnerstatus |
|
|
|
} : '未找到用户'); |
|
|
|
|
|
|
|
if (!user) { |
|
|
|
return res.status(404).json({ |
|
|
|
success: false, |
|
|
|
@ -6061,6 +6152,7 @@ app.post('/api/settlement/submit', async (req, res) => { |
|
|
|
|
|
|
|
// 检查用户是否已有入驻信息且状态为审核中
|
|
|
|
if (user.collaborationid && user.partnerstatus === 'underreview') { |
|
|
|
console.log('8. 用户已有待审核的入驻申请,拒绝重复提交'); |
|
|
|
return res.status(400).json({ |
|
|
|
success: false, |
|
|
|
code: 400, |
|
|
|
@ -6068,66 +6160,113 @@ app.post('/api/settlement/submit', async (req, res) => { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
console.log('8. 用户状态检查通过,允许提交'); |
|
|
|
|
|
|
|
// 更新用户表中的入驻信息
|
|
|
|
// 转换collaborationid为中文(使用明确的英文标识以避免混淆)
|
|
|
|
let collaborationidCN = collaborationid; |
|
|
|
if (collaborationid === 'chicken') { |
|
|
|
collaborationidCN = '鸡场'; |
|
|
|
} else if (collaborationid === 'trader') { |
|
|
|
collaborationidCN = '贸易商'; |
|
|
|
} |
|
|
|
// 兼容旧的wholesale标识
|
|
|
|
else if (collaborationid === 'wholesale') { |
|
|
|
collaborationidCN = '贸易商'; |
|
|
|
const collaborationidMap = { |
|
|
|
'chicken': '鸡场', |
|
|
|
'trader': '贸易商', |
|
|
|
'wholesale': '贸易商' |
|
|
|
}; |
|
|
|
|
|
|
|
if (collaborationidMap.hasOwnProperty(collaborationid)) { |
|
|
|
collaborationidCN = collaborationidMap[collaborationid]; |
|
|
|
} else { |
|
|
|
// 如果传入的不是预期的英文值,直接使用传入值(可能是中文)
|
|
|
|
collaborationidCN = collaborationid; |
|
|
|
} |
|
|
|
|
|
|
|
console.log('9. collaborationid转换结果:', { |
|
|
|
原值: collaborationid, |
|
|
|
转换后: collaborationidCN, |
|
|
|
映射表: collaborationidMap |
|
|
|
}); |
|
|
|
|
|
|
|
// 转换cooperation为中文合作模式(使用明确的英文标识以避免混淆)
|
|
|
|
// 直接使用传入的中文合作模式,确保支持:资源委托、自主定义销售、区域包场合作、其他
|
|
|
|
let cooperationCN = cooperation; |
|
|
|
const cooperationMap = { |
|
|
|
'resource_delegation': '资源委托', |
|
|
|
'self_define_sales': '自主定义销售', |
|
|
|
'regional_exclusive': '区域包场合作', |
|
|
|
'other': '其他', |
|
|
|
'wholesale': '资源委托', |
|
|
|
'self_define': '自主定义销售', |
|
|
|
// 添加中文值映射(前端直接传递中文值)
|
|
|
|
'代销业务': '代销业务', |
|
|
|
'采销联盟合作': '采销联盟合作', |
|
|
|
'包场合作': '包场合作' |
|
|
|
}; |
|
|
|
|
|
|
|
if (cooperationMap.hasOwnProperty(cooperation)) { |
|
|
|
cooperationCN = cooperationMap[cooperation]; |
|
|
|
} else { |
|
|
|
// 如果传入的不是预期的英文值,直接使用传入值(可能是中文)
|
|
|
|
cooperationCN = cooperation; |
|
|
|
} |
|
|
|
|
|
|
|
console.log('10. cooperation转换结果:', { |
|
|
|
原值: cooperation, |
|
|
|
转换后: cooperationCN, |
|
|
|
映射表: cooperationMap |
|
|
|
}); |
|
|
|
|
|
|
|
// 如果传入的是英文值,则进行映射
|
|
|
|
if (cooperation === 'resource_delegation') { |
|
|
|
cooperationCN = '资源委托'; |
|
|
|
} else if (cooperation === 'self_define_sales') { |
|
|
|
cooperationCN = '自主定义销售'; |
|
|
|
} else if (cooperation === 'regional_exclusive') { |
|
|
|
cooperationCN = '区域包场合作'; |
|
|
|
} else if (cooperation === 'other') { |
|
|
|
cooperationCN = '其他'; |
|
|
|
} |
|
|
|
// 兼容旧的wholesale标识
|
|
|
|
else if (cooperation === 'wholesale') { |
|
|
|
cooperationCN = '资源委托'; |
|
|
|
} |
|
|
|
// 兼容旧的self_define标识
|
|
|
|
else if (cooperation === 'self_define') { |
|
|
|
cooperationCN = '自主定义销售'; |
|
|
|
} |
|
|
|
// 确保存储的是中文合作模式
|
|
|
|
|
|
|
|
// 执行更新操作
|
|
|
|
const updateResult = await User.update({ |
|
|
|
collaborationid: collaborationidCN, // 合作商身份(中文)
|
|
|
|
cooperation: cooperationCN, // 合作模式(中文)
|
|
|
|
company: company, // 公司名称
|
|
|
|
phoneNumber: phoneNumber, // 电话号码
|
|
|
|
province: province, // 省份
|
|
|
|
city: city, // 城市
|
|
|
|
district: district, // 区县
|
|
|
|
detailedaddress: detailedaddress || '',// 详细地址
|
|
|
|
businesslicenseurl: businesslicenseurl || '', // 营业执照 - 使用空字符串作为默认值
|
|
|
|
proofurl: proofurl || '', // 证明材料 - 使用空字符串作为默认值
|
|
|
|
brandurl: brandurl || '', // 品牌授权链文件
|
|
|
|
partnerstatus: 'underreview', // 合作商状态明确设置为审核中,覆盖数据库默认值
|
|
|
|
// 构建更新数据对象,确保字段名称与数据库表结构完全匹配
|
|
|
|
// 特别注意:数据库中以下字段为NOT NULL约束
|
|
|
|
const updateData = { |
|
|
|
nickName: user.nickName || String(company || '未知联系人'), // 数据库NOT NULL: 联系人
|
|
|
|
collaborationid: String(collaborationidCN || '未选择'), // 数据库NOT NULL: 合作商身份
|
|
|
|
cooperation: String(cooperationCN || '未选择'), // 数据库NOT NULL: 合作模式
|
|
|
|
company: String(company || ''), // 公司名称
|
|
|
|
phoneNumber: String(phoneNumber || ''), // 电话号码
|
|
|
|
province: String(province || ''), // 数据库NOT NULL: 省份
|
|
|
|
city: String(city || ''), // 数据库NOT NULL: 城市
|
|
|
|
district: String(district || ''), // 数据库NOT NULL: 区域
|
|
|
|
detailedaddress: String(detailedaddress || ''), // 详细地址
|
|
|
|
businesslicenseurl: String(!businesslicenseurl || businesslicenseurl.trim() === '' ? '未上传' : businesslicenseurl), // 数据库NOT NULL: 营业执照
|
|
|
|
proofurl: String(!proofurl || proofurl.trim() === '' ? '未上传' : proofurl), // 数据库NOT NULL: 证明材料
|
|
|
|
brandurl: String(!brandurl || brandurl.trim() === '' ? '' : brandurl), // 品牌授权链文件(可为空)
|
|
|
|
partnerstatus: 'underreview', // 合作商状态明确设置为审核中
|
|
|
|
updated_at: getBeijingTime() |
|
|
|
}, { |
|
|
|
where: { openid: openid } // 改为使用openid作为查询条件,确保能找到用户
|
|
|
|
}; |
|
|
|
|
|
|
|
console.log('11. 构建的updateData对象:', JSON.stringify(updateData, null, 2)); |
|
|
|
console.log('12. 准备执行数据库更新,openid:', openid); |
|
|
|
|
|
|
|
// 【调试】在更新前记录当前数据库状态
|
|
|
|
const beforeUpdateUser = await User.findOne({ where: { openid: openid } }); |
|
|
|
console.log('13. 更新前数据库状态:', beforeUpdateUser ? { |
|
|
|
userId: beforeUpdateUser.userId, |
|
|
|
openid: beforeUpdateUser.openid, |
|
|
|
collaborationid: beforeUpdateUser.collaborationid, |
|
|
|
cooperation: beforeUpdateUser.cooperation, |
|
|
|
company: beforeUpdateUser.company, |
|
|
|
phoneNumber: beforeUpdateUser.phoneNumber, |
|
|
|
province: beforeUpdateUser.province, |
|
|
|
city: beforeUpdateUser.city, |
|
|
|
district: beforeUpdateUser.district, |
|
|
|
detailedaddress: beforeUpdateUser.detailedaddress, |
|
|
|
businesslicenseurl: beforeUpdateUser.businesslicenseurl, |
|
|
|
proofurl: beforeUpdateUser.proofurl, |
|
|
|
brandurl: beforeUpdateUser.brandurl, |
|
|
|
partnerstatus: beforeUpdateUser.partnerstatus, |
|
|
|
updated_at: beforeUpdateUser.updated_at |
|
|
|
} : '未找到用户记录'); |
|
|
|
|
|
|
|
// 使用Sequelize的update方法更新数据
|
|
|
|
const updateResult = await User.update(updateData, { |
|
|
|
where: { openid: openid } |
|
|
|
}); |
|
|
|
|
|
|
|
console.log('14. Sequelize更新操作结果:', { |
|
|
|
affectedRows: updateResult[0], |
|
|
|
affectedCount: updateResult[1], |
|
|
|
成功: updateResult[0] > 0 |
|
|
|
}); |
|
|
|
|
|
|
|
// 检查更新结果
|
|
|
|
console.log('更新结果:', updateResult); |
|
|
|
if (updateResult[0] === 0) { |
|
|
|
console.error('更新失败,未找到匹配的用户记录或没有更新任何字段'); |
|
|
|
console.error('15. 更新失败,未找到匹配的用户记录或没有更新任何字段'); |
|
|
|
return res.status(500).json({ |
|
|
|
success: false, |
|
|
|
code: 500, |
|
|
|
@ -6135,21 +6274,63 @@ app.post('/api/settlement/submit', async (req, res) => { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// 【调试】在更新后记录数据库状态
|
|
|
|
const afterUpdateUser = await User.findOne({ where: { openid: openid } }); |
|
|
|
console.log('16. 更新后数据库状态:', afterUpdateUser ? { |
|
|
|
userId: afterUpdateUser.userId, |
|
|
|
openid: afterUpdateUser.openid, |
|
|
|
collaborationid: afterUpdateUser.collaborationid, |
|
|
|
cooperation: afterUpdateUser.cooperation, |
|
|
|
company: afterUpdateUser.company, |
|
|
|
phoneNumber: afterUpdateUser.phoneNumber, |
|
|
|
province: afterUpdateUser.province, |
|
|
|
city: afterUpdateUser.city, |
|
|
|
district: afterUpdateUser.district, |
|
|
|
detailedaddress: afterUpdateUser.detailedaddress, |
|
|
|
businesslicenseurl: afterUpdateUser.businesslicenseurl, |
|
|
|
proofurl: afterUpdateUser.proofurl, |
|
|
|
brandurl: afterUpdateUser.brandurl, |
|
|
|
partnerstatus: afterUpdateUser.partnerstatus, |
|
|
|
updated_at: afterUpdateUser.updated_at |
|
|
|
} : '未找到用户记录'); |
|
|
|
|
|
|
|
// 【调试】对比更新前后的差异
|
|
|
|
if (beforeUpdateUser && afterUpdateUser) { |
|
|
|
console.log('17. 更新前后数据对比:'); |
|
|
|
const changedFields = {}; |
|
|
|
Object.keys(updateData).forEach(field => { |
|
|
|
const beforeValue = beforeUpdateUser[field]; |
|
|
|
const afterValue = afterUpdateUser[field]; |
|
|
|
if (beforeValue !== afterValue) { |
|
|
|
changedFields[field] = { |
|
|
|
更新前: beforeValue, |
|
|
|
更新后: afterValue |
|
|
|
}; |
|
|
|
} |
|
|
|
}); |
|
|
|
console.log('18. 实际发生变化的字段:', changedFields); |
|
|
|
} |
|
|
|
|
|
|
|
// 验证更新是否成功
|
|
|
|
const updatedUser = await User.findOne({ where: { openid: openid } }); |
|
|
|
console.log('更新后的用户状态:', updatedUser ? updatedUser.partnerstatus : '未找到用户'); |
|
|
|
console.log('19. 验证更新后的用户状态:', updatedUser ? updatedUser.partnerstatus : '未找到用户'); |
|
|
|
|
|
|
|
// 双重确认:如果状态仍不是underreview,再次更新
|
|
|
|
if (updatedUser && updatedUser.partnerstatus !== 'underreview') { |
|
|
|
console.warn('检测到状态未更新正确,执行二次更新:', updatedUser.partnerstatus); |
|
|
|
console.warn('20. 检测到状态未更新正确,执行二次更新,当前状态:', updatedUser.partnerstatus); |
|
|
|
await User.update({ |
|
|
|
partnerstatus: 'underreview' |
|
|
|
}, { |
|
|
|
where: { openid: openid } |
|
|
|
}); |
|
|
|
|
|
|
|
// 【调试】二次更新后的状态
|
|
|
|
const finalUpdateUser = await User.findOne({ where: { openid: openid } }); |
|
|
|
console.log('21. 二次更新后的用户状态:', finalUpdateUser ? finalUpdateUser.partnerstatus : '未找到用户'); |
|
|
|
} |
|
|
|
|
|
|
|
console.log('用户入驻信息更新成功,用户ID:', user.userId); |
|
|
|
console.log('22. 用户入驻信息更新成功,用户ID:', user.userId); |
|
|
|
console.log('===== 入驻API调试结束 ====='); |
|
|
|
|
|
|
|
res.json({ |
|
|
|
success: true, |
|
|
|
|