|
|
@ -310,9 +310,10 @@ User.init({ |
|
|
allowNull: false, |
|
|
allowNull: false, |
|
|
unique: true |
|
|
unique: true |
|
|
}, |
|
|
}, |
|
|
nickName: { |
|
|
name: { |
|
|
type: DataTypes.STRING(100), |
|
|
type: DataTypes.STRING(255), |
|
|
allowNull: false // 微信名,必填
|
|
|
allowNull: true, // 昵称,可选
|
|
|
|
|
|
comment: '昵称' |
|
|
}, |
|
|
}, |
|
|
avatarUrl: { |
|
|
avatarUrl: { |
|
|
type: DataTypes.TEXT |
|
|
type: DataTypes.TEXT |
|
|
@ -593,9 +594,10 @@ Contact.init({ |
|
|
allowNull: false, |
|
|
allowNull: false, |
|
|
unique: true |
|
|
unique: true |
|
|
}, |
|
|
}, |
|
|
nickName: { |
|
|
name: { |
|
|
type: DataTypes.STRING(100), |
|
|
type: DataTypes.STRING(100), |
|
|
allowNull: false // 联系人
|
|
|
allowNull: false, // 联系人
|
|
|
|
|
|
comment: '联系人' |
|
|
}, |
|
|
}, |
|
|
phoneNumber: { |
|
|
phoneNumber: { |
|
|
type: DataTypes.STRING(20), |
|
|
type: DataTypes.STRING(20), |
|
|
@ -839,14 +841,14 @@ async function createUserAssociations(user) { |
|
|
await sequelize.transaction(async (transaction) => { |
|
|
await sequelize.transaction(async (transaction) => { |
|
|
// 1. 处理联系人记录 - 使用INSERT ... ON DUPLICATE KEY UPDATE确保无论如何都只保留一条记录
|
|
|
// 1. 处理联系人记录 - 使用INSERT ... ON DUPLICATE KEY UPDATE确保无论如何都只保留一条记录
|
|
|
await sequelize.query( |
|
|
await sequelize.query( |
|
|
`INSERT INTO contacts (userId, nickName, phoneNumber, created_at, updated_at)
|
|
|
`INSERT INTO contacts (userId, name, phoneNumber, created_at, updated_at)
|
|
|
VALUES (?, ?, ?, NOW(), NOW()) |
|
|
VALUES (?, ?, ?, NOW(), NOW()) |
|
|
ON DUPLICATE KEY UPDATE |
|
|
ON DUPLICATE KEY UPDATE |
|
|
nickName = VALUES(nickName), |
|
|
name = VALUES(name), |
|
|
phoneNumber = VALUES(phoneNumber), |
|
|
phoneNumber = VALUES(phoneNumber), |
|
|
updated_at = NOW()`,
|
|
|
updated_at = NOW()`,
|
|
|
{ |
|
|
{ |
|
|
replacements: [user.userId, user.nickName || '默认联系人', user.phoneNumber || ''], |
|
|
replacements: [user.userId, user.name || '默认联系人', user.phoneNumber || ''], |
|
|
transaction: transaction |
|
|
transaction: transaction |
|
|
} |
|
|
} |
|
|
); |
|
|
); |
|
|
@ -1098,7 +1100,7 @@ app.post('/api/wechat/getOpenid', async (req, res) => { |
|
|
openid, |
|
|
openid, |
|
|
userId, |
|
|
userId, |
|
|
session_key, |
|
|
session_key, |
|
|
nickName: '微信用户', // 临时占位,等待用户授权
|
|
|
name: '微信用户', // 临时占位,等待用户授权
|
|
|
phoneNumber: '', // 使用空字符串代替临时手机号,后续由微信小程序拉取的真实手机号更新
|
|
|
phoneNumber: '', // 使用空字符串代替临时手机号,后续由微信小程序拉取的真实手机号更新
|
|
|
type: userType, // 使用客户端传入的类型或默认买家身份
|
|
|
type: userType, // 使用客户端传入的类型或默认买家身份
|
|
|
province: '', // 默认空字符串
|
|
|
province: '', // 默认空字符串
|
|
|
@ -1114,7 +1116,7 @@ app.post('/api/wechat/getOpenid', async (req, res) => { |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// 为新创建的用户创建关联记录
|
|
|
// 为新创建的用户创建关联记录
|
|
|
const newUser = { userId, openid, nickName: '微信用户', phoneNumber: '' }; |
|
|
const newUser = { userId, openid, name: '微信用户', phoneNumber: '' }; |
|
|
await createUserAssociations(newUser); |
|
|
await createUserAssociations(newUser); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -1159,7 +1161,7 @@ app.post('/api/user/validate', async (req, res) => { |
|
|
// 查找用户
|
|
|
// 查找用户
|
|
|
const user = await User.findOne({ |
|
|
const user = await User.findOne({ |
|
|
where: { openid }, |
|
|
where: { openid }, |
|
|
attributes: ['openid', 'userId', 'nickName', 'avatarUrl', 'phoneNumber', 'type'] |
|
|
attributes: ['openid', 'userId', 'name', 'avatarUrl', 'phoneNumber', 'type'] |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
if (!user) { |
|
|
if (!user) { |
|
|
@ -1204,10 +1206,9 @@ 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', 'nickName', 'phoneNumber', 'wechat', 'account', 'accountNumber', 'bank', 'address'] |
|
|
attributes: ['id', 'name', 'phoneNumber', 'wechat', 'account', 'accountNumber', 'bank', 'address'] |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
model: UserManagement, |
|
|
model: UserManagement, |
|
|
@ -1407,12 +1408,12 @@ app.post('/api/product/list', async (req, res) => { |
|
|
const { count, rows: products } = await Product.findAndCountAll({ |
|
|
const { count, rows: products } = await Product.findAndCountAll({ |
|
|
where, |
|
|
where, |
|
|
include: [ |
|
|
include: [ |
|
|
{ |
|
|
{ |
|
|
model: User, |
|
|
model: User, |
|
|
as: 'seller', |
|
|
as: 'seller', |
|
|
attributes: ['userId', 'nickName', 'avatarUrl'] |
|
|
attributes: ['userId', 'name', 'avatarUrl'] |
|
|
} |
|
|
} |
|
|
], |
|
|
], |
|
|
attributes: { |
|
|
attributes: { |
|
|
include: [ |
|
|
include: [ |
|
|
'region' // 【新增】确保返回地区字段
|
|
|
'region' // 【新增】确保返回地区字段
|
|
|
@ -2713,7 +2714,7 @@ try { |
|
|
{ |
|
|
{ |
|
|
model: User, |
|
|
model: User, |
|
|
as: 'seller', |
|
|
as: 'seller', |
|
|
attributes: ['userId', 'nickName', 'avatarUrl'] |
|
|
attributes: ['userId', 'name', 'avatarUrl'] |
|
|
} |
|
|
} |
|
|
] |
|
|
] |
|
|
}); |
|
|
}); |
|
|
@ -3060,7 +3061,7 @@ app.post('/api/products/detail', async (req, res) => { |
|
|
{ |
|
|
{ |
|
|
model: User, |
|
|
model: User, |
|
|
as: 'seller', |
|
|
as: 'seller', |
|
|
attributes: ['userId', 'nickName', 'avatarUrl'] |
|
|
attributes: ['userId', 'name', 'avatarUrl'] |
|
|
} |
|
|
} |
|
|
] |
|
|
] |
|
|
}); |
|
|
}); |
|
|
@ -4195,7 +4196,7 @@ app.post('/api/user/debug', async (req, res) => { |
|
|
// 查询用户信息
|
|
|
// 查询用户信息
|
|
|
const user = await User.findOne({ |
|
|
const user = await User.findOne({ |
|
|
where: { openid }, |
|
|
where: { openid }, |
|
|
attributes: ['openid', 'userId', 'nickName', 'phoneNumber', 'type'] |
|
|
attributes: ['openid', 'userId', 'name', 'phoneNumber', 'type'] |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
if (!user) { |
|
|
if (!user) { |
|
|
@ -4320,7 +4321,7 @@ app.post('/api/product/hide', async (req, res) => { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.log('找到用户信息:', { userId: user.userId, nickName: user.nickName }); |
|
|
console.log('找到用户信息:', { userId: user.userId, name: user.name }); |
|
|
|
|
|
|
|
|
// 查找商品并验证所有权 - 直接使用userId,因为商品创建时使用的就是userId
|
|
|
// 查找商品并验证所有权 - 直接使用userId,因为商品创建时使用的就是userId
|
|
|
const product = await Product.findOne({ |
|
|
const product = await Product.findOne({ |
|
|
@ -4561,7 +4562,7 @@ app.post('/api/product/publish', async (req, res) => { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.log('找到用户:', { userId: user.userId, nickName: user.nickName, type: user.type }); |
|
|
console.log('找到用户:', { userId: user.userId, name: user.name, type: user.type }); |
|
|
|
|
|
|
|
|
// 验证用户类型
|
|
|
// 验证用户类型
|
|
|
console.log(`验证用户类型: 用户ID=${user.userId}, 类型=${user.type}`); |
|
|
console.log(`验证用户类型: 用户ID=${user.userId}, 类型=${user.type}`); |
|
|
@ -4625,7 +4626,7 @@ app.post('/api/product/publish', async (req, res) => { |
|
|
{ |
|
|
{ |
|
|
model: User, |
|
|
model: User, |
|
|
as: 'seller', |
|
|
as: 'seller', |
|
|
attributes: ['userId', 'nickName', 'avatarUrl'] |
|
|
attributes: ['userId', 'name', 'avatarUrl'] |
|
|
} |
|
|
} |
|
|
] |
|
|
] |
|
|
}); |
|
|
}); |
|
|
|