diff --git a/routes/category.js b/routes/category.js index 6f7a430..962f6d4 100644 --- a/routes/category.js +++ b/routes/category.js @@ -3,19 +3,61 @@ const { getDB } = require('../database'); const router = express.Router(); -// 获取商品分类列表 router.get('/', async (req, res) => { try { - const [categories] = await getDB().execute( - 'SELECT * FROM category' - ); + const { page = 1, limit = 10, search = '', level = '' } = req.query; + // 确保参数为有效数字 + const pageNum = Math.max(1, parseInt(page) || 1); + const limitNum = Math.max(1, Math.min(100, parseInt(limit) || 10)); // 限制最大100条 + const offset = Math.max(0, (pageNum - 1) * limitNum); + + let whereClause = 'WHERE 1=1'; + const params = []; + + if (search) { + whereClause += ' AND category_name LIKE ?'; + params.push(`%${search}%`); + } + + if (level) { + whereClause += ' AND level = ?'; + params.push(level); + } + + console.log('分页参数:', { pageNum, limitNum, offset, search, level }); + + const countQuery = `SELECT COUNT(*) as total FROM category ${whereClause}`; + const [countResult] = await getDB().execute(countQuery, params); + const total = countResult[0].total; + + const query = ` + SELECT * + FROM category + ${whereClause} + LIMIT ${limitNum} OFFSET ${offset} + `; + const queryParams = [...params]; + const [categories] = await getDB().execute(query, queryParams); + + if(level === '2') { + for(let category of categories) { + const [parentCategory] = await getDB().execute( + 'SELECT category_name FROM category WHERE id = ?', + [category.parent_id] + ); + category.parentCategory = parentCategory[0].category_name; + } + } + res.json({ success: true, data: { - categories + categories, + total } }); + } catch (error) { console.error('获取商品分类失败:', error); res.status(500).json({ success: false, message: '获取商品分类失败' }); @@ -24,10 +66,10 @@ router.get('/', async (req, res) => { router.post('/', async (req, res) => { try { - const { name, level, parent_id } = req.body; + const { name, level, parent_id, image } = req.body; await getDB().execute( - 'INSERT INTO category (category_name, level, parent_id) VALUES (?, ?, ?)', - [name, level, parent_id === undefined ? null : parent_id] + 'INSERT INTO category (category_name, level, parent_id, image) VALUES (?, ?, ?, ?)', + [name, level, parent_id === undefined ? null : parent_id, image === undefined ? null : image] ); res.json({ success: true, @@ -42,11 +84,11 @@ router.post('/', async (req, res) => { router.put('/:id', async (req, res) => { try { const { id } = req.params; - const { category_name } = req.body; + const { category_name, image } = req.body; console.log("123456", req.body, id); await getDB().execute( - 'UPDATE category SET category_name = ? WHERE id = ?', - [category_name, id] + 'UPDATE category SET category_name = ? , image = ? WHERE id = ?', + [category_name, image === undefined ? null : image, id] ); res.json({ success: true, diff --git a/routes/orders.js b/routes/orders.js index 818ec76..b45acb6 100644 --- a/routes/orders.js +++ b/routes/orders.js @@ -1,5 +1,6 @@ const express = require('express'); const { getDB } = require('../database'); +const { log } = require('winston'); const router = express.Router(); @@ -328,17 +329,18 @@ 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'; + // const isAdmin = req.user.role === 'admin'; let whereClause = 'WHERE o.id = ?'; const params = [id]; // 非管理员只能查看自己的订单 - if (!isAdmin) { - whereClause += ' AND o.user_id = ?'; - params.push(req.user.id); - } + // if (!isAdmin) { + // whereClause += ' AND o.user_id = ?'; + // params.push(req.user.id); + // } const query = ` SELECT @@ -774,6 +776,8 @@ router.put('/:id/status', async (req, res) => { const orderId = req.params.id; const { status } = req.body; + const deliveryCode = req.body.logistics_no; + const logisticsCompany = req.body.logistics_company; const validStatuses = ['pending', 'shipped', 'completed', 'cancelled']; if (!validStatuses.includes(status)) { @@ -828,10 +832,17 @@ router.put('/:id/status', async (req, res) => { } // 更新订单状态 - await db.execute( - 'UPDATE orders SET status = ?, updated_at = NOW() WHERE id = ?', - [status, orderId] - ); + if (status === 'shipped') { + await db.execute( + 'UPDATE orders SET status = ?, delivery_code = ?, logistics_company = ?, updated_at = NOW() WHERE id = ?', + [status, deliveryCode, logisticsCompany, orderId] + ); + } else { + await db.execute( + 'UPDATE orders SET status = ?, updated_at = NOW() WHERE id = ?', + [status, orderId] + ); + } await db.query('COMMIT'); diff --git a/routes/products.js b/routes/products.js index d533938..4029aa2 100644 --- a/routes/products.js +++ b/routes/products.js @@ -13,7 +13,7 @@ router.get('/', async (req, res) => { const limitNum = Math.max(1, Math.min(100, parseInt(limit) || 10)); // 限制最大100条 const offset = Math.max(0, (pageNum - 1) * limitNum); - console.log('分页参数:', { pageNum, limitNum, offset, search, category, status }); + // console.log('分页参数:', { pageNum, limitNum, offset, search, category, status }); let whereClause = 'WHERE 1=1'; const params = []; diff --git a/routes/withdrawals.js b/routes/withdrawals.js index 4b06674..e6c2bb3 100644 --- a/routes/withdrawals.js +++ b/routes/withdrawals.js @@ -18,7 +18,7 @@ router.get('/', async (req, res) => { const params = []; if (search) { - whereClause += ' AND name LIKE ?'; + whereClause += ' AND amount LIKE ?'; params.push(`%${search}%`); }