修改商城逻辑

This commit is contained in:
2025-08-28 09:14:56 +08:00
parent a1944a573e
commit 691789d5d3
28 changed files with 10842 additions and 292 deletions

View File

@@ -3,7 +3,70 @@ const router = express.Router();
const { getDB } = require('../database');
const { auth, adminAuth } = require('../middleware/auth');
// 获取用户当前积分
/**
* @swagger
* tags:
* name: Points
* description: 积分管理相关接口
*/
/**
* @swagger
* components:
* schemas:
* PointsHistory:
* type: object
* properties:
* id:
* type: integer
* description: 积分历史记录ID
* points_change:
* type: integer
* description: 积分变动数量
* type:
* type: string
* description: 积分变动类型(earn-获得, spend-消费, admin_adjust-管理员调整)
* description:
* type: string
* description: 积分变动描述
* created_at:
* type: string
* format: date-time
* description: 创建时间
*/
/**
* @swagger
* /api/points/balance:
* get:
* summary: 获取用户当前积分余额
* tags: [Points]
* security:
* - bearerAuth: []
* responses:
* 200:
* description: 成功获取积分余额
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: object
* properties:
* points:
* type: integer
* description: 用户当前积分
* 401:
* description: 未授权,需要登录
* 404:
* description: 用户不存在
* 500:
* description: 服务器错误
*/
router.get('/balance', auth, async (req, res) => {
try {
const userId = req.user.id;
@@ -29,7 +92,90 @@ router.get('/balance', auth, async (req, res) => {
}
});
// 获取用户积分历史记录
/**
* @swagger
* /api/points/history:
* get:
* summary: 获取用户积分历史记录
* tags: [Points]
* 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: type
* schema:
* type: string
* enum: [earn, spend, admin_adjust]
* description: 积分变动类型
* - in: query
* name: username
* schema:
* type: string
* description: 用户名(仅管理员可用)
* - in: query
* name: change
* schema:
* type: string
* enum: [positive, negative]
* 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
* example: true
* data:
* type: object
* properties:
* records:
* type: array
* items:
* $ref: '#/components/schemas/PointsHistory'
* pagination:
* type: object
* properties:
* page:
* type: integer
* limit:
* type: integer
* total:
* type: integer
* totalPages:
* type: integer
* 401:
* description: 未授权,需要登录
* 500:
* description: 服务器错误
*/
router.get('/history', auth, async (req, res) => {
try {
const { page = 1, limit = 10, type, username, change, startDate, endDate } = req.query;
@@ -135,7 +281,66 @@ router.get('/history', auth, async (req, res) => {
}
});
// 管理员调整用户积分
/**
* @swagger
* /api/points/adjust:
* post:
* summary: 管理员调整用户积分
* tags: [Points]
* security:
* - bearerAuth: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - userId
* - points
* - reason
* properties:
* userId:
* type: integer
* description: 用户ID
* points:
* type: integer
* description: 调整的积分数量(正数为增加,负数为减少)
* reason:
* type: string
* description: 调整原因
* responses:
* 200:
* description: 积分调整成功
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* message:
* type: string
* example: 积分调整成功
* data:
* type: object
* properties:
* userId:
* type: integer
* pointsChanged:
* type: integer
* newBalance:
* type: integer
* 400:
* description: 参数错误或积分不足
* 401:
* description: 未授权,需要管理员权限
* 404:
* description: 用户不存在
* 500:
* description: 服务器错误
*/
router.post('/adjust', auth, adminAuth, async (req, res) => {
const connection = await getDB().getConnection();
@@ -202,7 +407,64 @@ router.post('/adjust', auth, adminAuth, async (req, res) => {
}
});
// 管理员给用户充值积分
/**
* @swagger
* /api/points/recharge:
* post:
* summary: 管理员给用户充值积分
* tags: [Points]
* security:
* - bearerAuth: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - user_id
* - points
* properties:
* user_id:
* type: integer
* description: 用户ID
* points:
* type: integer
* description: 充值的积分数量(必须为正数)
* description:
* type: string
* description: 充值描述
* default: 管理员充值
* responses:
* 200:
* description: 积分充值成功
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* message:
* type: string
* example: 积分充值成功
* data:
* type: object
* properties:
* userId:
* type: integer
* pointsAdded:
* type: integer
* 400:
* description: 参数错误
* 401:
* description: 未授权,需要管理员权限
* 404:
* description: 用户不存在
* 500:
* description: 服务器错误
*/
router.post('/recharge', auth, adminAuth, async (req, res) => {
const connection = await getDB().getConnection();
@@ -261,7 +523,53 @@ router.post('/recharge', auth, adminAuth, async (req, res) => {
// 获取积分排行榜
/**
* @swagger
* /api/points/leaderboard:
* get:
* summary: 获取积分排行榜
* tags: [Points]
* security:
* - bearerAuth: []
* parameters:
* - in: query
* name: limit
* schema:
* type: integer
* default: 10
* description: 返回的排行榜数量
* responses:
* 200:
* description: 成功获取积分排行榜
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: object
* properties:
* leaderboard:
* type: array
* items:
* type: object
* properties:
* rank:
* type: integer
* userId:
* type: integer
* username:
* type: string
* points:
* type: integer
* 401:
* description: 未授权,需要登录
* 500:
* description: 服务器错误
*/
router.get('/leaderboard', auth, async (req, res) => {
try {
const { limit = 10 } = req.query;
@@ -292,7 +600,48 @@ router.get('/leaderboard', auth, async (req, res) => {
}
});
// 获取积分统计信息(管理员权限)
/**
* @swagger
* /api/points/stats:
* get:
* summary: 获取积分统计信息(管理员权限)
* tags: [Points]
* security:
* - bearerAuth: []
* responses:
* 200:
* description: 成功获取积分统计信息
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: object
* properties:
* stats:
* type: object
* properties:
* totalPoints:
* type: integer
* description: 系统中总积分数量
* totalEarned:
* type: integer
* description: 总积分发放量
* totalSpent:
* type: integer
* description: 总积分消费量
* activeUsers:
* type: integer
* description: 活跃用户数
* 401:
* description: 未授权,需要管理员权限
* 500:
* description: 服务器错误
*/
router.get('/stats', auth, adminAuth, async (req, res) => {
try {
// 总积分发放量