完善订单相关逻辑和商品推荐逻辑,添加接口注释

This commit is contained in:
dzl
2025-10-17 17:26:37 +08:00
parent f9a757046c
commit e535e8a9c5
6 changed files with 619 additions and 103 deletions

View File

@@ -4,6 +4,103 @@ const { auth, adminAuth } = require('../middleware/auth');
const router = express.Router();
/**
* @swagger
* /api/products:
* get:
* summary: 获取商品列表
* description: 返回商品列表,支持分页、搜索、分类、状态过滤
* tags: [products]
* parameters:
* - in: query
* name: page
* schema:
* type: integer
* description: 页码默认1
* - in: query
* name: limit
* schema:
* type: integer
* description: 每页数量默认10最大100
* - in: query
* name: search
* schema:
* type: string
* description: 搜索商品名称
* - in: query
* name: category
* schema:
* type: string
* description: 分类名称
* - in: query
* name: status
* schema:
* type: string
* description: 商品状态active/inactive
* responses:
* 200:
* description: 成功返回商品列表
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: array
* items:
* type: object
* properties:
* id:
* type: string
* example: "1"
* name:
* type: string
* example: "商品A"
* rongdou_price:
* type: number
* example: 100
* points_price:
* type: number
* example: 1000
* stock:
* type: integer
* example: 100
* image:
* type: string
* example: "https://example.com/image.jpg"
* description:
* type: string
* example: "这是一个商品"
* status:
* type: string
* example: "active"
* category_id:
* type: string
* example: "1"
* created_at:
* type: string
* format: date-time
* example: "2023-01-01T00:00:00Z"
* updated_at:
* type: string
* format: date-time
* example: "2023-01-01T00:00:00Z"
* sales:
* type: integer
* example: 100
* images:
* type: array
* items:
* type: string
* example: "https://example.com/image.jpg"
* 400:
* description: 无效的分页参数
* 500:
* description: 服务器内部错误
*/
// 商品管理路由
router.get('/', async (req, res) => {
try {
@@ -396,6 +493,8 @@ router.get('/:id', async (req, res) => {
ORDER BY psc.combination_key`,
[id]
);
// console.log(123,specCombinations);
// 为每个规格组合获取详细的规格值信息
const enhancedSpecifications = [];
@@ -468,6 +567,8 @@ router.get('/:id', async (req, res) => {
});
}
}
// console.log(123,enhancedSpecifications);
// 获取商品属性
const [attributes] = await getDB().execute(
@@ -994,14 +1095,25 @@ router.get('/:id/recommended', async (req, res) => {
// 如果同类别商品不足,补充其他热门商品
if (recommendedProducts.length < 6) {
const recommendQuery = `
let recommendQuery = `
SELECT products_id FROM recommend_product
WHERE products_id NOT IN (${filteredRecommendProductIds.map(() => '?').join(',')})
ORDER BY RAND()
LIMIT ${6 - recommendedProducts.length}
`
const [recommendProductIds] = await getDB().execute(recommendQuery, [...filteredRecommendProductIds]);
WHERE 1 = 1
`;
if (filteredRecommendProductIds.length > 0) {
recommendQuery += ` AND products_id NOT IN (${filteredRecommendProductIds.map(() => '?').join(',')})`;
}
recommendQuery += ` ORDER BY RAND() LIMIT ${6 - recommendedProducts.length}`;
// 根据是否有排除ID来传递参数
const queryParams = filteredRecommendProductIds.length > 0
? [...filteredRecommendProductIds]
: [];
const [recommendProductIds] = await getDB().execute(recommendQuery, queryParams);
filteredRecommendProductIds.push(...recommendProductIds.map(item => item.products_id));
for (const item of recommendProductIds) {
const recommendQuery = `
SELECT id, name, price, points_price as points,
@@ -1012,7 +1124,7 @@ router.get('/:id/recommended', async (req, res) => {
const [recommendProduct] = await getDB().execute(recommendQuery, [item.products_id]);
recommendedProducts.push(recommendProduct[0]);
}
if (recommendProductIds.length + recommendedProducts.length < 6) {
if (recommendProductIds.length < 6) {
// 补充其他热门商品
const additionalQuery = `
SELECT id, name, price, points_price as points,