liufumingren 7c8e531362 feat(shop): 店铺/店铺商品关联货主与打单平台权限限制
- shops、shop_products 新增 subsidiary_id,店铺商品货主跟随店铺并按店铺回填历史数据
- 店铺列表/详情/新增/编辑/删除按货主范围隔离:货主权限仅可见/可维护自己所属公司的店铺,
  新增必须选货主,货主下拉仅自己所属公司;集团改店铺货主时店铺商品同步跟随
- 外部打单平台导入的店铺(type=2)在货主权限下只读:仅保留拉取订单、同步商品,不可编辑/删除
- 打单平台相关接口(平台资料、账号、商家、快递接口、外部发货)加集团权限门禁;添加店铺必须指定货主
- 货主权限菜单隐藏「外部打单平台」;店铺商品列表/绑定按货主范围隔离
- 店铺与店铺商品导出新增货主列并按范围收窄

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-05 02:15:42 +08:00

227 lines
6.8 KiB
JavaScript
Raw Permalink 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 { parsePagination, buildPageResult } = require('../lib/pagination');
/** 所在公司权限可访问的系统管理菜单(员工档案) */
const EMPLOYEE_MENU_PATH = '/system/employee';
/** 仅集团权限可访问的菜单:外部打单平台(账号/商家导入/快递接口均为集团维护) */
const GROUP_ONLY_MENU_PATHS = [ '/external-print-platform' ];
/**
* @controller MenuController 菜单管理
*/
class MenuController extends Controller {
/**
* @summary 菜单列表
* @description 分页查询菜单,支持按父级、类型与状态过滤
* @router get /api/menus
* @apikey
* @request query integer page 页码,默认 1
* @request query integer pageSize 每页条数,默认 20
* @request query integer parent_id 父菜单ID,0 表示根节点
* @request query integer type 类型:1目录 2菜单 3按钮,不传则不过滤
* @request query integer status 状态:1启用 0禁用,不传则不过滤
* @response 200 menuListResponse success
*/
async index() {
const { ctx } = this;
const { page, pageSize, offset, limit } = parsePagination(ctx.query);
const { parent_id: parentId, type, status } = ctx.query;
const where = {};
if (parentId !== undefined && parentId !== '') where.parent_id = Number(parentId);
if (type !== undefined && type !== '') where.type = Number(type);
if (status !== undefined && status !== '') where.status = Number(status);
const { count, rows } = await ctx.model.Menu.findAndCountAll({
where,
offset,
limit,
order: [[ 'sort', 'DESC' ], [ 'id', 'ASC' ]],
});
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) };
}
/**
* @summary 菜单树
* @description 一次性返回全部菜单并按 parent_id 组装为树形结构;
* 传入 principal_id 且当前账号在该主体下仅为「所在公司」权限时,
* 系统管理下仅保留「员工档案」(其余菜单需集团权限)
* @router get /api/menus/tree
* @apikey
* @request query integer status 状态:1启用 0禁用,不传则返回全部
* @request query integer principal_id 当前主体ID(用于判定权限范围)
* @response 200 baseResponse success
*/
async tree() {
const { ctx } = this;
const { status } = ctx.query;
const where = {};
if (status !== undefined && status !== '') where.status = Number(status);
const list = await ctx.model.Menu.findAll({
where,
order: [[ 'sort', 'DESC' ], [ 'id', 'ASC' ]],
});
let rows = list.map(item => item.toJSON());
if (ctx.query.principal_id) {
const access = await ctx.service.access.resolve(ctx.userId, ctx.query.principal_id);
if (!access.isGroup) rows = this.filterCompanyScopeMenus(rows);
}
const map = new Map();
const tree = [];
rows.forEach(item => {
item.children = [];
map.set(item.id, item);
});
rows.forEach(item => {
const parent = map.get(item.parent_id);
if (parent) {
parent.children.push(item);
} else {
tree.push(item);
}
});
ctx.body = { code: 0, message: 'success', data: tree };
}
/**
* 所在公司权限的菜单收束:系统管理目录下仅保留「员工档案」,外部打单平台相关菜单隐藏
* @param {Array<Object>} rows 菜单行
* @return {Array<Object>} 过滤后的菜单行
*/
filterCompanyScopeMenus(rows) {
const removedIds = new Set();
const employee = rows.find(row => row.path === EMPLOYEE_MENU_PATH && Number(row.type) === 2);
if (employee) {
const systemId = Number(employee.parent_id);
rows
.filter(row => Number(row.parent_id) === systemId && Number(row.id) !== Number(employee.id))
.forEach(row => removedIds.add(Number(row.id)));
}
// 外部打单平台相关菜单仅集团权限可见
rows
.filter(row => GROUP_ONLY_MENU_PATHS.includes(row.path))
.forEach(row => removedIds.add(Number(row.id)));
// 父级被移除的子级一并移除,避免孤儿节点被挂到根节点
let changed = true;
while (changed) {
changed = false;
for (const row of rows) {
if (removedIds.has(Number(row.id))) continue;
if (removedIds.has(Number(row.parent_id))) {
removedIds.add(Number(row.id));
changed = true;
}
}
}
return rows.filter(row => !removedIds.has(Number(row.id)));
}
/**
* @summary 菜单详情
* @description 按主键查询菜单
* @router get /api/menus/{id}
* @apikey
* @request path integer *id 菜单主键ID
* @response 200 baseResponse success
*/
async show() {
const { ctx } = this;
const menu = await ctx.model.Menu.findByPk(ctx.params.id);
if (!menu) {
ctx.status = 404;
ctx.body = { code: 404, message: '菜单不存在' };
return;
}
ctx.body = { code: 0, message: 'success', data: menu };
}
/**
* @summary 新增菜单
* @description parent_id 不传默认为 0(根节点)
* @router post /api/menus
* @apikey
* @request body createMenuRequest *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 menu = await ctx.model.Menu.create({
parent_id: 0,
type: 2,
sort: 0,
status: 1,
...ctx.request.body,
});
ctx.body = { code: 0, message: 'success', data: { id: menu.id } };
}
/**
* @summary 更新菜单
* @description 未传入的字段保持原值
* @router put /api/menus/{id}
* @apikey
* @request path integer *id 菜单主键ID
* @request body updateMenuRequest *body
* @response 200 baseResponse success
*/
async update() {
const { ctx } = this;
const menu = await ctx.model.Menu.findByPk(ctx.params.id);
if (!menu) {
ctx.status = 404;
ctx.body = { code: 404, message: '菜单不存在' };
return;
}
await menu.update(ctx.request.body);
ctx.body = { code: 0, message: 'success', data: { id: menu.id } };
}
/**
* @summary 删除菜单
* @description 物理删除菜单;存在子菜单时需先删除子菜单
* @router delete /api/menus/{id}
* @apikey
* @request path integer *id 菜单主键ID
* @response 200 baseResponse success
*/
async destroy() {
const { ctx } = this;
const menu = await ctx.model.Menu.findByPk(ctx.params.id);
if (!menu) {
ctx.status = 404;
ctx.body = { code: 404, message: '菜单不存在' };
return;
}
await menu.destroy();
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
}
}
module.exports = MenuController;