修改商城逻辑
This commit is contained in:
407
routes/orders.js
407
routes/orders.js
@@ -4,6 +4,55 @@ const { auth, adminAuth } = require('../middleware/auth');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* tags:
|
||||
* name: Orders
|
||||
* description: 订单管理相关接口
|
||||
*/
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* components:
|
||||
* schemas:
|
||||
* Order:
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
* description: 订单ID
|
||||
* order_no:
|
||||
* type: string
|
||||
* description: 订单编号
|
||||
* user_id:
|
||||
* type: integer
|
||||
* description: 用户ID
|
||||
* total_amount:
|
||||
* type: number
|
||||
* description: 订单总金额
|
||||
* total_points:
|
||||
* type: number
|
||||
* description: 订单总积分
|
||||
* status:
|
||||
* type: string
|
||||
* enum: [pending, shipped, completed, cancelled]
|
||||
* description: 订单状态
|
||||
* address:
|
||||
* type: string
|
||||
* description: 收货地址
|
||||
* created_at:
|
||||
* type: string
|
||||
* format: date-time
|
||||
* description: 创建时间
|
||||
* updated_at:
|
||||
* type: string
|
||||
* format: date-time
|
||||
* description: 更新时间
|
||||
* username:
|
||||
* type: string
|
||||
* description: 用户名
|
||||
*/
|
||||
|
||||
// 生成订单号
|
||||
function generateOrderNo() {
|
||||
const timestamp = Date.now().toString();
|
||||
@@ -11,7 +60,93 @@ function generateOrderNo() {
|
||||
return `ORD${timestamp}${random}`.toUpperCase();
|
||||
}
|
||||
|
||||
// 获取订单列表
|
||||
/**
|
||||
* @swagger
|
||||
* /api/orders:
|
||||
* get:
|
||||
* summary: 获取订单列表
|
||||
* tags: [Orders]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: page
|
||||
* schema:
|
||||
* type: integer
|
||||
* default: 1
|
||||
* description: 页码
|
||||
* - in: query
|
||||
* name: limit
|
||||
* schema:
|
||||
* type: integer
|
||||
* default: 10
|
||||
* description: 每页数量
|
||||
* - in: query
|
||||
* name: search
|
||||
* schema:
|
||||
* type: string
|
||||
* description: 搜索关键词(订单号或用户名)
|
||||
* - in: query
|
||||
* name: orderNumber
|
||||
* schema:
|
||||
* type: string
|
||||
* description: 订单号
|
||||
* - in: query
|
||||
* name: username
|
||||
* schema:
|
||||
* type: string
|
||||
* description: 用户名
|
||||
* - in: query
|
||||
* name: status
|
||||
* schema:
|
||||
* type: string
|
||||
* enum: [pending, shipped, completed, cancelled]
|
||||
* description: 订单状态
|
||||
* - in: query
|
||||
* name: startDate
|
||||
* schema:
|
||||
* type: string
|
||||
* format: date
|
||||
* description: 开始日期
|
||||
* - in: query
|
||||
* name: endDate
|
||||
* schema:
|
||||
* type: string
|
||||
* format: date
|
||||
* description: 结束日期
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 成功获取订单列表
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* data:
|
||||
* type: object
|
||||
* properties:
|
||||
* orders:
|
||||
* type: array
|
||||
* items:
|
||||
* $ref: '#/components/schemas/Order'
|
||||
* pagination:
|
||||
* type: object
|
||||
* properties:
|
||||
* page:
|
||||
* type: integer
|
||||
* limit:
|
||||
* type: integer
|
||||
* total:
|
||||
* type: integer
|
||||
* pages:
|
||||
* type: integer
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.get('/', auth, async (req, res) => {
|
||||
try {
|
||||
const { page = 1, limit = 10, search = '', orderNumber = '', username = '', status = '', startDate = '', endDate = '' } = req.query;
|
||||
@@ -109,7 +244,43 @@ router.get('/', auth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 获取单个订单详情
|
||||
/**
|
||||
* @swagger
|
||||
* /api/orders/{id}:
|
||||
* get:
|
||||
* summary: 获取单个订单详情
|
||||
* tags: [Orders]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: 订单ID
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 成功获取订单详情
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* data:
|
||||
* type: object
|
||||
* properties:
|
||||
* order:
|
||||
* $ref: '#/components/schemas/Order'
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 404:
|
||||
* description: 订单不存在
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.get('/:id', auth, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
@@ -150,7 +321,64 @@ router.get('/:id', auth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 创建订单
|
||||
/**
|
||||
* @swagger
|
||||
* /api/orders:
|
||||
* post:
|
||||
* summary: 创建订单
|
||||
* tags: [Orders]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* product_id:
|
||||
* type: integer
|
||||
* description: 商品ID
|
||||
* quantity:
|
||||
* type: integer
|
||||
* description: 购买数量
|
||||
* shipping_address:
|
||||
* type: string
|
||||
* description: 收货地址
|
||||
* required:
|
||||
* - product_id
|
||||
* - quantity
|
||||
* - shipping_address
|
||||
* responses:
|
||||
* 201:
|
||||
* description: 订单创建成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* message:
|
||||
* type: string
|
||||
* data:
|
||||
* type: object
|
||||
* properties:
|
||||
* orderId:
|
||||
* type: integer
|
||||
* orderNumber:
|
||||
* type: string
|
||||
* pointsUsed:
|
||||
* type: integer
|
||||
* 400:
|
||||
* description: 参数错误或积分不足
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 404:
|
||||
* description: 商品不存在或已下架
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.post('/', auth, async (req, res) => {
|
||||
const db = getDB();
|
||||
await db.query('START TRANSACTION');
|
||||
@@ -253,7 +481,42 @@ router.post('/', auth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 用户取消订单
|
||||
/**
|
||||
* @swagger
|
||||
* /api/orders/{id}/cancel:
|
||||
* put:
|
||||
* summary: 用户取消订单
|
||||
* tags: [Orders]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: 订单ID
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 订单取消成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* message:
|
||||
* type: string
|
||||
* 400:
|
||||
* description: 只能取消待处理的订单
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 404:
|
||||
* description: 订单不存在
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.put('/:id/cancel', auth, async (req, res) => {
|
||||
const db = getDB();
|
||||
await db.query('START TRANSACTION');
|
||||
@@ -310,7 +573,42 @@ router.put('/:id/cancel', auth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 确认收货
|
||||
/**
|
||||
* @swagger
|
||||
* /api/orders/{id}/confirm:
|
||||
* put:
|
||||
* summary: 确认收货
|
||||
* tags: [Orders]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: 订单ID
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 确认收货成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* message:
|
||||
* type: string
|
||||
* 400:
|
||||
* description: 只能确认已发货的订单
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 404:
|
||||
* description: 订单不存在
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.put('/:id/confirm', auth, async (req, res) => {
|
||||
try {
|
||||
const orderId = req.params.id;
|
||||
@@ -345,7 +643,57 @@ router.put('/:id/confirm', auth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 更新订单状态(管理员)
|
||||
/**
|
||||
* @swagger
|
||||
* /api/orders/{id}/status:
|
||||
* put:
|
||||
* summary: 更新订单状态(管理员)
|
||||
* tags: [Orders]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
* required: true
|
||||
* schema:
|
||||
* type: integer
|
||||
* description: 订单ID
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* status:
|
||||
* type: string
|
||||
* enum: [pending, shipped, completed, cancelled]
|
||||
* description: 订单状态
|
||||
* required:
|
||||
* - status
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 订单状态更新成功
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* message:
|
||||
* type: string
|
||||
* 400:
|
||||
* description: 无效的订单状态
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 403:
|
||||
* description: 无管理员权限
|
||||
* 404:
|
||||
* description: 订单不存在
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.put('/:id/status', auth, adminAuth, async (req, res) => {
|
||||
const db = getDB();
|
||||
await db.query('START TRANSACTION');
|
||||
@@ -406,7 +754,52 @@ router.put('/:id/status', auth, adminAuth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 获取订单统计信息(管理员权限)
|
||||
/**
|
||||
* @swagger
|
||||
* /api/orders/stats:
|
||||
* get:
|
||||
* summary: 获取订单统计信息(管理员权限)
|
||||
* tags: [Orders]
|
||||
* security:
|
||||
* - bearerAuth: []
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 成功获取订单统计信息
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* success:
|
||||
* type: boolean
|
||||
* data:
|
||||
* type: object
|
||||
* properties:
|
||||
* totalOrders:
|
||||
* type: integer
|
||||
* description: 总订单数
|
||||
* pendingOrders:
|
||||
* type: integer
|
||||
* description: 待发货订单数
|
||||
* completedOrders:
|
||||
* type: integer
|
||||
* description: 已完成订单数
|
||||
* monthOrders:
|
||||
* type: integer
|
||||
* description: 本月新增订单数
|
||||
* monthGrowthRate:
|
||||
* type: number
|
||||
* description: 月增长率
|
||||
* totalPointsConsumed:
|
||||
* type: number
|
||||
* description: 总积分消费
|
||||
* 401:
|
||||
* description: 未授权
|
||||
* 403:
|
||||
* description: 无管理员权限
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
router.get('/stats', auth, adminAuth, async (req, res) => {
|
||||
try {
|
||||
// 总订单数
|
||||
|
||||
Reference in New Issue
Block a user