From 21585fe3e9a03142b24f99af7a08833e9927dc8c Mon Sep 17 00:00:00 2001 From: dzl <786316265@qq.com> Date: Wed, 15 Oct 2025 17:28:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=89=A9=E6=B5=81=E4=B8=8E?= =?UTF-8?q?=E5=95=86=E5=93=81=E7=BB=84=E5=90=88=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/category.js | 2 +- routes/orders.js | 5 ++- routes/products.js | 5 +++ routes/specifications.js | 69 +++++++++++++++++++++++++++++----------- 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/routes/category.js b/routes/category.js index 962f6d4..1768ce0 100644 --- a/routes/category.js +++ b/routes/category.js @@ -5,7 +5,7 @@ const router = express.Router(); router.get('/', async (req, res) => { try { - const { page = 1, limit = 10, search = '', level = '' } = req.query; + const { page = 1, limit = 100, search = '', level = '' } = req.query; // 确保参数为有效数字 const pageNum = Math.max(1, parseInt(page) || 1); diff --git a/routes/orders.js b/routes/orders.js index b45acb6..27115f2 100644 --- a/routes/orders.js +++ b/routes/orders.js @@ -329,7 +329,6 @@ router.get('/', async (req, res) => { router.get('/:id', async (req, res) => { try { - console.log(1234,req.query) const { id } = req.params; // const isAdmin = req.user.role === 'admin'; @@ -345,7 +344,7 @@ router.get('/:id', async (req, res) => { const query = ` SELECT o.id, o.order_no, o.user_id, o.total_amount, o.total_points, - o.status, o.address, o.created_at, o.updated_at, + o.status, o.address, o.created_at, o.updated_at, o.delivery_code, o.logistics_company, u.username, u.phone FROM orders o LEFT JOIN users u ON o.user_id = u.id @@ -386,7 +385,7 @@ router.get('/:id', async (req, res) => { } // 处理地址信息 - console.log(order.address,'order.address'); + // console.log(order.address,'order.address'); if (order.address) { try { diff --git a/routes/products.js b/routes/products.js index 4029aa2..f5b8978 100644 --- a/routes/products.js +++ b/routes/products.js @@ -207,6 +207,11 @@ router.get('/:id', async (req, res) => { [item.category_id] ); item.category_name = category[0][0].category_name; + const parentCategory = await getDB().execute( + `SELECT * FROM category WHERE id = ?`, + [category[0][0].parent_id] + ); + item.parentCategory_name = parentCategory[0][0].category_name; } product.category = categories; diff --git a/routes/specifications.js b/routes/specifications.js index 3fea030..66c469b 100644 --- a/routes/specifications.js +++ b/routes/specifications.js @@ -9,18 +9,17 @@ const sql = require("../config/config"); router.get('/names', async (req, res) => { try { const {status = 'active'} = req.query; - const {id: created_id} = req.user; - let query = `SELECT * - FROM spec_names - WHERE created_id = ${created_id}`; + let query = ` + SELECT * FROM spec_names + `; const params = []; if (status) { - query += ' and status = ?'; + query += ' WHERE status = ?'; params.push(status); } - query += ' ORDER BY sort_order, id'; + query += ' ORDER BY sort_order'; const [specNames] = await getDB().execute(query, params); @@ -39,14 +38,13 @@ router.post('/names', async (req, res) => { const db = getDB(); try { const {name, display_name, sort_order = 0} = req.body; - const {id: created_id} = req.user; if (!name || !display_name) { return res.status(400).json({success: false, message: '规格名称和显示名称不能为空'}); } - let spec_name = new SelectBuilder().from() + // let spec_name = new SelectBuilder().from() const [result] = await getDB().execute( - 'INSERT INTO spec_names (name, display_name, sort_order,created_id) VALUES (?, ?, ?,?)', - [name, display_name, sort_order,created_id] + 'INSERT INTO spec_names (name, display_name, sort_order) VALUES (?, ?, ?)', + [name, display_name, sort_order] ); res.status(201).json({ @@ -67,15 +65,16 @@ router.post('/names', async (req, res) => { router.delete('/names/:id', async (req, res) => { try { const {id} = req.params; - const {id:user_id,role} = req.user + const db = getDB(); + // const {id:user_id,role} = req.user let specCountQuery = new SelectBuilder() .from('spec_names') .select('COUNT(*) as total') .where('id=?',id) - if(role !== 'admin'){ - specCountQuery.where('created_id=?',user_id) - } + // if(role !== 'admin'){ + // specCountQuery.where('created_id=?',user_id) + // } let [spec] = await specCountQuery.execute(db) if (spec.total === 0) { return res.status(404).json({success: false, message: '规格名称不存在'}); @@ -114,7 +113,7 @@ router.get('/values', async (req, res) => { let query = ` SELECT sv.*, sn.name as spec_name, sn.display_name as spec_display_name FROM spec_values sv - LEFT JOIN spec_names sn ON sv.spec_name_id = sn.id + LEFT JOIN spec_names sn ON sv.spec_name_id = sn.id WHERE 1 = 1 `; const params = []; @@ -172,11 +171,42 @@ router.post('/values', async (req, res) => { } }); +router.delete('/values/:id', async (req, res) => { + try { + const {id} = req.params; + console.log(12345,id) + + // 检查规格值是否存在 + const [specValue] = await getDB().execute( + 'SELECT COUNT(*) as count FROM spec_values WHERE id = ?', + [id] + ); + if (specValue[0].count === 0) { + return res.status(404).json({success: false, message: '规格值不存在'}); + } else { + // 删除规格值 + await getDB().execute( + 'DELETE FROM spec_values WHERE id = ?', + [id] + ); + } + + res.json({ + success: true, + message: '规格值删除成功' + }); + } catch (error) { + console.error('删除规格值失败:', error); + res.status(500).json({success: false, message: '删除规格值失败'}); + } +}) + + router.get('/combinations/:productId', async (req, res) => { try { const {productId} = req.params; - const {status = 'active'} = req.query; + const {status} = req.query; // 获取商品的规格组合 let query = ` @@ -390,7 +420,7 @@ router.put('/combinations/:id', async (req, res) => { barcode, weight, volume, - status + is_available } = req.body; // 检查规格组合是否存在 @@ -447,9 +477,9 @@ router.put('/combinations/:id', async (req, res) => { updateValues.push(volume); } - if (status !== undefined) { + if (is_available !== undefined) { updateFields.push('status = ?'); - updateValues.push(status); + updateValues.push(is_available ? 'active' : 'inactive'); } if (updateFields.length === 0) { @@ -462,6 +492,7 @@ router.put('/combinations/:id', async (req, res) => { const updateQuery = `UPDATE product_spec_combinations SET ${updateFields.join(', ')} WHERE id = ?`; + console.log(12345,updateQuery, updateValues); await getDB().execute(updateQuery, updateValues);