From 779a7fa57b1fcde2a90cc0324bf0b8da4c3a840b Mon Sep 17 00:00:00 2001 From: dzl <786316265@qq.com> Date: Fri, 17 Oct 2025 17:26:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=95=86=E5=93=81=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/products.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/routes/products.js b/routes/products.js index 2df3e25..f509c5a 100644 --- a/routes/products.js +++ b/routes/products.js @@ -405,7 +405,7 @@ router.get('/:id', async (req, res) => { router.post('/', async (req, res) => { try { const { - name, description, price, points_price, rongdou_price = 0, stock, category, + name, description, price, points_price, rongdou_price = 0, stock, category = [], image_url, images = [], videos = [], details, status = 'active', shop_name, shop_avatar, payment_methods = ['points', 'rongdou', 'points_rongdou'], attributes = [] } = req.body; @@ -424,15 +424,33 @@ router.post('/', async (req, res) => { // ); const [result] = await getDB().execute( `UPDATE products - SET name = ?, description = ?, price = ?, points_price = ?, rongdou_price = ?, stock = ?, category = ?, + SET name = ?, description = ?, price = ?, points_price = ?, rongdou_price = ?, stock = ?, image_url = ?, images = ?, videos = ?, details = ?, shop_name = ?, shop_avatar = ?, payment_methods = ?, status = ?, updated_at = NOW() WHERE name = ?`, - [name, description, price, points_price, rongdou_price, stock, category || null, + [name, description, price, points_price, rongdou_price, stock, image_url, JSON.stringify(images), JSON.stringify(videos), details, shop_name, shop_avatar, JSON.stringify(payment_methods), status, 'temp'] ) - const productId = result.insertId; + const [product] = await getDB().execute( + 'SELECT * FROM products WHERE name = ?', + [name] + ); + const productId = product[0].id; + // console.log(123,product) + // console.log(123,category) + // 处理分类 + if (category.length > 0) { + for (const cat of category) { + // console.log(123,cat,product) + await getDB().execute( + `INSERT INTO products_category (product_id, category_id, create_time) + VALUES (?, ?, NOW())`, + [productId, cat] + ); + } + } + @@ -629,7 +647,7 @@ router.delete('/:id', async (req, res) => { } // 检查是否有相关订单 - const orderCheckQuery = 'SELECT id FROM orders WHERE product_id = ? LIMIT 1'; + const orderCheckQuery = 'SELECT id FROM order_items WHERE product_id = ? LIMIT 1'; const [orders] = await getDB().execute(orderCheckQuery, [id]); if (orders.length > 0) {