Compare commits
2 Commits
658ff89c6a
...
40dced4079
| Author | SHA1 | Date | |
|---|---|---|---|
| 40dced4079 | |||
| 0388f3b0e7 |
2
.env
2
.env
@@ -5,7 +5,7 @@ DB_PASSWORD=5fYhw8z6T62b7heS
|
||||
DB_NAME=maov2
|
||||
|
||||
# JWT密钥
|
||||
JWT_SECRET=your_jwt_secret_key
|
||||
JWT_SECRET=NINGBOJURONGkejiyouxiangongsi202
|
||||
|
||||
# 阿里云短信服务配置
|
||||
# 请在阿里云控制台获取以下配置信息:
|
||||
|
||||
@@ -43,7 +43,7 @@ router.get('/', async (req, res) => {
|
||||
|
||||
// 获取商品列表
|
||||
const query = `
|
||||
SELECT id, name, rongdou_price, category, points_price, stock, image_url as image, description, status, payment_methods, created_at, updated_at
|
||||
SELECT id, name, shop_name, rongdou_price, category, points_price, stock, sales, image_url as image, description, status, payment_methods, created_at, updated_at
|
||||
FROM products
|
||||
${whereClause}
|
||||
ORDER BY created_at DESC
|
||||
|
||||
344
routes/shopbackend.js
Normal file
344
routes/shopbackend.js
Normal file
@@ -0,0 +1,344 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { auth } = require('../middleware/auth');
|
||||
const { getDB } = require('../database');
|
||||
|
||||
router.get('/', auth, async (req, res) => {
|
||||
try {
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
text: '123'
|
||||
}
|
||||
});
|
||||
console.log(req)
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/product-details/:id', auth, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const query = `
|
||||
SELECT id, name, price, category, description, stock, image_url
|
||||
FROM products
|
||||
WHERE id = ?
|
||||
`;
|
||||
const [product] = await getDB().execute(query, [id]);
|
||||
if (product.length === 0) {
|
||||
return res.status(404).json({ error: 'Product not found' });
|
||||
}
|
||||
res.json({
|
||||
success: true,
|
||||
data: product[0]
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/flash-products', auth, async (req, res) => {
|
||||
try {
|
||||
const { page = 1, limit = 10, search = '', category = '' } = 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});
|
||||
|
||||
let whereClause = 'WHERE 1=1';
|
||||
const params = [];
|
||||
|
||||
if (search) {
|
||||
whereClause += ' AND name LIKE ?';
|
||||
params.push(`%${search}%`);
|
||||
}
|
||||
|
||||
if (category) {
|
||||
whereClause += ' AND category = ?';
|
||||
params.push(category);
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
const countQuery = `SELECT COUNT(*) as total FROM flash_product ${whereClause}`;
|
||||
const [countResult] = await getDB().execute(countQuery, params);
|
||||
const total = countResult[0].total;
|
||||
|
||||
// 获取商品列表
|
||||
const query = `
|
||||
SELECT id, start_time, end_time, flash_stock, flash_price, products_id
|
||||
FROM flash_product
|
||||
${whereClause}
|
||||
ORDER BY id DESC
|
||||
LIMIT ${limitNum} OFFSET ${offset}
|
||||
`;
|
||||
|
||||
// 确保参数数组正确传递
|
||||
const queryParams = [...params];
|
||||
console.log('Query params:', queryParams, 'Query:', query);
|
||||
const [flashProducts] = await getDB().execute(query, queryParams);
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
flashProducts,
|
||||
pagination: {
|
||||
page: pageNum,
|
||||
limit: limitNum,
|
||||
total,
|
||||
pages: Math.ceil(total / limitNum)
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
}
|
||||
})
|
||||
|
||||
router.get('/flash-product/:id', auth, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const query = `
|
||||
SELECT id, start_time, end_time, flash_stock, flash_price, products_id
|
||||
FROM flash_product
|
||||
WHERE products_id = ?
|
||||
`;
|
||||
const [flashProduct] = await getDB().execute(query, [id]);
|
||||
if (flashProduct.length === 0) {
|
||||
return res.status(404).json({ error: 'Flash product not found' });
|
||||
}
|
||||
res.json({
|
||||
success: true,
|
||||
data: flashProduct[0]
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
}
|
||||
})
|
||||
|
||||
router.delete('/flash-product/:id', auth, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const query = `
|
||||
DELETE FROM flash_product
|
||||
WHERE products_id = ?
|
||||
`;
|
||||
const [result] = await getDB().execute(query, [id]);
|
||||
if (result.affectedRows === 0) {
|
||||
return res.status(404).json({ error: 'Flash product not found' });
|
||||
}
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Flash product deleted successfully'
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/flash-product', auth, async (req, res) => {
|
||||
try {
|
||||
const { start_time, end_time, flash_stock, flash_price, products_id } = req.body;
|
||||
const query = `
|
||||
INSERT INTO flash_product (start_time, end_time, flash_stock, flash_price, products_id)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`;
|
||||
const [result] = await getDB().execute(query, [start_time, end_time, flash_stock, flash_price, products_id]);
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Flash product created successfully',
|
||||
id: result.insertId
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
}
|
||||
})
|
||||
|
||||
router.put('/flash-product/:id', auth, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { start_time, end_time, flash_stock, flash_price, products_id } = req.body;
|
||||
const query = `
|
||||
UPDATE flash_product
|
||||
SET start_time = ?, end_time = ?, flash_stock = ?, flash_price = ?, products_id = ?
|
||||
WHERE products_id = ?
|
||||
`;
|
||||
const [result] = await getDB().execute(query, [start_time, end_time, flash_stock, flash_price, products_id, id]);
|
||||
if (result.affectedRows === 0) {
|
||||
return res.status(404).json({ error: 'Flash product not found' });
|
||||
}
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Flash product updated successfully'
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
router.get('/recommend-products', auth, async (req, res) => {
|
||||
try {
|
||||
const { page = 1, limit = 10, search = '', category = '' } = 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});
|
||||
|
||||
let whereClause = 'WHERE 1=1';
|
||||
const params = [];
|
||||
|
||||
if (search) {
|
||||
whereClause += ' AND name LIKE ?';
|
||||
params.push(`%${search}%`);
|
||||
}
|
||||
|
||||
if (category) {
|
||||
whereClause += ' AND category = ?';
|
||||
params.push(category);
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
const countQuery = `SELECT COUNT(*) as total FROM flash_product ${whereClause}`;
|
||||
const [countResult] = await getDB().execute(countQuery, params);
|
||||
const total = countResult[0].total;
|
||||
|
||||
// 获取商品列表
|
||||
const query = `
|
||||
SELECT id, products_id
|
||||
FROM recommend_product
|
||||
${whereClause}
|
||||
ORDER BY id DESC
|
||||
LIMIT ${limitNum} OFFSET ${offset}
|
||||
`;
|
||||
|
||||
// 确保参数数组正确传递
|
||||
const queryParams = [...params];
|
||||
console.log('Query params:', queryParams, 'Query:', query);
|
||||
const [recommendProducts] = await getDB().execute(query, queryParams);
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
recommendProducts,
|
||||
pagination: {
|
||||
page: pageNum,
|
||||
limit: limitNum,
|
||||
total,
|
||||
pages: Math.ceil(total / limitNum)
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
}
|
||||
})
|
||||
|
||||
router.post('/recommend-product', auth, async (req, res) => {
|
||||
try {
|
||||
const { products_id } = req.body;
|
||||
const query = `
|
||||
INSERT INTO recommend_product (products_id)
|
||||
VALUES (?)
|
||||
`;
|
||||
const [result] = await getDB().execute(query, [products_id]);
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Recommend product created successfully',
|
||||
id: result.insertId
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
}
|
||||
})
|
||||
|
||||
router.delete('/recommend-product/:id', auth, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const query = `
|
||||
DELETE FROM recommend_product
|
||||
WHERE products_id = ?
|
||||
`;
|
||||
const [result] = await getDB().execute(query, [id]);
|
||||
if (result.affectedRows === 0) {
|
||||
return res.status(404).json({ error: 'Recommend product not found' });
|
||||
}
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Recommend product deleted successfully'
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
}
|
||||
})
|
||||
|
||||
// 供应商
|
||||
router.get('/producer-list', auth, async (req, res) => {
|
||||
try {
|
||||
const { page = 1, limit = 10, search = '', category = '' } = 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});
|
||||
|
||||
let whereClause = 'WHERE 1=1';
|
||||
const params = [];
|
||||
|
||||
if (search) {
|
||||
whereClause += ' AND name LIKE ?';
|
||||
params.push(`%${search}%`);
|
||||
}
|
||||
|
||||
if (category) {
|
||||
whereClause += ' AND category = ?';
|
||||
params.push(category);
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
const countQuery = `SELECT COUNT(*) as total FROM flash_product ${whereClause}`;
|
||||
const [countResult] = await getDB().execute(countQuery, params);
|
||||
const total = countResult[0].total;
|
||||
|
||||
// 获取商品列表
|
||||
const query = `
|
||||
SELECT id, producer_name
|
||||
FROM producer
|
||||
${whereClause}
|
||||
ORDER BY id DESC
|
||||
LIMIT ${limitNum} OFFSET ${offset}
|
||||
`;
|
||||
|
||||
// 确保参数数组正确传递
|
||||
const queryParams = [...params];
|
||||
console.log('Query params:', queryParams, 'Query:', query);
|
||||
const [producerList] = await getDB().execute(query, queryParams);
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
producerList,
|
||||
pagination: {
|
||||
page: pageNum,
|
||||
limit: limitNum,
|
||||
total,
|
||||
pages: Math.ceil(total / limitNum)
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Internal server error', message: error.message });
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router;
|
||||
@@ -249,6 +249,9 @@ app.use('/api/announcements', require('./routes/announcements')); // 通知公
|
||||
app.use('/api/wechat-pay', require('./routes/wechatPay')); // 只保留微信支付
|
||||
app.use('/api/payment', require('./routes/payment'));
|
||||
|
||||
// 商城后台相关接口
|
||||
app.use('/api/shopbackend', require('./routes/shopbackend'));
|
||||
|
||||
// 前端路由 - 必须在最后,作为fallback
|
||||
app.get('/', (req, res) => {
|
||||
res.removeHeader('Origin-Agent-Cluster');
|
||||
|
||||
Reference in New Issue
Block a user