- 组织架构:主体管理改为组织架构页(左侧 tab:集团设置 / 下属公司),主体列表仅保留顶部下拉切换 - 新增下属公司表 principal_subsidiaries、接口与页面,按当前主体隔离 - 集团设置读取/编辑当前主体基础信息,补所属主体校验与更新字段白名单 - 权限管理:新增 permissions / roles / role_permissions 表 - 权限以现有顶级菜单为分类,每个菜单两种范围:集团全部 / 所在公司 - 系统预设角色:系统管理员(全部集团权限)、货主权限(全部所在公司权限) - 权限管理页只展示角色组,支持按菜单勾选权限;系统预设角色只读 - 员工档案:账号菜单改为员工档案 - 新增员工-下属公司多对多关联(principal_user_companies) - 创建/编辑员工必须选择角色组;除所有者外必须至少关联一家公司 - 菜单重命名与旧地址重定向;账号导出新增角色组、所属公司 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
222 lines
7.6 KiB
JavaScript
222 lines
7.6 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
const { parsePagination, buildPageResult } = require('../lib/pagination');
|
||
|
||
/** 允许更新的主体字段,避免请求体覆盖 id 等主键信息 */
|
||
const WRITABLE_FIELDS = [ 'name', 'type', 'contact_name', 'contact_phone', 'status', 'remark' ];
|
||
|
||
/**
|
||
* @controller PrincipalController 主体管理
|
||
*/
|
||
class PrincipalController extends Controller {
|
||
/**
|
||
* @summary 主体列表
|
||
* @description 分页查询主体,支持名称/编码模糊搜索、类型与状态过滤
|
||
* @router get /api/principals
|
||
* @apikey
|
||
* @request query integer page 页码,默认 1
|
||
* @request query integer pageSize 每页条数,默认 20
|
||
* @request query string keyword 关键词,匹配主体名称
|
||
* @request query integer type 主体类型:1自营 2加盟 3外部,不传则不过滤
|
||
* @request query integer status 状态:1启用 0禁用,不传则不过滤
|
||
* @response 200 principalListResponse success
|
||
*/
|
||
async index() {
|
||
const { ctx } = this;
|
||
const Op = this.app.Sequelize.Op;
|
||
const { page, pageSize, offset, limit } = parsePagination(ctx.query);
|
||
const { keyword, type, status } = ctx.query;
|
||
|
||
const where = {};
|
||
if (keyword) {
|
||
where.name = { [Op.like]: `%${keyword}%` };
|
||
}
|
||
if (type !== undefined && type !== '') where.type = Number(type);
|
||
if (status !== undefined && status !== '') where.status = Number(status);
|
||
|
||
// 按当前登录账号所属主体隔离(多租户):仅返回通过该账号 principal_users 关联的主体
|
||
// 主体ID列表由 JWT 中间件从缓存加载,列表本身不做缓存(分页/筛选要求实时)
|
||
const principalIds = ctx.state.principalIds || await ctx.service.cache.getUserPrincipalIds(ctx.userId);
|
||
where.id = { [Op.in]: principalIds };
|
||
|
||
const { count, rows } = await ctx.model.Principal.findAndCountAll({
|
||
where,
|
||
offset,
|
||
limit,
|
||
order: [[ 'id', 'DESC' ]],
|
||
});
|
||
|
||
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) };
|
||
}
|
||
|
||
/**
|
||
* @summary 我的可选主体(头部主体切换下拉)
|
||
* @description 返回当前登录账号已绑定的主体列表,用于页面头部切换/选择主体;
|
||
* 该接口走 Redis 缓存(主体新增、更新、删除或绑定关系变更时自动失效),管理端列表接口 /api/principals 不缓存
|
||
* @router get /api/principals/options
|
||
* @apikey
|
||
* @response 200 principalOptionsResponse success
|
||
*/
|
||
async options() {
|
||
const { ctx } = this;
|
||
const list = await ctx.service.cache.getUserPrincipalOptions(ctx.userId);
|
||
ctx.body = { code: 0, message: 'success', data: { list } };
|
||
}
|
||
|
||
/**
|
||
* @summary 主体详情
|
||
* @description 按主键查询主体(走缓存,主体更新/删除时自动失效)
|
||
* @router get /api/principals/{id}
|
||
* @apikey
|
||
* @request path integer *id 主体主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async show() {
|
||
const { ctx } = this;
|
||
const bound = await this.isBound(ctx.params.id);
|
||
if (!bound) {
|
||
ctx.status = 403;
|
||
ctx.body = { code: 403, message: '无权查看该主体' };
|
||
return;
|
||
}
|
||
|
||
const principal = await ctx.service.cache.getPrincipal(ctx.params.id);
|
||
|
||
if (!principal) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '主体不存在' };
|
||
return;
|
||
}
|
||
|
||
ctx.body = { code: 0, message: 'success', data: principal };
|
||
}
|
||
|
||
/**
|
||
* @summary 新增主体
|
||
* @description 主体名称 name 必填;创建者自动关联为该主体 owner(多租户隔离)
|
||
* @router post /api/principals
|
||
* @apikey
|
||
* @request body createPrincipalRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async create() {
|
||
const { ctx } = this;
|
||
const { name } = ctx.request.body;
|
||
|
||
if (!name) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: 'name 为必填项' };
|
||
return;
|
||
}
|
||
|
||
const principal = await ctx.model.Principal.create(ctx.request.body);
|
||
|
||
// 将创建者关联为该主体的默认用户(owner),建立多租户归属关系
|
||
if (ctx.userId) {
|
||
await ctx.model.PrincipalUser.create({
|
||
principal_id: principal.id,
|
||
user_id: ctx.userId,
|
||
role: 'owner',
|
||
is_default: 1,
|
||
status: 1,
|
||
});
|
||
}
|
||
|
||
// 新增主体后,创建者立即可见:失效其主体ID列表与可选主体缓存
|
||
await ctx.service.cache.invalidateUserPrincipals(ctx.userId);
|
||
|
||
ctx.body = { code: 0, message: 'success', data: { id: principal.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 更新主体
|
||
* @description 未传入的字段保持原值
|
||
* @router put /api/principals/{id}
|
||
* @apikey
|
||
* @request path integer *id 主体主键ID
|
||
* @request body updatePrincipalRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async update() {
|
||
const { ctx } = this;
|
||
const bound = await this.isBound(ctx.params.id);
|
||
if (!bound) {
|
||
ctx.status = 403;
|
||
ctx.body = { code: 403, message: '无权操作该主体' };
|
||
return;
|
||
}
|
||
|
||
const principal = await ctx.model.Principal.findByPk(ctx.params.id);
|
||
|
||
if (!principal) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '主体不存在' };
|
||
return;
|
||
}
|
||
|
||
const data = {};
|
||
WRITABLE_FIELDS.forEach(field => {
|
||
if (ctx.request.body[field] !== undefined) data[field] = ctx.request.body[field];
|
||
});
|
||
|
||
await principal.update(data);
|
||
|
||
// 主体信息变更:失效主体详情缓存及所有依赖主体信息的缓存(可选主体列表等)
|
||
await ctx.service.cache.invalidatePrincipal(principal.id);
|
||
|
||
ctx.body = { code: 0, message: 'success', data: { id: principal.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 删除主体
|
||
* @description 物理删除主体;存在关联商品/订单时拒绝删除并提示先清理;删除前自动解除主体与用户的绑定关系
|
||
* @router delete /api/principals/{id}
|
||
* @apikey
|
||
* @request path integer *id 主体主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async destroy() {
|
||
const { ctx } = this;
|
||
const principal = await ctx.model.Principal.findByPk(ctx.params.id);
|
||
|
||
if (!principal) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '主体不存在' };
|
||
return;
|
||
}
|
||
|
||
// 业务数据受外键约束,先提示清理,避免直接删除报数据库错误
|
||
const [ productCount, orderCount ] = await Promise.all([
|
||
ctx.model.Product.count({ where: { principal_id: principal.id } }),
|
||
ctx.model.Order.count({ where: { principal_id: principal.id } }),
|
||
]);
|
||
if (productCount || orderCount) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: `该主体下仍有 ${productCount} 个商品、${orderCount} 个订单,请先清理后再删除` };
|
||
return;
|
||
}
|
||
|
||
// 主体与用户的绑定关系含外键,需先解除,否则删除会因约束失败
|
||
await ctx.model.PrincipalUser.destroy({ where: { principal_id: principal.id } });
|
||
await principal.destroy();
|
||
|
||
await ctx.service.cache.invalidatePrincipal(principal.id);
|
||
|
||
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
|
||
}
|
||
|
||
/**
|
||
* 校验当前账号是否绑定该主体(组织架构 → 集团设置只允许操作/查看自己所属主体)
|
||
* @param {number|string} principalId 主体ID
|
||
* @return {Promise<boolean>} 是否已绑定
|
||
*/
|
||
async isBound(principalId) {
|
||
const { ctx } = this;
|
||
const principalIds = ctx.state.principalIds || await ctx.service.cache.getUserPrincipalIds(ctx.userId);
|
||
return new Set((principalIds || []).map(Number)).has(Number(principalId));
|
||
}
|
||
}
|
||
|
||
module.exports = PrincipalController;
|