接口更改
This commit is contained in:
@@ -24,10 +24,10 @@ router.get('/', async (req, res) => {
|
|||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { category_name } = req.body;
|
const { name, level, parent_id } = req.body;
|
||||||
await getDB().execute(
|
await getDB().execute(
|
||||||
'INSERT INTO category (category_name) VALUES (?)',
|
'INSERT INTO category (category_name, level, parent_id) VALUES (?, ?, ?)',
|
||||||
[category_name]
|
[name, level, parent_id === undefined ? null : parent_id]
|
||||||
);
|
);
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -43,6 +43,7 @@ router.put('/:id', async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const { category_name } = req.body;
|
const { category_name } = req.body;
|
||||||
|
console.log("123456", req.body, id);
|
||||||
await getDB().execute(
|
await getDB().execute(
|
||||||
'UPDATE category SET category_name = ? WHERE id = ?',
|
'UPDATE category SET category_name = ? WHERE id = ?',
|
||||||
[category_name, id]
|
[category_name, id]
|
||||||
@@ -61,6 +62,14 @@ router.put('/:id', async (req, res) => {
|
|||||||
router.delete('/:id', async (req, res) => {
|
router.delete('/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
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(
|
await getDB().execute(
|
||||||
'DELETE FROM category WHERE id = ?',
|
'DELETE FROM category WHERE id = ?',
|
||||||
[id]
|
[id]
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ router.get('/', async (req, res) => {
|
|||||||
const product_name = []
|
const product_name = []
|
||||||
for(id of item.products_id){
|
for(id of item.products_id){
|
||||||
const query = `
|
const query = `
|
||||||
SELECT id, name, price, category, description, stock, image_url
|
SELECT id, name, price, description, stock, image_url
|
||||||
FROM products
|
FROM products
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
`;
|
`;
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ router.get('/', async (req, res) => {
|
|||||||
|
|
||||||
// 获取商品列表
|
// 获取商品列表
|
||||||
const query = `
|
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
|
FROM products
|
||||||
${whereClause}
|
${whereClause}
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
@@ -55,13 +55,33 @@ router.get('/', async (req, res) => {
|
|||||||
products.forEach(item=>{
|
products.forEach(item=>{
|
||||||
item.payment_methods = JSON.parse(item.payment_methods)
|
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){
|
for(let item of products){
|
||||||
// console.log("1234567",item)
|
item.category = []
|
||||||
// const categoryQuery = `SELECT category_name FROM category WHERE id = ?`;
|
const [categories] = await getDB().execute(
|
||||||
// const [categoryResult] = await getDB().execute(categoryQuery, [parseInt(item.category)]);
|
`SELECT * FROM products_category WHERE product_id = ?`,
|
||||||
// item.category = categoryResult.category_name;
|
[item.id]
|
||||||
// console.log("qwer",categoryResult)
|
);
|
||||||
parseInt(item.category) === 20 ? console.log('yes') : console.log('no')
|
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({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -86,7 +106,7 @@ router.get('/hot', async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
// 从活跃商品中随机获取2个商品
|
// 从活跃商品中随机获取2个商品
|
||||||
const [products] = await getDB().execute(
|
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,
|
image_url, images, description, shop_name, shop_avatar,
|
||||||
payment_methods, sales, rating, status, created_at, updated_at
|
payment_methods, sales, rating, status, created_at, updated_at
|
||||||
FROM products
|
FROM products
|
||||||
@@ -121,7 +141,7 @@ router.get('/cheap', async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
// 从活跃商品中随机获取2个商品作为秒杀商品
|
// 从活跃商品中随机获取2个商品作为秒杀商品
|
||||||
const [products] = await getDB().execute(
|
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,
|
image_url, images, description, shop_name, shop_avatar,
|
||||||
payment_methods, sales, rating, status, created_at, updated_at
|
payment_methods, sales, rating, status, created_at, updated_at
|
||||||
FROM products
|
FROM products
|
||||||
@@ -162,7 +182,7 @@ router.get('/:id', async (req, res) => {
|
|||||||
const userId = req.user?.id; // 可选的用户ID,用于检查收藏状态
|
const userId = req.user?.id; // 可选的用户ID,用于检查收藏状态
|
||||||
|
|
||||||
const query = `
|
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,
|
image_url, images, videos, description, details, shop_name, shop_avatar,
|
||||||
payment_methods, sales, rating, status, created_at, updated_at
|
payment_methods, sales, rating, status, created_at, updated_at
|
||||||
FROM products
|
FROM products
|
||||||
@@ -177,6 +197,19 @@ router.get('/:id', async (req, res) => {
|
|||||||
|
|
||||||
const product = products[0];
|
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(
|
const [specCombinations] = await getDB().execute(
|
||||||
`SELECT psc.*,
|
`SELECT psc.*,
|
||||||
@@ -351,7 +384,6 @@ router.get('/:id', async (req, res) => {
|
|||||||
// 保持向后兼容
|
// 保持向后兼容
|
||||||
points: product.points_price,
|
points: product.points_price,
|
||||||
image: product.image_url,
|
image: product.image_url,
|
||||||
tags: product.category ? [product.category] : []
|
|
||||||
};
|
};
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
@@ -413,12 +445,12 @@ router.post('/', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 更新商品(管理员权限)
|
// 更新商品
|
||||||
router.put('/:id', async (req, res) => {
|
router.put('/:id', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const productId = req.params.id;
|
const productId = req.params.id;
|
||||||
const {
|
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,
|
image_url, images, videos, details, status, shop_name, shop_avatar, payment_methods,
|
||||||
specifications, attributes
|
specifications, attributes
|
||||||
} = req.body;
|
} = req.body;
|
||||||
@@ -467,11 +499,6 @@ router.put('/:id', async (req, res) => {
|
|||||||
updateValues.push(stock);
|
updateValues.push(stock);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (category !== undefined) {
|
|
||||||
updateFields.push('category = ?');
|
|
||||||
updateValues.push(category);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (image_url !== undefined) {
|
if (image_url !== undefined) {
|
||||||
updateFields.push('image_url = ?');
|
updateFields.push('image_url = ?');
|
||||||
updateValues.push(image_url);
|
updateValues.push(image_url);
|
||||||
@@ -543,6 +570,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({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: '商品更新成功'
|
message: '商品更新成功'
|
||||||
@@ -718,10 +767,9 @@ router.get('/:id/recommended', async (req, res) => {
|
|||||||
|
|
||||||
// 获取同类别的其他商品作为推荐
|
// 获取同类别的其他商品作为推荐
|
||||||
const query = `
|
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
|
p2.stock, p2.image_url as image, p2.description
|
||||||
FROM products p1
|
FROM products p1
|
||||||
JOIN products p2 ON p1.category = p2.category
|
|
||||||
WHERE p1.id = ? AND p2.id != ? AND p2.status = 'active'
|
WHERE p1.id = ? AND p2.id != ? AND p2.status = 'active'
|
||||||
ORDER BY RAND()
|
ORDER BY RAND()
|
||||||
LIMIT 6
|
LIMIT 6
|
||||||
@@ -734,7 +782,7 @@ router.get('/:id/recommended', async (req, res) => {
|
|||||||
const remainingCount = 6 - recommendedProducts.length;
|
const remainingCount = 6 - recommendedProducts.length;
|
||||||
if (remainingCount > 0) {
|
if (remainingCount > 0) {
|
||||||
const additionalQuery = `
|
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
|
stock, image_url as image, description
|
||||||
FROM products
|
FROM products
|
||||||
WHERE id != ? AND status = 'active'
|
WHERE id != ? AND status = 'active'
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ router.get('/product-details/:id', async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const query = `
|
const query = `
|
||||||
SELECT id, name, price, category, description, stock, image_url
|
SELECT id, name, price, description, stock, image_url
|
||||||
FROM products
|
FROM products
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
`;
|
`;
|
||||||
|
|||||||
Reference in New Issue
Block a user