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