'use strict'; const { Controller } = require('egg'); const { generateSalt, hashPassword } = require('../lib/password'); /** * @controller UserController 用户管理 */ class UserController extends Controller { /** * @summary 用户列表 * @description 分页查询用户,支持关键词模糊匹配(账号/昵称)与状态过滤 * @router get /api/users * @apikey * @request query integer page 页码,默认 1 * @request query integer pageSize 每页条数,默认 20 * @request query string keyword 关键词,匹配登录账号或昵称 * @request query integer status 状态:1启用 0禁用,不传则不过滤 * @response 200 userListResponse success */ async index() { const { ctx } = this; const Op = this.app.Sequelize.Op; const page = Number(ctx.query.page || 1); const pageSize = Number(ctx.query.pageSize || 20); const { keyword, status } = ctx.query; const where = {}; if (keyword) { where[Op.or] = [ { username: { [Op.like]: `%${keyword}%` } }, { nickname: { [Op.like]: `%${keyword}%` } }, ]; } if (status !== undefined && status !== '') where.status = Number(status); const { count, rows } = await ctx.model.User.findAndCountAll({ where, attributes: { exclude: [ 'password', 'salt' ] }, offset: (page - 1) * pageSize, limit: pageSize, order: [[ 'id', 'DESC' ]], }); ctx.body = { code: 0, message: 'success', data: { list: rows, total: count, page, pageSize } }; } /** * @summary 用户详情 * @description 按主键查询用户,不返回密码与盐值 * @router get /api/users/{id} * @apikey * @request path integer *id 用户主键ID * @response 200 baseResponse success */ async show() { const { ctx } = this; const user = await ctx.model.User.findByPk(ctx.params.id, { attributes: { exclude: [ 'password', 'salt' ] }, }); if (!user) { ctx.status = 404; ctx.body = { code: 404, message: '用户不存在' }; return; } ctx.body = { code: 0, message: 'success', data: user }; } /** * @summary 新增用户 * @description 密码传明文,服务端生成 salt 并以 sha256(password:salt) 落库 * @router post /api/users * @apikey * @request body createUserRequest *body * @response 200 baseResponse success */ async create() { const { ctx } = this; const { username, password } = ctx.request.body; if (!username || !password) { ctx.status = 400; ctx.body = { code: 400, message: 'username 与 password 为必填项' }; return; } const exist = await ctx.model.User.findOne({ where: { username } }); if (exist) { ctx.status = 400; ctx.body = { code: 400, message: '登录账号已存在' }; return; } const salt = generateSalt(); const user = await ctx.model.User.create({ ...ctx.request.body, salt, password: hashPassword(password, salt), }); ctx.body = { code: 0, message: 'success', data: { id: user.id } }; } /** * @summary 更新用户 * @description 未传入的字段保持原值;修改密码请调用重置密码接口 * @router put /api/users/{id} * @apikey * @request path integer *id 用户主键ID * @request body updateUserRequest *body * @response 200 baseResponse success */ async update() { const { ctx } = this; const user = await ctx.model.User.findByPk(ctx.params.id); if (!user) { ctx.status = 404; ctx.body = { code: 404, message: '用户不存在' }; return; } await user.update(ctx.request.body); ctx.body = { code: 0, message: 'success', data: { id: user.id } }; } /** * @summary 重置用户密码 * @description 重新生成盐值并更新加盐哈希密码 * @router put /api/users/{id}/password * @apikey * @request path integer *id 用户主键ID * @request body resetPasswordRequest *body * @response 200 baseResponse success */ async resetPassword() { const { ctx } = this; const { password } = ctx.request.body; if (!password) { ctx.status = 400; ctx.body = { code: 400, message: 'password 为必填项' }; return; } const user = await ctx.model.User.findByPk(ctx.params.id); if (!user) { ctx.status = 404; ctx.body = { code: 404, message: '用户不存在' }; return; } const salt = generateSalt(); await user.update({ salt, password: hashPassword(password, salt) }); ctx.body = { code: 0, message: 'success', data: { id: user.id } }; } /** * @summary 删除用户 * @description 物理删除用户记录 * @router delete /api/users/{id} * @apikey * @request path integer *id 用户主键ID * @response 200 baseResponse success */ async destroy() { const { ctx } = this; const user = await ctx.model.User.findByPk(ctx.params.id); if (!user) { ctx.status = 404; ctx.body = { code: 404, message: '用户不存在' }; return; } await user.destroy(); ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } }; } } module.exports = UserController;