新增翻页相关功能
This commit is contained in:
@@ -3,19 +3,61 @@ const { getDB } = require('../database');
|
|||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// 获取商品分类列表
|
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const [categories] = await getDB().execute(
|
const { page = 1, limit = 10, search = '', level = '' } = req.query;
|
||||||
'SELECT * FROM category'
|
|
||||||
);
|
|
||||||
|
|
||||||
|
// 确保参数为有效数字
|
||||||
|
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({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
categories
|
categories,
|
||||||
|
total
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取商品分类失败:', error);
|
console.error('获取商品分类失败:', error);
|
||||||
res.status(500).json({ success: false, message: '获取商品分类失败' });
|
res.status(500).json({ success: false, message: '获取商品分类失败' });
|
||||||
@@ -24,10 +66,10 @@ router.get('/', async (req, res) => {
|
|||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { name, level, parent_id } = req.body;
|
const { name, level, parent_id, image } = req.body;
|
||||||
await getDB().execute(
|
await getDB().execute(
|
||||||
'INSERT INTO category (category_name, level, parent_id) VALUES (?, ?, ?)',
|
'INSERT INTO category (category_name, level, parent_id, image) VALUES (?, ?, ?, ?)',
|
||||||
[name, level, parent_id === undefined ? null : parent_id]
|
[name, level, parent_id === undefined ? null : parent_id, image === undefined ? null : image]
|
||||||
);
|
);
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -42,11 +84,11 @@ router.post('/', async (req, res) => {
|
|||||||
router.put('/:id', async (req, res) => {
|
router.put('/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const { category_name } = req.body;
|
const { category_name, image } = req.body;
|
||||||
console.log("123456", req.body, id);
|
console.log("123456", req.body, id);
|
||||||
await getDB().execute(
|
await getDB().execute(
|
||||||
'UPDATE category SET category_name = ? WHERE id = ?',
|
'UPDATE category SET category_name = ? , image = ? WHERE id = ?',
|
||||||
[category_name, id]
|
[category_name, image === undefined ? null : image, id]
|
||||||
);
|
);
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const { getDB } = require('../database');
|
const { getDB } = require('../database');
|
||||||
|
const { log } = require('winston');
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
@@ -328,17 +329,18 @@ router.get('/', async (req, res) => {
|
|||||||
|
|
||||||
router.get('/:id', async (req, res) => {
|
router.get('/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
console.log(1234,req.query)
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const isAdmin = req.user.role === 'admin';
|
// const isAdmin = req.user.role === 'admin';
|
||||||
|
|
||||||
let whereClause = 'WHERE o.id = ?';
|
let whereClause = 'WHERE o.id = ?';
|
||||||
const params = [id];
|
const params = [id];
|
||||||
|
|
||||||
// 非管理员只能查看自己的订单
|
// 非管理员只能查看自己的订单
|
||||||
if (!isAdmin) {
|
// if (!isAdmin) {
|
||||||
whereClause += ' AND o.user_id = ?';
|
// whereClause += ' AND o.user_id = ?';
|
||||||
params.push(req.user.id);
|
// params.push(req.user.id);
|
||||||
}
|
// }
|
||||||
|
|
||||||
const query = `
|
const query = `
|
||||||
SELECT
|
SELECT
|
||||||
@@ -774,6 +776,8 @@ router.put('/:id/status', async (req, res) => {
|
|||||||
|
|
||||||
const orderId = req.params.id;
|
const orderId = req.params.id;
|
||||||
const { status } = req.body;
|
const { status } = req.body;
|
||||||
|
const deliveryCode = req.body.logistics_no;
|
||||||
|
const logisticsCompany = req.body.logistics_company;
|
||||||
|
|
||||||
const validStatuses = ['pending', 'shipped', 'completed', 'cancelled'];
|
const validStatuses = ['pending', 'shipped', 'completed', 'cancelled'];
|
||||||
if (!validStatuses.includes(status)) {
|
if (!validStatuses.includes(status)) {
|
||||||
@@ -828,10 +832,17 @@ router.put('/:id/status', async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 更新订单状态
|
// 更新订单状态
|
||||||
await db.execute(
|
if (status === 'shipped') {
|
||||||
'UPDATE orders SET status = ?, updated_at = NOW() WHERE id = ?',
|
await db.execute(
|
||||||
[status, orderId]
|
'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');
|
await db.query('COMMIT');
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ router.get('/', async (req, res) => {
|
|||||||
const limitNum = Math.max(1, Math.min(100, parseInt(limit) || 10)); // 限制最大100条
|
const limitNum = Math.max(1, Math.min(100, parseInt(limit) || 10)); // 限制最大100条
|
||||||
const offset = Math.max(0, (pageNum - 1) * limitNum);
|
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';
|
let whereClause = 'WHERE 1=1';
|
||||||
const params = [];
|
const params = [];
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ router.get('/', async (req, res) => {
|
|||||||
const params = [];
|
const params = [];
|
||||||
|
|
||||||
if (search) {
|
if (search) {
|
||||||
whereClause += ' AND name LIKE ?';
|
whereClause += ' AND amount LIKE ?';
|
||||||
params.push(`%${search}%`);
|
params.push(`%${search}%`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user