完善订单相关逻辑和商品推荐逻辑,添加接口注释
This commit is contained in:
320
routes/orders.js
320
routes/orders.js
@@ -385,7 +385,7 @@ router.get('/:id', auth, async (req, res) => {
|
||||
}
|
||||
|
||||
// 处理地址信息
|
||||
console.log(order.address,'order.address');
|
||||
// console.log(order.address,'order.address');
|
||||
|
||||
if (order.address) {
|
||||
try {
|
||||
@@ -442,6 +442,23 @@ router.post('/create-from-cart', auth, async (req, res) => {
|
||||
return res.status(400).json({ success: false, message: '购物车商品不存在' });
|
||||
}
|
||||
|
||||
for (const item of cartItems) {
|
||||
|
||||
const [stock] = await getDB().execute(
|
||||
`SELECT stock FROM product_spec_combinations WHERE id = ?`,
|
||||
[item.specification_id]
|
||||
);
|
||||
|
||||
if (stock[0].stock < item.quantity) {
|
||||
await db.query('ROLLBACK');
|
||||
return res.status(400).json({ success: false, message: `商品 ${item.name} 库存不足` });
|
||||
}
|
||||
await db.execute(
|
||||
`UPDATE product_spec_combinations SET stock = stock - ? WHERE id = ?`,
|
||||
[item.quantity, item.specification_id]
|
||||
);
|
||||
}
|
||||
|
||||
// 验证商品状态和库存,计算总价和支付方式
|
||||
let totalAmount = 0;
|
||||
let totalPoints = 0;
|
||||
@@ -612,9 +629,9 @@ router.put('/:id/cancel', auth, async (req, res) => {
|
||||
|
||||
const order = orders[0];
|
||||
|
||||
if (order.status !== 'pending') {
|
||||
if (order.status !== 'pending' && order.status !== 'pre_order') {
|
||||
await db.query('ROLLBACK');
|
||||
return res.status(400).json({ success: false, message: '只能取消待处理的订单' });
|
||||
return res.status(400).json({ success: false, message: '只能取消待处理或待支付的订单' });
|
||||
}
|
||||
|
||||
// 退还用户积分
|
||||
@@ -630,6 +647,19 @@ router.put('/:id/cancel', auth, async (req, res) => {
|
||||
[userId, order.total_points]
|
||||
);
|
||||
|
||||
console.log(12345,order.id);
|
||||
const [orderDetails] = await db.execute(
|
||||
'SELECT * FROM order_items WHERE order_id = ?',
|
||||
[order.id]
|
||||
);
|
||||
console.log(12345,orderDetails);
|
||||
// 返还库存
|
||||
await db.execute(
|
||||
'UPDATE product_spec_combinations SET stock = stock + ? WHERE id = ?',
|
||||
[orderDetails[0].quantity, orderDetails[0].spec_combination_id]
|
||||
);
|
||||
|
||||
|
||||
// 更新订单状态
|
||||
await db.execute(
|
||||
'UPDATE orders SET status = "cancelled", updated_at = NOW() WHERE id = ?',
|
||||
@@ -937,7 +967,7 @@ router.get('/pending-payment/:id', auth, async (req, res) => {
|
||||
`SELECT
|
||||
oi.id, oi.product_id, oi.quantity, oi.price, oi.points_price, oi.rongdou_price,
|
||||
oi.spec_combination_id,
|
||||
p.name as product_name, p.image_url, p.description,
|
||||
p.name as product_name, p.image_url, p.description, p.payment_methods,
|
||||
psc.spec_values as spec_info
|
||||
FROM order_items oi
|
||||
LEFT JOIN products p ON oi.product_id = p.id
|
||||
@@ -961,7 +991,10 @@ router.get('/pending-payment/:id', auth, async (req, res) => {
|
||||
success: true,
|
||||
data: {
|
||||
...order,
|
||||
items: orderItems
|
||||
items: orderItems.map(item => ({
|
||||
...item,
|
||||
payment_methods: JSON.parse(item.payment_methods)
|
||||
}))
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -1061,7 +1094,7 @@ router.post('/confirm-payment', auth, async (req, res) => {
|
||||
try {
|
||||
await connection.beginTransaction();
|
||||
|
||||
const { orderId: order_id, addressId: address_id, couponRecordId } = req.body;
|
||||
const { orderId: order_id, addressId: address_id, couponRecordId, paymentMethod } = req.body;
|
||||
const userId = req.user.id;
|
||||
|
||||
// 验证必填字段
|
||||
@@ -1089,29 +1122,29 @@ router.post('/confirm-payment', auth, async (req, res) => {
|
||||
const order = orders[0];
|
||||
|
||||
// 解析支付方式
|
||||
let allPaymentMethods = [];
|
||||
// console.log(typeof order.payment_methods_list);
|
||||
// let allPaymentMethods = [];
|
||||
// // console.log(typeof order.payment_methods_list);
|
||||
|
||||
if (order.payment_methods_list) {
|
||||
try {
|
||||
// 数据库中存储的是序列化的JSON字符串,直接解析
|
||||
// if (order.payment_methods_list) {
|
||||
// try {
|
||||
// // 数据库中存储的是序列化的JSON字符串,直接解析
|
||||
|
||||
allPaymentMethods = JSON.parse(JSON.parse(order.payment_methods_list));
|
||||
} catch (e) {
|
||||
console.error('解析支付方式失败:', e, 'raw data:', order.payment_methods_list);
|
||||
allPaymentMethods = [];
|
||||
}
|
||||
}
|
||||
// allPaymentMethods = JSON.parse(JSON.parse(order.payment_methods_list));
|
||||
// } catch (e) {
|
||||
// console.error('解析支付方式失败:', e, 'raw data:', order.payment_methods_list);
|
||||
// allPaymentMethods = [];
|
||||
// }
|
||||
// }
|
||||
|
||||
// 去重支付方式
|
||||
allPaymentMethods = [...new Set(allPaymentMethods)];
|
||||
// // 去重支付方式
|
||||
// allPaymentMethods = [...new Set(allPaymentMethods)];
|
||||
|
||||
// 判断支付方式类型
|
||||
const hasPoints = allPaymentMethods.includes('points') || allPaymentMethods.includes('points_rongdou');
|
||||
const hasRongdou = allPaymentMethods.includes('rongdou') || allPaymentMethods.includes('points_rongdou');
|
||||
const isComboPayment = allPaymentMethods.includes('points_rongdou');
|
||||
// // 判断支付方式类型
|
||||
// const hasPoints = allPaymentMethods.includes('points') || allPaymentMethods.includes('points_rongdou');
|
||||
// const hasRongdou = allPaymentMethods.includes('rongdou') || allPaymentMethods.includes('points_rongdou');
|
||||
// const isComboPayment = allPaymentMethods.includes('points_rongdou');
|
||||
|
||||
console.log('订单支付方式:', allPaymentMethods, { hasPoints, hasRongdou, isComboPayment });
|
||||
// console.log('订单支付方式:', allPaymentMethods, { hasPoints, hasRongdou, isComboPayment });
|
||||
|
||||
// 获取收货地址信息
|
||||
const [addresses] = await connection.execute(
|
||||
@@ -1142,85 +1175,184 @@ router.post('/confirm-payment', auth, async (req, res) => {
|
||||
return res.status(400).json({ success: false, message: '融豆不足' });
|
||||
}
|
||||
user.balance = Math.abs(user.balance);
|
||||
|
||||
// 根据支付方式处理扣费逻辑
|
||||
let totalRongdouNeeded = order.total_rongdou; // 需要的融豆总数
|
||||
let pointsToDeduct = 0; // 需要扣除的积分
|
||||
let rongdouToDeduct = 0; // 需要扣除的融豆
|
||||
|
||||
if (!hasRongdou && !hasPoints) {
|
||||
await connection.rollback();
|
||||
return res.status(400).json({ success: false, message: '商品支付方式配置错误' });
|
||||
}
|
||||
|
||||
if (hasPoints && !hasRongdou) {
|
||||
// 只支持积分支付,按10000积分=1融豆转换
|
||||
const pointsNeeded = totalRongdouNeeded * 10000;
|
||||
if (user.points < pointsNeeded) {
|
||||
await connection.rollback();
|
||||
return res.status(400).json({ success: false, message: '积分不足' });
|
||||
}
|
||||
pointsToDeduct = pointsNeeded;
|
||||
rongdouToDeduct = 0;
|
||||
} else if (!hasPoints && hasRongdou) {
|
||||
// 只支持融豆支付
|
||||
if (user.balance < totalRongdouNeeded) {
|
||||
await connection.rollback();
|
||||
return res.status(400).json({ success: false, message: '融豆不足' });
|
||||
}
|
||||
pointsToDeduct = 0;
|
||||
rongdouToDeduct = totalRongdouNeeded;
|
||||
} else if (hasPoints && hasRongdou) {
|
||||
// 组合支付:先扣积分,不足部分用融豆
|
||||
const availablePointsInRongdou = Math.floor(user.points / 10000); // 积分可转换的融豆数
|
||||
|
||||
if (availablePointsInRongdou >= totalRongdouNeeded) {
|
||||
// 积分足够支付全部
|
||||
pointsToDeduct = totalRongdouNeeded * 10000;
|
||||
rongdouToDeduct = 0;
|
||||
} else {
|
||||
// 积分不够,需要组合支付
|
||||
pointsToDeduct = availablePointsInRongdou * 10000;
|
||||
rongdouToDeduct = totalRongdouNeeded - availablePointsInRongdou;
|
||||
|
||||
if (user.balance < rongdouToDeduct) {
|
||||
|
||||
// 开始扣钱
|
||||
switch (paymentMethod) {
|
||||
case 'points':
|
||||
// 积分支付逻辑
|
||||
if (user.points < order.total_points) {
|
||||
await connection.rollback();
|
||||
return res.status(400).json({ success: false, message: '积分和融豆余额不足' });
|
||||
return res.status(400).json({ success: false, message: '积分不足' });
|
||||
}
|
||||
}
|
||||
await connection.execute(
|
||||
'UPDATE users SET points = points - ? WHERE id = ?',
|
||||
[order.total_points, userId]
|
||||
);
|
||||
|
||||
// 记录积分变动历史
|
||||
await connection.execute(
|
||||
`INSERT INTO points_history (user_id, type, amount, description, order_id)
|
||||
VALUES (?, 'spend', ?, ?, ?)`,
|
||||
[userId, order.total_points, `订单支付 - ${order.order_no}`, order_id]
|
||||
);
|
||||
|
||||
// 供应商分佣
|
||||
// await connection.execute(
|
||||
// 'UPDATE users SET points = points + ? WHERE id = ?',
|
||||
// [order.total_points * 0.1, order.shop_id]
|
||||
// );
|
||||
// console.log(123,order)
|
||||
|
||||
break;
|
||||
case 'beans':
|
||||
// 融豆支付逻辑
|
||||
if (user.balance < order.total_rongdou) {
|
||||
await connection.rollback();
|
||||
return res.status(400).json({ success: false, message: '融豆不足' });
|
||||
}
|
||||
await connection.execute(
|
||||
'UPDATE users SET balance = balance + ? WHERE id = ?',
|
||||
[order.total_rongdou, userId]
|
||||
);
|
||||
|
||||
// 记录融豆变动历史
|
||||
await connection.execute(
|
||||
`INSERT INTO points_history (user_id, type, amount, description, order_id)
|
||||
VALUES (?, 'spend', ?, ?, ?)`,
|
||||
[userId, order.total_rongdou, `订单支付 - ${order.order_no}`, order_id]
|
||||
);
|
||||
break;
|
||||
case 'mixed':
|
||||
// 积分和融豆组合支付逻辑
|
||||
|
||||
if(user.points < order.total_points) {
|
||||
const needPoints = (user.points/10000).floor(0) * 10000;
|
||||
const needBeans = order.total_rongdou - needPoints/10000;
|
||||
if(user.balance < needBeans) {
|
||||
await connection.rollback();
|
||||
return res.status(400).json({ success: false, message: '融豆不足' });
|
||||
}
|
||||
|
||||
await connection.execute(
|
||||
'UPDATE users SET points = points - ? WHERE id = ?',
|
||||
[needPoints, userId]
|
||||
);
|
||||
|
||||
// 记录积分变动历史
|
||||
await connection.execute(
|
||||
`INSERT INTO points_history (user_id, type, amount, description, order_id)
|
||||
VALUES (?, 'spend', ?, ?, ?)`,
|
||||
[userId, needPoints, `订单支付 - ${order.order_no}`, order_id]
|
||||
);
|
||||
|
||||
await connection.execute(
|
||||
'UPDATE users SET balance = balance + ? WHERE id = ?',
|
||||
[needBeans, userId]
|
||||
);
|
||||
|
||||
// 记录融豆变动历史
|
||||
await connection.execute(
|
||||
`INSERT INTO points_history (user_id, type, amount, description, order_id)
|
||||
VALUES (?, 'spend', ?, ?, ?)`,
|
||||
[userId, needBeans, `订单支付 - ${order.order_no}`, order_id]
|
||||
);
|
||||
} else {
|
||||
await connection.execute(
|
||||
'UPDATE users SET points = points - ? WHERE id = ?',
|
||||
[order.total_points, userId]
|
||||
);
|
||||
// 记录积分变动历史
|
||||
await connection.execute(
|
||||
`INSERT INTO points_history (user_id, type, amount, description, order_id)
|
||||
VALUES (?, 'spend', ?, ?, ?)`,
|
||||
[userId, order.total_points, `订单支付 - ${order.order_no}`, order_id]
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
await connection.rollback();
|
||||
return res.status(400).json({ success: false, message: '支付方式配置错误' });
|
||||
}
|
||||
|
||||
console.log('扣费计算:', { totalRongdouNeeded, pointsToDeduct, rongdouToDeduct, userPoints: user.points, userBalance: user.balance });
|
||||
// // 根据支付方式处理扣费逻辑
|
||||
// let totalRongdouNeeded = order.total_rongdou; // 需要的融豆总数
|
||||
// let pointsToDeduct = 0; // 需要扣除的积分
|
||||
// let rongdouToDeduct = 0; // 需要扣除的融豆
|
||||
|
||||
// if (!hasRongdou && !hasPoints) {
|
||||
// await connection.rollback();
|
||||
// return res.status(400).json({ success: false, message: '商品支付方式配置错误' });
|
||||
// }
|
||||
|
||||
// if (hasPoints && !hasRongdou) {
|
||||
// // 只支持积分支付,按10000积分=1融豆转换
|
||||
// const pointsNeeded = totalRongdouNeeded * 10000;
|
||||
// if (user.points < pointsNeeded) {
|
||||
// await connection.rollback();
|
||||
// return res.status(400).json({ success: false, message: '积分不足' });
|
||||
// }
|
||||
// pointsToDeduct = pointsNeeded;
|
||||
// rongdouToDeduct = 0;
|
||||
// } else if (!hasPoints && hasRongdou) {
|
||||
// // 只支持融豆支付
|
||||
// if (user.balance < totalRongdouNeeded) {
|
||||
// await connection.rollback();
|
||||
// return res.status(400).json({ success: false, message: '融豆不足' });
|
||||
// }
|
||||
// pointsToDeduct = 0;
|
||||
// rongdouToDeduct = totalRongdouNeeded;
|
||||
// } else if (hasPoints && hasRongdou) {
|
||||
// // 组合支付:先扣积分,不足部分用融豆
|
||||
// const availablePointsInRongdou = Math.floor(user.points / 10000); // 积分可转换的融豆数
|
||||
|
||||
// if (availablePointsInRongdou >= totalRongdouNeeded) {
|
||||
// // 积分足够支付全部
|
||||
// pointsToDeduct = totalRongdouNeeded * 10000;
|
||||
// rongdouToDeduct = 0;
|
||||
// } else {
|
||||
// // 积分不够,需要组合支付
|
||||
// pointsToDeduct = availablePointsInRongdou * 10000;
|
||||
// rongdouToDeduct = totalRongdouNeeded - availablePointsInRongdou;
|
||||
|
||||
// if (user.balance < rongdouToDeduct) {
|
||||
// await connection.rollback();
|
||||
// return res.status(400).json({ success: false, message: '积分和融豆余额不足' });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// console.log('扣费计算:', { totalRongdouNeeded, pointsToDeduct, rongdouToDeduct, userPoints: user.points, userBalance: user.balance });
|
||||
|
||||
// 扣除积分
|
||||
if (pointsToDeduct > 0) {
|
||||
await connection.execute(
|
||||
'UPDATE users SET points = points - ? WHERE id = ?',
|
||||
[pointsToDeduct, userId]
|
||||
);
|
||||
// if (pointsToDeduct > 0) {
|
||||
// await connection.execute(
|
||||
// 'UPDATE users SET points = points - ? WHERE id = ?',
|
||||
// [pointsToDeduct, userId]
|
||||
// );
|
||||
|
||||
// 记录积分变动历史
|
||||
await connection.execute(
|
||||
`INSERT INTO points_history (user_id, type, amount, description, order_id)
|
||||
VALUES (?, 'spend', ?, ?, ?)`,
|
||||
[userId, pointsToDeduct, `订单支付 - ${order.order_no}`, order_id]
|
||||
);
|
||||
}
|
||||
// // 记录积分变动历史
|
||||
// await connection.execute(
|
||||
// `INSERT INTO points_history (user_id, type, amount, description, order_id)
|
||||
// VALUES (?, 'spend', ?, ?, ?)`,
|
||||
// [userId, pointsToDeduct, `订单支付 - ${order.order_no}`, order_id]
|
||||
// );
|
||||
// }
|
||||
|
||||
// 扣除融豆
|
||||
if (rongdouToDeduct > 0) {
|
||||
await connection.execute(
|
||||
'UPDATE users SET balance = balance + ? WHERE id = ?',
|
||||
[rongdouToDeduct, userId]
|
||||
);
|
||||
// // 扣除融豆
|
||||
// if (rongdouToDeduct > 0) {
|
||||
// await connection.execute(
|
||||
// 'UPDATE users SET balance = balance + ? WHERE id = ?',
|
||||
// [rongdouToDeduct, userId]
|
||||
// );
|
||||
|
||||
// 记录融豆变动历史
|
||||
await connection.execute(
|
||||
`INSERT INTO rongdou_history (user_id, type, amount, description, order_id)
|
||||
VALUES (?, 'spend', ?, ?, ?)`,
|
||||
[userId, rongdouToDeduct, `订单支付 - ${order.order_no}`, order_id]
|
||||
);
|
||||
}
|
||||
// // 记录融豆变动历史
|
||||
// await connection.execute(
|
||||
// `INSERT INTO rongdou_history (user_id, type, amount, description, order_id)
|
||||
// VALUES (?, 'spend', ?, ?, ?)`,
|
||||
// [userId, rongdouToDeduct, `订单支付 - ${order.order_no}`, order_id]
|
||||
// );
|
||||
// }
|
||||
|
||||
// 更新优惠券记录
|
||||
if (couponRecordId) {
|
||||
|
||||
Reference in New Issue
Block a user