| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- /**
- * 可见范围(记录级数据权限)公共常量与工具。
- *
- * 取值与后端一致:0 全员/公开、1 本部门、2 仅创建人/仅本人。
- * 普通用户不能设置 0(仅超管可设置),前端只展示 1/2 两项;
- * 编辑历史全员数据时额外展示一个禁用项,允许保留原值。
- */
- export interface VisibleScopeOption {
- label: string;
- value: string;
- disabled?: boolean;
- }
- export interface DeptOption {
- id: string | number;
- label: string;
- children?: DeptOption[];
- }
- export const AGENT_SCOPE_OPTIONS: VisibleScopeOption[] = [
- { label: '全员', value: '0' },
- { label: '本部门', value: '1' },
- { label: '仅创建人', value: '2' },
- ];
- export const RESOURCE_SCOPE_OPTIONS: VisibleScopeOption[] = [
- { label: '公开', value: '0' },
- { label: '本部门', value: '1' },
- { label: '仅本人', value: '2' },
- ];
- /**
- * 按当前用户身份生成可见范围选项。
- *
- * @param options 全量选项
- * @param canPublic 当前用户是否可设置全员/公开(超管)
- * @param currentScope 编辑中的记录原值
- */
- export const scopeOptionsForUser = (
- options: VisibleScopeOption[],
- canPublic: boolean,
- currentScope?: string,
- ): VisibleScopeOption[] => {
- if (canPublic) {
- return options;
- }
- const allowed = options.filter((item) => item.value !== '0');
- if (currentScope === '0') {
- const publicOption = options.find((item) => item.value === '0');
- return publicOption
- ? [{ ...publicOption, label: `${publicOption.label}(仅管理员可设置)`, disabled: true }, ...allowed]
- : allowed;
- }
- return allowed;
- };
- export const visibleScopeLabel = (
- value: string | undefined,
- options: VisibleScopeOption[],
- ): string => options.find((item) => item.value === value)?.label ?? value ?? '-';
- /** 后端部门树(Hutool Tree 使用 name)转 a-tree-select 结构(label/value) */
- export const normalizeDeptTree = (nodes: any[] = []): DeptOption[] =>
- (nodes ?? []).map((node) => ({
- id: node.id,
- label: node.label ?? node.name ?? String(node.id),
- children: normalizeDeptTree(node.children ?? []),
- }));
- /** 在部门树中查找部门名称 */
- export const findDeptLabel = (nodes: DeptOption[] = [], id?: string | number): string => {
- if (id === undefined || id === null || id === '') {
- return '';
- }
- for (const node of nodes) {
- if (String(node.id) === String(id)) {
- return node.label;
- }
- const found = findDeptLabel(node.children ?? [], id);
- if (found) {
- return found;
- }
- }
- return '';
- };
|