新增优惠卷管理和图片上传
This commit is contained in:
@@ -7,25 +7,17 @@ router.get('/', async (req, res) => {
|
||||
const db = await getDB();
|
||||
const [firstCategory] = await db.query('SELECT * FROM category WHERE level = 1');
|
||||
|
||||
console.log(firstCategory);
|
||||
|
||||
for (const category of firstCategory) {
|
||||
const [secondCategories] = await db.query('SELECT * FROM category WHERE parent_id = ?', [category.id]);
|
||||
category.relative = secondCategories;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
res.json({ success: true, data: firstCategory.map(category => ({
|
||||
name: category.category_name,
|
||||
relative: category.relative.map(secondCategory => ({
|
||||
name: secondCategory.category_name,
|
||||
img: secondCategory.image,
|
||||
}))
|
||||
}))});
|
||||
} catch (error) {
|
||||
|
||||
@@ -68,4 +68,37 @@ router.get('/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/user/:id', async (req, res) => {
|
||||
try {
|
||||
const db = getDB();
|
||||
const userId = req.params.id;
|
||||
|
||||
const query = `
|
||||
SELECT * FROM coupon_use WHERE user_id = ?
|
||||
`
|
||||
const [coupon] = await db.query(query, [userId]);
|
||||
|
||||
for (const item of coupon) {
|
||||
const query = `
|
||||
SELECT * FROM coupon_products WHERE id = ?
|
||||
`
|
||||
const [couponInfo] = await db.query(query, [item.coupon_id]);
|
||||
item.couponInfo = couponInfo[0];
|
||||
item.couponInfo.products = [];
|
||||
for (const product of item.couponInfo.products_id) {
|
||||
const query = `
|
||||
SELECT name FROM products WHERE id = ?
|
||||
`
|
||||
const [productInfo] = await db.query(query, [product]);
|
||||
item.couponInfo.products.push(productInfo[0]);
|
||||
}
|
||||
}
|
||||
|
||||
res.json({success: true, coupon});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).json({ error: '加载优惠券失败' });
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router;
|
||||
@@ -973,7 +973,6 @@ router.get('/:id/recommended', async (req, res) => {
|
||||
SELECT * FROM products_category WHERE category_id IN (${categoryIds.map(() => '?').join(',')})
|
||||
`;
|
||||
const [recommendProductIds] = await getDB().execute(recommendId, categoryIds);
|
||||
console.log(1234, recommendProductIds);
|
||||
|
||||
filteredRecommendProductIds = [...new Set(recommendProductIds.map(item => item.product_id))];
|
||||
|
||||
@@ -993,20 +992,42 @@ router.get('/:id/recommended', async (req, res) => {
|
||||
|
||||
// 如果同类别商品不足,补充其他热门商品
|
||||
if (recommendedProducts.length < 6) {
|
||||
console.log(123456, filteredRecommendProductIds);
|
||||
// const remainingCount = 6 - recommendedProducts.length;
|
||||
const additionalQuery = `
|
||||
SELECT id, name, price, points_price as points,
|
||||
stock, image_url as image, description
|
||||
FROM products
|
||||
WHERE id != 3
|
||||
|
||||
const recommendQuery = `
|
||||
SELECT products_id FROM recommend_product
|
||||
WHERE products_id NOT IN (${filteredRecommendProductIds.map(() => '?').join(',')})
|
||||
ORDER BY RAND()
|
||||
LIMIT ${6 - recommendedProducts.length}
|
||||
`;
|
||||
const [additionalProducts] = await getDB().execute(
|
||||
additionalQuery,
|
||||
);
|
||||
recommendedProducts.push(...additionalProducts);
|
||||
`
|
||||
const [recommendProductIds] = await getDB().execute(recommendQuery, [...filteredRecommendProductIds]);
|
||||
filteredRecommendProductIds.push(...recommendProductIds.map(item => item.products_id));
|
||||
for (const item of recommendProductIds) {
|
||||
const recommendQuery = `
|
||||
SELECT id, name, price, points_price as points,
|
||||
stock, image_url as image, description
|
||||
FROM products
|
||||
WHERE id = ?
|
||||
`;
|
||||
const [recommendProduct] = await getDB().execute(recommendQuery, [item.products_id]);
|
||||
recommendedProducts.push(recommendProduct[0]);
|
||||
}
|
||||
if (recommendProductIds.length + recommendedProducts.length < 6) {
|
||||
// 补充其他热门商品
|
||||
const additionalQuery = `
|
||||
SELECT id, name, price, points_price as points,
|
||||
stock, image_url as image, description
|
||||
FROM products
|
||||
WHERE id NOT IN (${filteredRecommendProductIds.map(() => '?').join(',')})
|
||||
AND id != 3
|
||||
ORDER BY RAND()
|
||||
LIMIT ${6 - recommendedProducts.length}
|
||||
`;
|
||||
const [additionalProducts] = await getDB().execute(
|
||||
additionalQuery,
|
||||
filteredRecommendProductIds
|
||||
);
|
||||
recommendedProducts.push(...additionalProducts);
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
|
||||
Reference in New Issue
Block a user