From fad0d070b69f9f74bfe5fc937048ca5030b5ad8b Mon Sep 17 00:00:00 2001 From: dzl <786316265@qq.com> Date: Mon, 20 Oct 2025 17:21:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=86=E7=B1=BB=E7=AD=9B?= =?UTF-8?q?=E9=80=89=EF=BC=8C=E6=96=B0=E5=A2=9E=E4=B8=AA=E4=BA=BA=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=8C=BA?= =?UTF-8?q?=E5=88=86=E4=B8=8D=E5=90=8C=E5=AE=A2=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/couponproducts.js | 6 ++- routes/orders.js | 56 ++++++++++++++++++--- routes/products.js | 102 ++++++++++++++++++++++++--------------- routes/supplier.js | 4 +- routes/users.js | 69 ++++++++++++++++++++++++++ routes/withdrawals.js | 7 ++- server.js | 3 ++ 7 files changed, 198 insertions(+), 49 deletions(-) create mode 100644 routes/users.js diff --git a/routes/couponproducts.js b/routes/couponproducts.js index f50d275..d1bcea5 100644 --- a/routes/couponproducts.js +++ b/routes/couponproducts.js @@ -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 = ` diff --git a/routes/orders.js b/routes/orders.js index 495027b..01b18f6 100644 --- a/routes/orders.js +++ b/routes/orders.js @@ -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, diff --git a/routes/products.js b/routes/products.js index f509c5a..7360a99 100644 --- a/routes/products.js +++ b/routes/products.js @@ -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, diff --git a/routes/supplier.js b/routes/supplier.js index 3700a59..3e1db08 100644 --- a/routes/supplier.js +++ b/routes/supplier.js @@ -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'); diff --git a/routes/users.js b/routes/users.js new file mode 100644 index 0000000..5f9a92d --- /dev/null +++ b/routes/users.js @@ -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; \ No newline at end of file diff --git a/routes/withdrawals.js b/routes/withdrawals.js index e6c2bb3..e754ec4 100644 --- a/routes/withdrawals.js +++ b/routes/withdrawals.js @@ -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}`; diff --git a/server.js b/server.js index b0608a3..2b8e4ca 100644 --- a/server.js +++ b/server.js @@ -114,6 +114,9 @@ app.use('/category', require('./routes/category')); // 认证接口 app.use('/auth', require('./routes/auth')); +// 用户接口 +app.use('/users', require('./routes/users')); + // 404处理