visibleScope.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * 可见范围(记录级数据权限)公共常量与工具。
  3. *
  4. * 取值与后端一致:0 全员/公开、1 本部门、2 仅创建人/仅本人。
  5. * 普通用户不能设置 0(仅超管可设置),前端只展示 1/2 两项;
  6. * 编辑历史全员数据时额外展示一个禁用项,允许保留原值。
  7. */
  8. export interface VisibleScopeOption {
  9. label: string;
  10. value: string;
  11. disabled?: boolean;
  12. }
  13. export interface DeptOption {
  14. id: string | number;
  15. label: string;
  16. children?: DeptOption[];
  17. }
  18. export const AGENT_SCOPE_OPTIONS: VisibleScopeOption[] = [
  19. { label: '全员', value: '0' },
  20. { label: '本部门', value: '1' },
  21. { label: '仅创建人', value: '2' },
  22. ];
  23. export const RESOURCE_SCOPE_OPTIONS: VisibleScopeOption[] = [
  24. { label: '公开', value: '0' },
  25. { label: '本部门', value: '1' },
  26. { label: '仅本人', value: '2' },
  27. ];
  28. /**
  29. * 按当前用户身份生成可见范围选项。
  30. *
  31. * @param options 全量选项
  32. * @param canPublic 当前用户是否可设置全员/公开(超管)
  33. * @param currentScope 编辑中的记录原值
  34. */
  35. export const scopeOptionsForUser = (
  36. options: VisibleScopeOption[],
  37. canPublic: boolean,
  38. currentScope?: string,
  39. ): VisibleScopeOption[] => {
  40. if (canPublic) {
  41. return options;
  42. }
  43. const allowed = options.filter((item) => item.value !== '0');
  44. if (currentScope === '0') {
  45. const publicOption = options.find((item) => item.value === '0');
  46. return publicOption
  47. ? [{ ...publicOption, label: `${publicOption.label}(仅管理员可设置)`, disabled: true }, ...allowed]
  48. : allowed;
  49. }
  50. return allowed;
  51. };
  52. export const visibleScopeLabel = (
  53. value: string | undefined,
  54. options: VisibleScopeOption[],
  55. ): string => options.find((item) => item.value === value)?.label ?? value ?? '-';
  56. /** 后端部门树(Hutool Tree 使用 name)转 a-tree-select 结构(label/value) */
  57. export const normalizeDeptTree = (nodes: any[] = []): DeptOption[] =>
  58. (nodes ?? []).map((node) => ({
  59. id: node.id,
  60. label: node.label ?? node.name ?? String(node.id),
  61. children: normalizeDeptTree(node.children ?? []),
  62. }));
  63. /** 在部门树中查找部门名称 */
  64. export const findDeptLabel = (nodes: DeptOption[] = [], id?: string | number): string => {
  65. if (id === undefined || id === null || id === '') {
  66. return '';
  67. }
  68. for (const node of nodes) {
  69. if (String(node.id) === String(id)) {
  70. return node.label;
  71. }
  72. const found = findDeptLabel(node.children ?? [], id);
  73. if (found) {
  74. return found;
  75. }
  76. }
  77. return '';
  78. };