diff --git a/routes/orders.js b/routes/orders.js index 278d8da..b618f12 100644 --- a/routes/orders.js +++ b/routes/orders.js @@ -722,7 +722,7 @@ router.put('/:id/confirm', auth, async (req, res) => { // 检查订单是否存在且属于当前用户 const [orders] = await getDB().execute( - 'SELECT id, status FROM orders WHERE id = ? AND user_id = ?', + 'SELECT * FROM orders WHERE id = ? AND user_id = ?', [orderId, userId] ); @@ -736,6 +736,24 @@ router.put('/:id/confirm', auth, async (req, res) => { return res.status(400).json({ success: false, message: '只能确认已发货的订单' }); } + // 佣金分配 + const [products] = await getDB().execute( + 'SELECT * FROM order_items WHERE order_id = ?', + [orderId] + ); + for (const product of products) { + const [producersResult] = await getDB().execute( + 'SELECT * FROM products WHERE id = ?', + [product.product_id] + ); + if (producersResult[0].shop_name) { + await getDB().execute( + 'UPDATE users SET income = income + ? WHERE id = ?', + [producersResult[0].price * product.quantity, parseInt(producersResult[0].shop_name)] + ); + } + } + // 更新订单状态 await getDB().execute( 'UPDATE orders SET status = "completed", updated_at = NOW() WHERE id = ?', diff --git a/routes/products.js b/routes/products.js index 37f52fe..a4dff09 100644 --- a/routes/products.js +++ b/routes/products.js @@ -104,7 +104,7 @@ const router = express.Router(); // 商品管理路由 router.get('/', async (req, res) => { try { - const { page = 1, limit = 10, search = '', category = '', status = '' } = req.query; + const { page = 1, limit = 10, search = '', category = '', status = '', sort } = req.query; // 确保参数为有效数字 const pageNum = Math.max(1, parseInt(page) || 1); @@ -128,6 +128,21 @@ router.get('/', async (req, res) => { } else { whereClause += ' AND status = "active"'; } + + switch (sort) { + case 'price_desc': + whereClause += ' ORDER BY sale_price DESC' + break; + case 'price_asc': + whereClause += ' ORDER BY sale_price ASC' + break; + case 'sales_desc': + whereClause += ' ORDER BY sales DESC' + break; + default: + whereClause += ' ORDER BY created_at DESC' + break; + } // 获取总数 const countQuery = `SELECT COUNT(*) as total FROM products ${whereClause}`; @@ -139,13 +154,12 @@ router.get('/', async (req, res) => { SELECT id, name, rongdou_price, points_price, stock, image_url as image, description, status, payment_methods, created_at, updated_at, sales, images FROM products ${whereClause} - ORDER BY created_at DESC LIMIT ${limitNum} OFFSET ${offset} `; // 确保参数数组正确传递 const queryParams = [...params]; - console.log('Query params:', queryParams, 'Query:', query); + // console.log('Query params:', queryParams, 'Query:', query); const [products] = await getDB().execute(query, queryParams); products.forEach(item=>{