接口更改

This commit is contained in:
dzl
2025-10-11 17:31:25 +08:00
parent 2aa0e9d884
commit 9b2d5c0ce2
4 changed files with 111 additions and 54 deletions

View File

@@ -23,47 +23,56 @@ router.get('/', async (req, res) => {
});
router.post('/', async (req, res) => {
try {
const { category_name } = req.body;
await getDB().execute(
'INSERT INTO category (category_name) VALUES (?)',
[category_name]
);
res.json({
success: true,
message: '商品分类创建成功'
});
} catch (error) {
console.error('创建商品分类失败:', error);
res.status(500).json({ success: false, message: '创建商品分类失败' });
}
try {
const { name, level, parent_id } = req.body;
await getDB().execute(
'INSERT INTO category (category_name, level, parent_id) VALUES (?, ?, ?)',
[name, level, parent_id === undefined ? null : parent_id]
);
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 { category_name } = req.body;
await getDB().execute(
'UPDATE category SET category_name = ? WHERE id = ?',
[category_name, id]
);
res.json({
success: true,
message: '商品分类更新成功'
});
} catch (error) {
console.error('更新商品分类失败:', error);
res.status(500).json({ success: false, message: '更新商品分类失败' });
}
try {
const { id } = req.params;
const { category_name } = req.body;
console.log("123456", req.body, id);
await getDB().execute(
'UPDATE category SET category_name = ? WHERE id = ?',
[category_name, id]
);
res.json({
success: true,
message: '商品分类更新成功'
});
} catch (error) {
console.error('更新商品分类失败:', error);
res.status(500).json({ success: false, message: '更新商品分类失败' });
}
})
// 删除商品分类
router.delete('/:id', async (req, res) => {
try {
const { id } = req.params;
const [categories] = await getDB().execute(
'SELECT * FROM category WHERE parent_id = ?',
[id]
);
if (categories.length > 0) {
res.status(400).json({ success: false, message: '该分类下存在子分类,不能删除' });
return;
}
await getDB().execute(
'DELETE FROM category WHERE id = ?',
[id]
'DELETE FROM category WHERE id = ?',
[id]
);
res.json({

View File

@@ -38,7 +38,7 @@ router.get('/', async (req, res) => {
const product_name = []
for(id of item.products_id){
const query = `
SELECT id, name, price, category, description, stock, image_url
SELECT id, name, price, description, stock, image_url
FROM products
WHERE id = ?
`;

View File

@@ -42,7 +42,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, sales
SELECT id, name, rongdou_price, points_price, stock, image_url as image, description, status, payment_methods, created_at, updated_at, sales
FROM products
${whereClause}
ORDER BY created_at DESC
@@ -55,13 +55,33 @@ router.get('/', async (req, res) => {
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
// }
// }
for(let item of products){
// console.log("1234567",item)
// const categoryQuery = `SELECT category_name FROM category WHERE id = ?`;
// const [categoryResult] = await getDB().execute(categoryQuery, [parseInt(item.category)]);
// item.category = categoryResult.category_name;
// console.log("qwer",categoryResult)
parseInt(item.category) === 20 ? console.log('yes') : console.log('no')
item.category = []
const [categories] = await getDB().execute(
`SELECT * FROM products_category WHERE product_id = ?`,
[item.id]
);
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)
}
}
res.json({
success: true,
@@ -86,7 +106,7 @@ router.get('/hot', async (req, res) => {
try {
// 从活跃商品中随机获取2个商品
const [products] = await getDB().execute(
`SELECT id, name, category, price, points_price, rongdou_price, stock,
`SELECT id, name, price, points_price, rongdou_price, stock,
image_url, images, description, shop_name, shop_avatar,
payment_methods, sales, rating, status, created_at, updated_at
FROM products
@@ -121,7 +141,7 @@ router.get('/cheap', async (req, res) => {
try {
// 从活跃商品中随机获取2个商品作为秒杀商品
const [products] = await getDB().execute(
`SELECT id, name, category, price, points_price, rongdou_price, stock,
`SELECT id, name, price, points_price, rongdou_price, stock,
image_url, images, description, shop_name, shop_avatar,
payment_methods, sales, rating, status, created_at, updated_at
FROM products
@@ -162,7 +182,7 @@ router.get('/:id', async (req, res) => {
const userId = req.user?.id; // 可选的用户ID用于检查收藏状态
const query = `
SELECT id, name, category, price, points_price, rongdou_price, stock,
SELECT id, name, price, points_price, rongdou_price, stock,
image_url, images, videos, description, details, shop_name, shop_avatar,
payment_methods, sales, rating, status, created_at, updated_at
FROM products
@@ -176,6 +196,19 @@ router.get('/:id', async (req, res) => {
}
const product = products[0];
const [categories] = await getDB().execute(
`SELECT * FROM products_category WHERE product_id = ?`,
[id]
);
for(let item of categories){
const category = await getDB().execute(
`SELECT * FROM category WHERE id = ?`,
[item.category_id]
);
item.category_name = category[0][0].category_name;
}
product.category = categories;
// 获取商品的规格组合(新的笛卡尔积规格系统)
const [specCombinations] = await getDB().execute(
@@ -351,7 +384,6 @@ router.get('/:id', async (req, res) => {
// 保持向后兼容
points: product.points_price,
image: product.image_url,
tags: product.category ? [product.category] : []
};
res.json({
@@ -413,12 +445,12 @@ router.post('/', async (req, res) => {
}
});
// 更新商品(管理员权限)
// 更新商品
router.put('/:id', async (req, res) => {
try {
const productId = req.params.id;
const {
name, description, price, points_price, rongdou_price, stock, category,
name, description, price, points_price, rongdou_price, stock, category, level,
image_url, images, videos, details, status, shop_name, shop_avatar, payment_methods,
specifications, attributes
} = req.body;
@@ -467,11 +499,6 @@ router.put('/:id', async (req, res) => {
updateValues.push(stock);
}
if (category !== undefined) {
updateFields.push('category = ?');
updateValues.push(category);
}
if (image_url !== undefined) {
updateFields.push('image_url = ?');
updateValues.push(image_url);
@@ -542,6 +569,28 @@ router.put('/:id', async (req, res) => {
}
}
}
if(category) {
const [existCategorys] = await getDB().execute(
`SELECT * FROM products_category WHERE product_id = ?`,
[productId]
);
if(existCategorys.length > 0){
await getDB().execute(
`DELETE FROM products_category WHERE product_id = ?`,
[productId]
);
}
for (const id of category) {
await getDB().execute(
`INSERT INTO products_category (product_id, category_id, create_time)
VALUES (?, ?, NOW())`,
[productId, id]
);
}
}
res.json({
success: true,
@@ -718,10 +767,9 @@ router.get('/:id/recommended', async (req, res) => {
// 获取同类别的其他商品作为推荐
const query = `
SELECT p2.id, p2.name, p2.category, p2.price, p2.points_price as points,
SELECT p2.id, p2.name, p2.price, p2.points_price as points,
p2.stock, p2.image_url as image, p2.description
FROM products p1
JOIN products p2 ON p1.category = p2.category
WHERE p1.id = ? AND p2.id != ? AND p2.status = 'active'
ORDER BY RAND()
LIMIT 6
@@ -734,7 +782,7 @@ router.get('/:id/recommended', async (req, res) => {
const remainingCount = 6 - recommendedProducts.length;
if (remainingCount > 0) {
const additionalQuery = `
SELECT id, name, category, price, points_price as points,
SELECT id, name, price, points_price as points,
stock, image_url as image, description
FROM products
WHERE id != ? AND status = 'active'

View File

@@ -20,7 +20,7 @@ router.get('/product-details/:id', async (req, res) => {
try {
const { id } = req.params;
const query = `
SELECT id, name, price, category, description, stock, image_url
SELECT id, name, price, description, stock, image_url
FROM products
WHERE id = ?
`;