修复分类筛选,新增个人信息修改接口,区分不同客户端

This commit is contained in:
dzl
2025-10-20 17:21:40 +08:00
parent 779a7fa57b
commit fad0d070b6
7 changed files with 198 additions and 49 deletions

View File

@@ -5,13 +5,17 @@ const { getDB } = require('../database');
router.get('/', async (req, res) => {
try {
const { page = 1, limit = 10 } = req.query;
const { page = 1, limit = 10, type } = req.query;
const pageNum = parseInt(page) || 1;
const limitNum = parseInt(limit) || 10;
const offset = (pageNum - 1) * limitNum;
let whereClause = 'WHERE 1=1';
const params = [];
if(type){
whereClause += ' AND c.type = ?';
params.push(type);
}
const countQuery = `

View File

@@ -10,7 +10,7 @@ const router = express.Router();
// 获取订单列表
router.get('/', async (req, res) => {
try {
const { page = 1, limit = 10, search = '', orderNumber = '', username = '', status = '', startDate = '', endDate = '' } = req.query;
const { page = 1, limit = 10, search = '', orderNumber = '', username = '', status = '', startDate = '', endDate = '', shop_name = '' } = req.query;
@@ -59,11 +59,24 @@ router.get('/', async (req, res) => {
params.push(endDate);
}
if (shop_name) {
whereClause += ' AND p.shop_name = ?';
params.push(shop_name);
}
// 获取总数
// const countQuery = `
// SELECT COUNT(*) as total
// FROM orders as o
// LEFT JOIN users u ON o.user_id = u.id
// ${whereClause}
// `;
const countQuery = `
SELECT COUNT(*) as total
SELECT COUNT(DISTINCT o.id) as total
FROM orders as o
LEFT JOIN users u ON o.user_id = u.id
LEFT JOIN users u ON o.user_id = u.id
LEFT JOIN order_items oi ON o.id = oi.order_id
LEFT JOIN products p ON oi.product_id = p.id
${whereClause}
`;
console.log(countQuery, params);
@@ -73,13 +86,26 @@ router.get('/', async (req, res) => {
console.log(total, '数量');
// 获取订单列表
// 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.total_rongdou,
// u.username, o.salesperson_id, o.delivery_code, o.logistics_company
// FROM orders o
// LEFT JOIN users u ON o.user_id = u.id
// ${whereClause}
// ORDER BY o.created_at DESC
// LIMIT ${limitNum} OFFSET ${offset}
// `;
const query = `
SELECT
SELECT DISTINCT
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.total_rongdou,
o.status, o.address, o.created_at, o.updated_at, o.total_rongdou,
u.username, o.salesperson_id, o.delivery_code, o.logistics_company
FROM orders o
LEFT JOIN users u ON o.user_id = u.id
LEFT JOIN order_items oi ON o.id = oi.order_id
LEFT JOIN products p ON oi.product_id = p.id
${whereClause}
ORDER BY o.created_at DESC
LIMIT ${limitNum} OFFSET ${offset}
@@ -114,7 +140,7 @@ router.get('/', async (req, res) => {
}
// 处理地址信息
console.log(order.address,'order.address');
// console.log(order.address,'order.address');
if (order.address) {
try {
@@ -127,9 +153,27 @@ router.get('/', async (req, res) => {
order.items = orderItems;
}
// let shopNames = [];
// for (const order of orders) {
// const orderItems = order.items;
// // console.log(111,orderItems[0].product_id);
// const query = `
// SELECT shop_name as shopName
// FROM order_items oi
// LEFT JOIN products p ON oi.product_id = p.id
// LEFT JOIN product_spec_combinations psc ON oi.spec_combination_id = psc.id
// WHERE oi.order_id = ? AND oi.product_id = ?
// `;
// const [result] = await getDB().execute(query, [order.id, orderItems[0].product_id]);
// order.shop_name = result[0].shopName;
// }
res.json({
success: true,
data: {
// orders: orders.filter(order => order.shop_name === shop_name.toString()),
orders,
pagination: {
page: pageNum,

View File

@@ -6,83 +6,97 @@ 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 = '', shop_name = '' } = 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);
// console.log('分页参数:', { pageNum, limitNum, offset, search, category, status });
let whereClause = 'WHERE 1=1';
const params = [];
let joinClause = ''; // 添加 JOIN 子句变量
if (search) {
whereClause += ' AND name LIKE ?';
whereClause += ' AND p.name LIKE ?';
params.push(`%${search}%`);
}
if (status) {
whereClause += ' AND p.status = ?';
params.push(status);
} else {
whereClause += ' AND p.status = "active"';
}
if (shop_name) {
whereClause += ' AND p.shop_name = ?';
params.push(shop_name);
}
// 处理分类筛选
if (category) {
whereClause += ' AND category = ?';
joinClause += ' JOIN products_category pc ON p.id = pc.product_id';
joinClause += ' JOIN category c ON pc.category_id = c.id';
whereClause += ' AND c.category_name = ?';
params.push(category);
}
if (status) {
whereClause += ' AND status = ?';
params.push(status);
} else {
whereClause += ' AND status = "active"';
}
// 获取总数
const countQuery = `SELECT COUNT(*) as total FROM products ${whereClause}`;
// 获取总数 - 需要添加 DISTINCT 因为 JOIN 可能导致重复
const countQuery = `
SELECT COUNT(DISTINCT p.id) as total
FROM products p
${joinClause}
${whereClause}
`;
const [countResult] = await getDB().execute(countQuery, params);
const total = countResult[0].total;
// 获取商品列表
const query = `
SELECT id, name, rongdou_price, points_price, stock, image_url as image, description, status, payment_methods, created_at, updated_at, sales
FROM products
SELECT DISTINCT
p.id, p.name, p.rongdou_price, p.points_price, p.stock,
p.image_url as image, p.description, p.status, p.payment_methods,
p.created_at, p.updated_at, p.sales, p.shop_name
FROM products p
${joinClause}
${whereClause}
ORDER BY created_at DESC
ORDER BY p.created_at DESC
LIMIT ${limitNum} OFFSET ${offset}
`;
// 确保参数数组正确传递
const queryParams = [...params];
const [products] = await getDB().execute(query, queryParams);
products.forEach(item=>{
item.payment_methods = JSON.parse(item.payment_methods)
})
// for(let item of products){
// if(item.category !== null){
// const categories = [];
// for(let id of item.category){
// const categoryQuery = `SELECT * FROM category WHERE id = ?`;
// const [categoryResult] = await getDB().execute(categoryQuery, [parseInt(id)]);
// if(categoryResult.length !== 0){
// categories.push(categoryResult[0].category_name);
// }
// }
// item.category = categories
// }
// }
const [products] = await getDB().execute(query, params);
// 处理支付方式
products.forEach(item => {
item.payment_methods = JSON.parse(item.payment_methods);
});
// 获取分类信息
for (let item of products) {
const query = `
SELECT * FROM users WHERE id = ?
`
const [user] = await getDB().execute(query, [parseInt(item.shop_name)])
item.provider = user[0]
for(let item of products){
item.category = []
item.category = [];
const [categories] = await getDB().execute(
`SELECT * FROM products_category WHERE product_id = ?`,
[item.id]
);
for(let category of categories){
for (let category of categories) {
const [categoryDetails] = await getDB().execute(
`SELECT * FROM category WHERE id = ?`,
[category.category_id]
);
item.category.push(categoryDetails[0].category_name)
if (categoryDetails.length > 0) {
item.category.push(categoryDetails[0].category_name);
}
}
}
res.json({
success: true,
data: {
@@ -390,6 +404,14 @@ router.get('/:id', async (req, res) => {
points: product.points_price,
image: product.image_url,
};
if(enhancedProduct.shop_name) {
const query = `
SELECT * FROM users WHERE id = ?
`
const [user] = await getDB().execute(query, [parseInt(enhancedProduct.shop_name)])
enhancedProduct.shop_name = user[0].username
}
res.json({
success: true,

View File

@@ -64,7 +64,9 @@ router.post('/add', async (req, res) => {
avatar,
phone,
user_type: 'supplier',
audit_status: 'approved'
role: 'supplier',
audit_status: 'approved',
payment_status: 'paid'
}
console.log(insetObj, '111')
await db.query('START TRANSACTION');

69
routes/users.js Normal file
View File

@@ -0,0 +1,69 @@
const express = require('express');
const { getDB } = require('../database');
const router = express.Router();
const bcrypt = require('bcryptjs');
router.put('/password', async (req, res) => {
try {
console.log(123, req.body);
const { id, oldPassword, newPassword } = req.body;
// 1. 先查询用户信息
const [users] = await getDB().execute('SELECT * FROM users WHERE id = ?', [parseInt(id)]);
if (users.length === 0) {
return res.status(404).json({ success: false, message: '用户不存在' });
}
const user = users[0];
console.log('数据库中的密码:', user.password);
// 2. 验证旧密码(使用 bcrypt 比较)
const isOldPasswordValid = await bcrypt.compare(oldPassword, user.password);
if (!isOldPasswordValid) {
return res.status(400).json({ success: false, message: '旧密码错误' });
}
// 3. 加密新密码
const hashedNewPassword = await bcrypt.hash(newPassword, 10);
// 4. 更新密码
const query = `
UPDATE users
SET password = ?
WHERE id = ?
`;
const [result] = await getDB().execute(query, [hashedNewPassword, parseInt(id)]);
if (result.affectedRows === 0) {
return res.status(500).json({ success: false, message: '密码更新失败' });
}
res.json({ success: true, message: '密码更新成功' });
} catch (error) {
console.error('更新密码失败:', error);
res.status(500).json({ success: false, message: '更新密码失败' });
}
});
router.put('/:id', async (req, res) => {
try {
const { id } = req.params;
const { alipayQr, avatar, bankCard, idCard, phone, realName, unionpayQr, username, wechatQr } = req.body;
const query = `
UPDATE users
SET alipay_qr = ?, avatar = ?, bank_card = ?, id_card = ?, phone = ?, real_name = ?, unionpay_qr = ?, username = ?, wechat_qr = ?
WHERE id = ?
`;
const [user] = await getDB().execute(query, [alipayQr, avatar, bankCard, idCard, phone, realName, unionpayQr, username, wechatQr, parseInt(id)]);
if (user.length === 0) {
return res.status(404).json({ success: false, message: '用户不存在' });
}
res.json({ success: true, data: user[0] });
} catch (error) {
console.error('更新用户信息失败:', error);
res.status(500).json({ success: false, message: '更新用户信息失败' });
}
})
module.exports = router;

View File

@@ -5,7 +5,7 @@ const router = express.Router();
router.get('/', async (req, res) => {
try {
const { page = 1, limit = 10, search = '' } = req.query;
const { page = 1, limit = 10, search = '', user_id } = req.query;
// 确保参数为有效数字
const pageNum = Math.max(1, parseInt(page) || 1);
@@ -21,6 +21,11 @@ router.get('/', async (req, res) => {
whereClause += ' AND amount LIKE ?';
params.push(`%${search}%`);
}
if (user_id) {
whereClause += ' AND user_id = ?';
params.push(user_id);
}
// 获取总数
const countQuery = `SELECT COUNT(*) as total FROM withdrawal ${whereClause}`;

View File

@@ -114,6 +114,9 @@ app.use('/category', require('./routes/category'));
// 认证接口
app.use('/auth', require('./routes/auth'));
// 用户接口
app.use('/users', require('./routes/users'));
// 404处理