2025-09-02 21:59:27 +08:00
|
|
|
|
const express = require('express')
|
|
|
|
|
|
const bcrypt = require('bcryptjs')
|
|
|
|
|
|
const Joi = require('joi')
|
|
|
|
|
|
const router = express.Router()
|
|
|
|
|
|
|
2025-09-05 01:18:40 +08:00
|
|
|
|
// 引入数据库模型
|
2025-09-19 00:42:14 +08:00
|
|
|
|
const { Admin } = require('../models')
|
2025-09-05 01:18:40 +08:00
|
|
|
|
const sequelize = require('sequelize')
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
2025-09-12 13:15:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @swagger
|
|
|
|
|
|
* components:
|
|
|
|
|
|
* schemas:
|
|
|
|
|
|
* User:
|
|
|
|
|
|
* type: object
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* id:
|
|
|
|
|
|
* type: integer
|
|
|
|
|
|
* description: 用户ID
|
|
|
|
|
|
* username:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 用户名
|
|
|
|
|
|
* email:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* format: email
|
|
|
|
|
|
* description: 邮箱
|
|
|
|
|
|
* phone:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 手机号
|
|
|
|
|
|
* user_type:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* enum: [admin, buyer, supplier, trader]
|
|
|
|
|
|
* description: 用户类型
|
|
|
|
|
|
* status:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* enum: [active, inactive, suspended]
|
|
|
|
|
|
* description: 用户状态
|
|
|
|
|
|
* createdAt:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* format: date-time
|
|
|
|
|
|
* description: 创建时间
|
|
|
|
|
|
* updatedAt:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* format: date-time
|
|
|
|
|
|
* description: 更新时间
|
|
|
|
|
|
* CreateUserRequest:
|
|
|
|
|
|
* type: object
|
|
|
|
|
|
* required:
|
|
|
|
|
|
* - username
|
|
|
|
|
|
* - email
|
|
|
|
|
|
* - password
|
|
|
|
|
|
* - user_type
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* username:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 用户名
|
|
|
|
|
|
* email:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* format: email
|
|
|
|
|
|
* description: 邮箱
|
|
|
|
|
|
* phone:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 手机号
|
|
|
|
|
|
* password:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 密码
|
|
|
|
|
|
* user_type:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* enum: [admin, buyer, supplier, trader]
|
|
|
|
|
|
* description: 用户类型
|
|
|
|
|
|
* status:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* enum: [active, inactive, suspended]
|
|
|
|
|
|
* description: 用户状态
|
|
|
|
|
|
* UpdateUserRequest:
|
|
|
|
|
|
* type: object
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* username:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 用户名
|
|
|
|
|
|
* email:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* format: email
|
|
|
|
|
|
* description: 邮箱
|
|
|
|
|
|
* phone:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 手机号
|
|
|
|
|
|
* user_type:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* enum: [admin, buyer, supplier, trader]
|
|
|
|
|
|
* description: 用户类型
|
|
|
|
|
|
* status:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* enum: [active, inactive, suspended]
|
|
|
|
|
|
* description: 用户状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-09-02 21:59:27 +08:00
|
|
|
|
// 验证模式
|
|
|
|
|
|
const createUserSchema = Joi.object({
|
|
|
|
|
|
username: Joi.string().min(2).max(50).required(),
|
|
|
|
|
|
email: Joi.string().email().required(),
|
|
|
|
|
|
phone: Joi.string().pattern(/^1[3-9]\d{9}$/).allow(''),
|
|
|
|
|
|
password: Joi.string().min(6).max(100).required(),
|
2025-09-12 13:15:03 +08:00
|
|
|
|
user_type: Joi.string().valid('admin', 'buyer', 'supplier', 'trader').required(),
|
|
|
|
|
|
status: Joi.string().valid('active', 'inactive', 'suspended').default('active')
|
2025-09-02 21:59:27 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const updateUserSchema = Joi.object({
|
|
|
|
|
|
username: Joi.string().min(2).max(50),
|
|
|
|
|
|
email: Joi.string().email(),
|
|
|
|
|
|
phone: Joi.string().pattern(/^1[3-9]\d{9}$/).allow(''),
|
2025-09-12 13:15:03 +08:00
|
|
|
|
user_type: Joi.string().valid('admin', 'buyer', 'supplier', 'trader'),
|
|
|
|
|
|
status: Joi.string().valid('active', 'inactive', 'suspended')
|
2025-09-02 21:59:27 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
2025-09-12 13:15:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @swagger
|
|
|
|
|
|
* /api/users:
|
|
|
|
|
|
* get:
|
|
|
|
|
|
* summary: 获取用户列表
|
|
|
|
|
|
* tags: [用户管理]
|
|
|
|
|
|
* security:
|
|
|
|
|
|
* - bearerAuth: []
|
|
|
|
|
|
* parameters:
|
|
|
|
|
|
* - in: query
|
|
|
|
|
|
* name: page
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: integer
|
|
|
|
|
|
* description: 页码,默认为1
|
|
|
|
|
|
* - in: query
|
|
|
|
|
|
* name: pageSize
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: integer
|
|
|
|
|
|
* description: 每页条数,默认为20
|
|
|
|
|
|
* - in: query
|
|
|
|
|
|
* name: keyword
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 关键词搜索(用户名、邮箱、手机号)
|
|
|
|
|
|
* - in: query
|
|
|
|
|
|
* name: user_type
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 用户类型筛选
|
|
|
|
|
|
* - in: query
|
|
|
|
|
|
* name: status
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* description: 用户状态筛选
|
|
|
|
|
|
* responses:
|
|
|
|
|
|
* 200:
|
|
|
|
|
|
* description: 获取成功
|
|
|
|
|
|
* content:
|
|
|
|
|
|
* application/json:
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: object
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* success:
|
|
|
|
|
|
* type: boolean
|
|
|
|
|
|
* data:
|
|
|
|
|
|
* type: object
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* items:
|
|
|
|
|
|
* type: array
|
|
|
|
|
|
* items:
|
|
|
|
|
|
* $ref: '#/components/schemas/User'
|
|
|
|
|
|
* total:
|
|
|
|
|
|
* type: integer
|
|
|
|
|
|
* page:
|
|
|
|
|
|
* type: integer
|
|
|
|
|
|
* pageSize:
|
|
|
|
|
|
* type: integer
|
|
|
|
|
|
* totalPages:
|
|
|
|
|
|
* type: integer
|
|
|
|
|
|
* 401:
|
|
|
|
|
|
* description: 未授权
|
|
|
|
|
|
* 500:
|
|
|
|
|
|
* description: 服务器内部错误
|
|
|
|
|
|
*/
|
2025-09-02 21:59:27 +08:00
|
|
|
|
// 获取用户列表
|
2025-09-05 01:18:40 +08:00
|
|
|
|
router.get('/', async (req, res) => {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
try {
|
2025-09-05 01:18:40 +08:00
|
|
|
|
const { page = 1, pageSize = 20, keyword, user_type, status } = req.query
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
2025-09-05 01:18:40 +08:00
|
|
|
|
// 构建查询条件
|
|
|
|
|
|
const where = {}
|
2025-09-02 21:59:27 +08:00
|
|
|
|
if (keyword) {
|
2025-09-05 01:18:40 +08:00
|
|
|
|
where[sequelize.Op.or] = [
|
|
|
|
|
|
{ username: { [sequelize.Op.like]: `%${keyword}%` } },
|
|
|
|
|
|
{ email: { [sequelize.Op.like]: `%${keyword}%` } },
|
|
|
|
|
|
{ phone: { [sequelize.Op.like]: `%${keyword}%` } }
|
|
|
|
|
|
]
|
2025-09-02 21:59:27 +08:00
|
|
|
|
}
|
2025-09-05 01:18:40 +08:00
|
|
|
|
if (user_type) where.user_type = user_type
|
|
|
|
|
|
if (status) where.status = status
|
|
|
|
|
|
|
|
|
|
|
|
// 分页查询
|
2025-09-19 00:42:14 +08:00
|
|
|
|
const result = await Admin.findAndCountAll({
|
2025-09-05 01:18:40 +08:00
|
|
|
|
where,
|
|
|
|
|
|
limit: parseInt(pageSize),
|
|
|
|
|
|
offset: (parseInt(page) - 1) * parseInt(pageSize),
|
|
|
|
|
|
order: [['createdAt', 'DESC']]
|
|
|
|
|
|
})
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
data: {
|
2025-09-05 01:18:40 +08:00
|
|
|
|
items: result.rows,
|
|
|
|
|
|
total: result.count,
|
2025-09-02 21:59:27 +08:00
|
|
|
|
page: parseInt(page),
|
|
|
|
|
|
pageSize: parseInt(pageSize),
|
2025-09-05 01:18:40 +08:00
|
|
|
|
totalPages: Math.ceil(result.count / parseInt(pageSize))
|
2025-09-02 21:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
} catch (error) {
|
2025-09-05 01:18:40 +08:00
|
|
|
|
console.error('获取用户列表失败:', error)
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '获取用户列表失败'
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-09-12 13:15:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @swagger
|
|
|
|
|
|
* /api/users/{id}:
|
|
|
|
|
|
* get:
|
|
|
|
|
|
* summary: 获取用户详情
|
|
|
|
|
|
* tags: [用户管理]
|
|
|
|
|
|
* security:
|
|
|
|
|
|
* - bearerAuth: []
|
|
|
|
|
|
* parameters:
|
|
|
|
|
|
* - in: path
|
|
|
|
|
|
* name: id
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: integer
|
|
|
|
|
|
* required: true
|
|
|
|
|
|
* description: 用户ID
|
|
|
|
|
|
* responses:
|
|
|
|
|
|
* 200:
|
|
|
|
|
|
* description: 获取成功
|
|
|
|
|
|
* content:
|
|
|
|
|
|
* application/json:
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: object
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* success:
|
|
|
|
|
|
* type: boolean
|
|
|
|
|
|
* data:
|
|
|
|
|
|
* $ref: '#/components/schemas/User'
|
|
|
|
|
|
* 401:
|
|
|
|
|
|
* description: 未授权
|
|
|
|
|
|
* 404:
|
|
|
|
|
|
* description: 用户不存在
|
|
|
|
|
|
* 500:
|
|
|
|
|
|
* description: 服务器内部错误
|
|
|
|
|
|
*/
|
2025-09-02 21:59:27 +08:00
|
|
|
|
// 获取用户详情
|
2025-09-05 01:18:40 +08:00
|
|
|
|
router.get('/:id', async (req, res) => {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params
|
2025-09-12 13:15:03 +08:00
|
|
|
|
|
2025-09-19 00:42:14 +08:00
|
|
|
|
const user = await Admin.findByPk(id)
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '用户不存在'
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
data: user
|
|
|
|
|
|
})
|
|
|
|
|
|
} catch (error) {
|
2025-09-05 01:18:40 +08:00
|
|
|
|
console.error('获取用户详情失败:', error)
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '获取用户详情失败'
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-09-12 13:15:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @swagger
|
|
|
|
|
|
* /api/users:
|
|
|
|
|
|
* post:
|
|
|
|
|
|
* summary: 创建新用户
|
|
|
|
|
|
* tags: [用户管理]
|
|
|
|
|
|
* security:
|
|
|
|
|
|
* - bearerAuth: []
|
|
|
|
|
|
* requestBody:
|
|
|
|
|
|
* required: true
|
|
|
|
|
|
* content:
|
|
|
|
|
|
* application/json:
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* $ref: '#/components/schemas/CreateUserRequest'
|
|
|
|
|
|
* responses:
|
|
|
|
|
|
* 201:
|
|
|
|
|
|
* description: 创建成功
|
|
|
|
|
|
* content:
|
|
|
|
|
|
* application/json:
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: object
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* success:
|
|
|
|
|
|
* type: boolean
|
|
|
|
|
|
* message:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* data:
|
|
|
|
|
|
* $ref: '#/components/schemas/User'
|
|
|
|
|
|
* 400:
|
|
|
|
|
|
* description: 参数验证失败
|
|
|
|
|
|
* 401:
|
|
|
|
|
|
* description: 未授权
|
|
|
|
|
|
* 500:
|
|
|
|
|
|
* description: 服务器内部错误
|
|
|
|
|
|
*/
|
|
|
|
|
|
// 创建新用户
|
2025-09-02 21:59:27 +08:00
|
|
|
|
router.post('/', async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { error, value } = createUserSchema.validate(req.body)
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '参数验证失败',
|
|
|
|
|
|
details: error.details[0].message
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-05 01:18:40 +08:00
|
|
|
|
const { username, email, phone, password, user_type, status } = value
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
2025-09-12 13:15:03 +08:00
|
|
|
|
// 检查用户名、邮箱是否已存在
|
2025-09-19 00:42:14 +08:00
|
|
|
|
const existingUser = await Admin.findOne({
|
2025-09-05 01:18:40 +08:00
|
|
|
|
where: {
|
|
|
|
|
|
[sequelize.Op.or]: [
|
2025-09-12 13:15:03 +08:00
|
|
|
|
{ username },
|
|
|
|
|
|
{ email }
|
2025-09-05 01:18:40 +08:00
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
2025-09-05 01:18:40 +08:00
|
|
|
|
if (existingUser) {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
2025-09-12 13:15:03 +08:00
|
|
|
|
message: '用户名或邮箱已被使用'
|
2025-09-02 21:59:27 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-05 01:18:40 +08:00
|
|
|
|
// 密码加密
|
2025-09-12 13:15:03 +08:00
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 10)
|
2025-09-05 01:18:40 +08:00
|
|
|
|
|
2025-09-12 13:15:03 +08:00
|
|
|
|
// 创建用户
|
2025-09-19 00:42:14 +08:00
|
|
|
|
const user = await Admin.create({
|
2025-09-02 21:59:27 +08:00
|
|
|
|
username,
|
|
|
|
|
|
email,
|
2025-09-12 13:15:03 +08:00
|
|
|
|
phone,
|
|
|
|
|
|
password_hash: hashedPassword,
|
2025-09-05 01:18:40 +08:00
|
|
|
|
user_type,
|
2025-09-12 13:15:03 +08:00
|
|
|
|
status
|
2025-09-05 01:18:40 +08:00
|
|
|
|
})
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
2025-09-12 13:15:03 +08:00
|
|
|
|
// 移除密码哈希,避免返回敏感信息
|
|
|
|
|
|
const userData = user.toJSON()
|
|
|
|
|
|
delete userData.password_hash
|
|
|
|
|
|
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(201).json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '用户创建成功',
|
2025-09-12 13:15:03 +08:00
|
|
|
|
data: userData
|
2025-09-02 21:59:27 +08:00
|
|
|
|
})
|
|
|
|
|
|
} catch (error) {
|
2025-09-05 01:18:40 +08:00
|
|
|
|
console.error('创建用户失败:', error)
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '创建用户失败'
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-09-12 13:15:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @swagger
|
|
|
|
|
|
* /api/users/{id}:
|
|
|
|
|
|
* put:
|
|
|
|
|
|
* summary: 更新用户信息
|
|
|
|
|
|
* tags: [用户管理]
|
|
|
|
|
|
* security:
|
|
|
|
|
|
* - bearerAuth: []
|
|
|
|
|
|
* parameters:
|
|
|
|
|
|
* - in: path
|
|
|
|
|
|
* name: id
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: integer
|
|
|
|
|
|
* required: true
|
|
|
|
|
|
* description: 用户ID
|
|
|
|
|
|
* requestBody:
|
|
|
|
|
|
* required: true
|
|
|
|
|
|
* content:
|
|
|
|
|
|
* application/json:
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* $ref: '#/components/schemas/UpdateUserRequest'
|
|
|
|
|
|
* responses:
|
|
|
|
|
|
* 200:
|
|
|
|
|
|
* description: 更新成功
|
|
|
|
|
|
* content:
|
|
|
|
|
|
* application/json:
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: object
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* success:
|
|
|
|
|
|
* type: boolean
|
|
|
|
|
|
* message:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* data:
|
|
|
|
|
|
* $ref: '#/components/schemas/User'
|
|
|
|
|
|
* 400:
|
|
|
|
|
|
* description: 参数验证失败
|
|
|
|
|
|
* 401:
|
|
|
|
|
|
* description: 未授权
|
|
|
|
|
|
* 404:
|
|
|
|
|
|
* description: 用户不存在
|
|
|
|
|
|
* 500:
|
|
|
|
|
|
* description: 服务器内部错误
|
|
|
|
|
|
*/
|
|
|
|
|
|
// 更新用户信息
|
2025-09-05 01:18:40 +08:00
|
|
|
|
router.put('/:id', async (req, res) => {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params
|
|
|
|
|
|
|
|
|
|
|
|
const { error, value } = updateUserSchema.validate(req.body)
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '参数验证失败',
|
|
|
|
|
|
details: error.details[0].message
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-12 13:15:03 +08:00
|
|
|
|
// 查找用户
|
2025-09-19 00:42:14 +08:00
|
|
|
|
const user = await Admin.findByPk(id)
|
2025-09-12 13:15:03 +08:00
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '用户不存在'
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 21:59:27 +08:00
|
|
|
|
// 更新用户信息
|
2025-09-05 01:18:40 +08:00
|
|
|
|
await user.update(value)
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '用户更新成功',
|
2025-09-05 01:18:40 +08:00
|
|
|
|
data: user
|
2025-09-02 21:59:27 +08:00
|
|
|
|
})
|
|
|
|
|
|
} catch (error) {
|
2025-09-05 01:18:40 +08:00
|
|
|
|
console.error('更新用户失败:', error)
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '更新用户失败'
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-09-12 13:15:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @swagger
|
|
|
|
|
|
* /api/users/{id}:
|
|
|
|
|
|
* delete:
|
|
|
|
|
|
* summary: 删除用户
|
|
|
|
|
|
* tags: [用户管理]
|
|
|
|
|
|
* security:
|
|
|
|
|
|
* - bearerAuth: []
|
|
|
|
|
|
* parameters:
|
|
|
|
|
|
* - in: path
|
|
|
|
|
|
* name: id
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: integer
|
|
|
|
|
|
* required: true
|
|
|
|
|
|
* description: 用户ID
|
|
|
|
|
|
* responses:
|
|
|
|
|
|
* 200:
|
|
|
|
|
|
* description: 删除成功
|
|
|
|
|
|
* content:
|
|
|
|
|
|
* application/json:
|
|
|
|
|
|
* schema:
|
|
|
|
|
|
* type: object
|
|
|
|
|
|
* properties:
|
|
|
|
|
|
* success:
|
|
|
|
|
|
* type: boolean
|
|
|
|
|
|
* message:
|
|
|
|
|
|
* type: string
|
|
|
|
|
|
* 401:
|
|
|
|
|
|
* description: 未授权
|
|
|
|
|
|
* 404:
|
|
|
|
|
|
* description: 用户不存在
|
|
|
|
|
|
* 500:
|
|
|
|
|
|
* description: 服务器内部错误
|
|
|
|
|
|
*/
|
2025-09-02 21:59:27 +08:00
|
|
|
|
// 删除用户
|
2025-09-05 01:18:40 +08:00
|
|
|
|
router.delete('/:id', async (req, res) => {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const { id } = req.params
|
2025-09-12 13:15:03 +08:00
|
|
|
|
|
2025-09-19 00:42:14 +08:00
|
|
|
|
const user = await Admin.findByPk(id)
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
2025-09-05 01:18:40 +08:00
|
|
|
|
if (!user) {
|
2025-09-02 21:59:27 +08:00
|
|
|
|
return res.status(404).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '用户不存在'
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-12 13:15:03 +08:00
|
|
|
|
// 软删除或永久删除
|
|
|
|
|
|
// 如果需要软删除,可以改为更新status为'inactive'
|
2025-09-05 01:18:40 +08:00
|
|
|
|
await user.destroy()
|
2025-09-02 21:59:27 +08:00
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '用户删除成功'
|
|
|
|
|
|
})
|
|
|
|
|
|
} catch (error) {
|
2025-09-05 01:18:40 +08:00
|
|
|
|
console.error('删除用户失败:', error)
|
2025-09-02 21:59:27 +08:00
|
|
|
|
res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '删除用户失败'
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router
|