Compare commits
2 Commits
5d50c42e3e
...
077686a0fc
| Author | SHA1 | Date | |
|---|---|---|---|
| 077686a0fc | |||
| 305ac5dde6 |
@@ -104,7 +104,7 @@ router.get('/hot', async (req, res) => {
|
||||
payment_methods, sales, rating, status, created_at, updated_at
|
||||
FROM products
|
||||
WHERE status = 'active' AND stock > 0
|
||||
ORDER BY RAND()
|
||||
ORDER BY sales DESC
|
||||
LIMIT 2`
|
||||
);
|
||||
|
||||
@@ -156,36 +156,43 @@ router.get('/hot', async (req, res) => {
|
||||
*/
|
||||
router.get('/cheap', async (req, res) => {
|
||||
try {
|
||||
// 从活跃商品中随机获取2个商品作为秒杀商品
|
||||
|
||||
const [products] = await getDB().execute(
|
||||
`SELECT id, name, category, 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
|
||||
WHERE status = 'active' AND stock > 0
|
||||
`SELECT id, start_time, end_time, flash_stock, flash_price, products_id
|
||||
FROM flash_product
|
||||
WHERE end_time > NOW() AND flash_stock > 0
|
||||
ORDER BY RAND()
|
||||
LIMIT 2`
|
||||
);
|
||||
|
||||
// 格式化商品数据,为秒杀商品添加特殊标识
|
||||
const formattedProducts = products.map(product => ({
|
||||
...product,
|
||||
images: product.images ? JSON.parse(product.images) : (product.image_url ? [product.image_url] : []),
|
||||
payment_methods: product.payment_methods ? JSON.parse(product.payment_methods) : ['points'],
|
||||
// 秒杀商品特殊处理:价格打8折
|
||||
flash_sale_price: Math.floor(product.price * 0.8),
|
||||
flash_sale_points: Math.floor(product.points_price * 0.8),
|
||||
flash_sale_rongdou: Math.floor(product.rongdou_price * 0.8),
|
||||
is_flash_sale: true,
|
||||
// 保持向后兼容
|
||||
points: product.points_price,
|
||||
image: product.image_url
|
||||
}));
|
||||
|
||||
const tempProducts = await Promise.all(products.map(async item=>{
|
||||
|
||||
const [product] = await getDB().execute(
|
||||
`SELECT id, name, category, 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
|
||||
WHERE id = ?`,
|
||||
[item.products_id]
|
||||
)
|
||||
|
||||
item = {
|
||||
...product[0],
|
||||
images: product.images ? JSON.parse(product.images) : (product.image_url ? [product.image_url] : []),
|
||||
payment_methods: product.payment_methods ? JSON.parse(product.payment_methods) : ['points'],
|
||||
...item,
|
||||
points: product.points_price,
|
||||
image: product.image_url,
|
||||
id: product[0].id,
|
||||
}
|
||||
console.log("222",item)
|
||||
return item
|
||||
}))
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
products: formattedProducts
|
||||
products: tempProducts
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user