- product_categories / product_brands / product_units 新增 subsidiary_id(NULL 表示主体级、全集团共用) - 列表与下拉按货主范围过滤:货主权限仅可见「主体级 + 自己所属公司」,并支持按货主筛选 - 新增/编辑支持选择货主,货主权限只能选主体或自己所属公司;同货主范围内名称唯一 - 详情/更新/删除对越权属性返回 403;导出新增货主列并收窄到可见范围 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
78 lines
3.1 KiB
JavaScript
78 lines
3.1 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 商品属性(分类 / 品牌 / 单位)的货主(下属公司)作用域辅助。
|
||
*
|
||
* 数据规则:
|
||
* - subsidiary_id 为 NULL 表示「主体级」属性,全集团可见可用;
|
||
* - 填写 subsidiary_id 则为该下属公司(货主)私有;
|
||
* - 集团权限可见/可维护全部;所在公司权限仅可见「主体级 + 自己所属公司」,
|
||
* 且只能把属性建到「主体级」或自己所属公司。
|
||
*/
|
||
|
||
/**
|
||
* 解析当前账号在该主体下的可见范围
|
||
* @param {Egg.Context} ctx egg context(需 principalCheck 已写入 ctx.state.principalId)
|
||
* @return {Promise<{isGroup: boolean, ownCompanyIds: Array<number>}>} 范围信息
|
||
*/
|
||
async function resolveScope(ctx) {
|
||
const access = await ctx.service.access.resolve(ctx.userId, ctx.state.principalId);
|
||
return { isGroup: !!access.isGroup, ownCompanyIds: (access.ownCompanyIds || []).map(Number) };
|
||
}
|
||
|
||
/**
|
||
* 组装「主体级 + 自己所属公司」的可见条件(集团权限返回 null,表示不限制)
|
||
* @param {Object} ctx egg context
|
||
* @param {Object} scope resolveScope 的结果
|
||
* @param {string} column 表别名限定列名,默认 subsidiary_id
|
||
* @return {Object|null} Sequelize where 片段
|
||
*/
|
||
function visibilityWhere(ctx, scope, column = 'subsidiary_id') {
|
||
if (scope.isGroup) return null;
|
||
const Op = ctx.app.Sequelize.Op;
|
||
const or = [{ [column]: null }];
|
||
if (scope.ownCompanyIds.length) or.push({ [column]: { [Op.in]: scope.ownCompanyIds } });
|
||
return { [Op.or]: or };
|
||
}
|
||
|
||
/**
|
||
* 校验并归一化前端传入的 subsidiary_id
|
||
* @param {Object} ctx egg context
|
||
* @param {Object} scope resolveScope 的结果
|
||
* @param {number|string|null|undefined} value 前端传入的 subsidiary_id
|
||
* @return {Promise<{ok: boolean, value?: number|null, message?: string}>} 校验结果(value 为 null 即主体级)
|
||
*/
|
||
async function resolveSubsidiaryValue(ctx, scope, value) {
|
||
// 空值表示主体级:所在公司权限同样允许
|
||
if (value === undefined || value === null || value === '') return { ok: true, value: null };
|
||
|
||
const id = Number(value);
|
||
if (!Number.isInteger(id) || id <= 0) return { ok: false, message: '所属货主不合法' };
|
||
|
||
const subsidiary = await ctx.model.PrincipalSubsidiary.findOne({
|
||
where: { id, principal_id: ctx.state.principalId },
|
||
attributes: [ 'id' ],
|
||
});
|
||
if (!subsidiary) return { ok: false, message: '所属货主不存在或不属于当前集团' };
|
||
|
||
if (!scope.isGroup && !scope.ownCompanyIds.includes(id)) {
|
||
return { ok: false, forbidden: true, message: '只能选择自己所属公司或主体' };
|
||
}
|
||
return { ok: true, value: id };
|
||
}
|
||
|
||
/**
|
||
* 判断属性记录是否在当前账号可见范围内
|
||
* @param {Object} item 属性 model 实例
|
||
* @param {Object} scope resolveScope 的结果
|
||
* @return {boolean} 是否可见
|
||
*/
|
||
function isVisible(item, scope) {
|
||
if (!item) return false;
|
||
if (scope.isGroup) return true;
|
||
if (item.subsidiary_id === null || item.subsidiary_id === undefined) return true;
|
||
return scope.ownCompanyIds.includes(Number(item.subsidiary_id));
|
||
}
|
||
|
||
module.exports = { resolveScope, visibilityWhere, resolveSubsidiaryValue, isVisible };
|