- 新增 access 服务与 /api/access/context:按角色组权限判定集团 / 所在公司范围 - 菜单树按范围裁剪:所在公司范围仅保留「员工档案」,其余系统管理菜单隐藏 - 员工档案:角色组与所属公司必填;所在公司范围仅可见、仅可关联自己所属公司 - 创建者(所有者)行禁止编辑与解绑,且不可被移出主体;禁止新建所有者 - 所在公司范围禁止修改自己或他人的角色组、所属公司 - 新增员工时邮箱已注册则复用既有账号(reused),不再报「该邮箱已注册」 - 迁移:将所有者 role_id 统一指向系统管理员角色组 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
217 lines
7.1 KiB
JavaScript
217 lines
7.1 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
const { parsePagination, buildPageResult } = require('../lib/pagination');
|
||
|
||
/** 允许前端写入的字段,避免覆盖 principal_id 等归属信息 */
|
||
const WRITABLE_FIELDS = [ 'name', 'credit_code', 'contact_name', 'contact_phone', 'address', 'status', 'remark' ];
|
||
|
||
/** 取请求体中的可写字段(未传的字段保持原值)
|
||
* @param {Object} body 请求体
|
||
* @return {Object} 可写字段
|
||
*/
|
||
function pickWritable(body) {
|
||
const data = {};
|
||
WRITABLE_FIELDS.forEach(field => {
|
||
if (body[field] !== undefined) data[field] = body[field];
|
||
});
|
||
return data;
|
||
}
|
||
|
||
/**
|
||
* @controller PrincipalSubsidiaryController 组织架构 → 下属公司
|
||
* 数据按当前主体(ctx.state.principalId)隔离:集团主体下拉切换后只看到该主体下的公司档案。
|
||
*/
|
||
class PrincipalSubsidiaryController extends Controller {
|
||
/**
|
||
* @summary 下属公司列表
|
||
* @description 分页查询当前主体下的下属公司,支持关键词(公司名称/信用代码/联系人)与状态过滤
|
||
* @router get /api/principal-subsidiaries
|
||
* @apikey
|
||
* @request query integer page 页码,默认 1
|
||
* @request query integer pageSize 每页条数,默认 20
|
||
* @request query string keyword 关键词,匹配公司名称 / 统一社会信用代码 / 联系人
|
||
* @request query integer status 状态:1启用 0禁用,不传则不过滤
|
||
* @response 200 principalSubsidiaryListResponse 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 = { principal_id: ctx.state.principalId };
|
||
const text = String(keyword || '').trim();
|
||
if (text) {
|
||
where[Op.or] = [
|
||
{ name: { [Op.like]: `%${text}%` } },
|
||
{ credit_code: { [Op.like]: `%${text}%` } },
|
||
{ contact_name: { [Op.like]: `%${text}%` } },
|
||
];
|
||
}
|
||
if (status !== undefined && status !== '') where.status = Number(status);
|
||
|
||
const { count, rows } = await ctx.model.PrincipalSubsidiary.findAndCountAll({
|
||
where,
|
||
offset,
|
||
limit,
|
||
order: [[ 'id', 'DESC' ]],
|
||
});
|
||
|
||
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) };
|
||
}
|
||
|
||
/**
|
||
* @summary 下属公司下拉(仅启用)
|
||
* @description 供员工档案/商品等页面选择公司,按当前主体隔离;
|
||
* mine=1 时仅返回当前账号自己关联的公司(集团权限返回全部)
|
||
* @router get /api/principal-subsidiaries/select
|
||
* @apikey
|
||
* @request query integer mine 是否仅返回自己所属的公司:1是
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async select() {
|
||
const { ctx } = this;
|
||
const Op = this.app.Sequelize.Op;
|
||
const principalId = ctx.state.principalId;
|
||
|
||
const where = { principal_id: principalId, status: 1 };
|
||
if (String(ctx.query.mine || '') === '1') {
|
||
const access = await ctx.service.access.resolve(ctx.userId, principalId);
|
||
if (!access.isGroup) where.id = { [Op.in]: access.ownCompanyIds };
|
||
}
|
||
|
||
const rows = await ctx.model.PrincipalSubsidiary.findAll({
|
||
where,
|
||
attributes: [ 'id', 'name' ],
|
||
order: [[ 'id', 'ASC' ]],
|
||
raw: true,
|
||
});
|
||
ctx.body = { code: 0, message: 'success', data: rows };
|
||
}
|
||
|
||
/**
|
||
* @summary 下属公司详情
|
||
* @router get /api/principal-subsidiaries/{id}
|
||
* @apikey
|
||
* @request path integer *id 下属公司主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async show() {
|
||
const { ctx } = this;
|
||
const item = await this.findScoped(ctx.params.id);
|
||
if (!item) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '下属公司不存在' };
|
||
return;
|
||
}
|
||
ctx.body = { code: 0, message: 'success', data: item };
|
||
}
|
||
|
||
/**
|
||
* @summary 新增下属公司
|
||
* @description 同一主体下公司名称唯一
|
||
* @router post /api/principal-subsidiaries
|
||
* @apikey
|
||
* @request body createPrincipalSubsidiaryRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async create() {
|
||
const { ctx } = this;
|
||
const principalId = ctx.state.principalId;
|
||
const data = pickWritable(ctx.request.body || {});
|
||
const name = String(data.name || '').trim();
|
||
|
||
if (!name) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: '公司名称为必填项' };
|
||
return;
|
||
}
|
||
|
||
const exist = await ctx.model.PrincipalSubsidiary.findOne({ where: { principal_id: principalId, name } });
|
||
if (exist) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: '该主体下已存在同名公司' };
|
||
return;
|
||
}
|
||
|
||
const item = await ctx.model.PrincipalSubsidiary.create({ ...data, name, principal_id: principalId });
|
||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 更新下属公司
|
||
* @description 未传入的字段保持原值;同一主体下公司名称唯一
|
||
* @router put /api/principal-subsidiaries/{id}
|
||
* @apikey
|
||
* @request path integer *id 下属公司主键ID
|
||
* @request body updatePrincipalSubsidiaryRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async update() {
|
||
const { ctx } = this;
|
||
const item = await this.findScoped(ctx.params.id);
|
||
if (!item) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '下属公司不存在' };
|
||
return;
|
||
}
|
||
|
||
const data = pickWritable(ctx.request.body || {});
|
||
if (data.name !== undefined) {
|
||
const name = String(data.name || '').trim();
|
||
if (!name) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: '公司名称为必填项' };
|
||
return;
|
||
}
|
||
const exist = await ctx.model.PrincipalSubsidiary.findOne({
|
||
where: { principal_id: item.principal_id, name },
|
||
});
|
||
if (exist && Number(exist.id) !== Number(item.id)) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: '该主体下已存在同名公司' };
|
||
return;
|
||
}
|
||
data.name = name;
|
||
}
|
||
|
||
await item.update(data);
|
||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 删除下属公司
|
||
* @router delete /api/principal-subsidiaries/{id}
|
||
* @apikey
|
||
* @request path integer *id 下属公司主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async destroy() {
|
||
const { ctx } = this;
|
||
const item = await this.findScoped(ctx.params.id);
|
||
if (!item) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '下属公司不存在' };
|
||
return;
|
||
}
|
||
|
||
await item.destroy();
|
||
ctx.body = { code: 0, message: 'success', data: { id: Number(item.id) } };
|
||
}
|
||
|
||
/**
|
||
* 按当前主体取下属公司,避免同一账号切换主体后操作到其它主体(集团)的数据
|
||
* @param {number|string} id 下属公司主键
|
||
* @return {Promise<object|null>} 下属公司记录
|
||
*/
|
||
async findScoped(id) {
|
||
const { ctx } = this;
|
||
return await ctx.model.PrincipalSubsidiary.findOne({
|
||
where: { id, principal_id: ctx.state.principalId },
|
||
});
|
||
}
|
||
}
|
||
|
||
module.exports = PrincipalSubsidiaryController;
|