liufumingren 8597cf5ac9 feat(backend): 用户鉴权、菜单权限、主体与系统基础模块
- 登录/邮箱验证码/密码流程(controller/auth.js、lib/emailCode.js、middleware/jwt_auth.js)
- 主体(principal)隔离与主体用户、对外接口配置模块及 principalCheck 中间件
- 菜单、用户、文件上传、快递公司字典(express_companies)等基础模块
- 通用工具:分页(lib/pagination.js)、缓存(service/cache.js)
- 全量接口契约(contract/index.js)与路由注册(router.js)、config.default.js 配置
- 相关迁移:菜单组件调整与菜单种子、创建 files 表
2026-09-13 02:23:34 +08:00

189 lines
5.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const { Controller } = require('egg');
const { generateSalt, hashPassword } = require('../lib/password');
const { parsePagination, buildPageResult } = require('../lib/pagination');
/**
* @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, pageSize, offset, limit } = parsePagination(ctx.query);
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,
offset,
limit,
attributes: { exclude: [ 'password', 'salt' ] },
order: [[ 'id', 'DESC' ]],
});
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, 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;
}
// 请求拦截器会注入 principal_id,而 users 表无该字段,更新前剔除避免脏字段写入
delete ctx.request.body.principal_id;
await user.update(ctx.request.body);
await ctx.service.cache.invalidateUser(user.id);
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) });
await ctx.service.cache.invalidateUser(user.id);
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();
await ctx.service.cache.invalidateUser(user.id);
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
}
}
module.exports = UserController;