刘光辉
6 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { getButtonAuthorizeSelector } from '#/api/system/buttonAuthorize';
 
interface ButtonResource {
  children?: ButtonResource[];
  enCode?: string;
}
 
export function collectButtonCodes(resources: ButtonResource[]): Set<string> {
  const codes = new Set<string>();
  const pending = [...resources];
  while (pending.length) {
    const resource = pending.pop()!;
    if (resource.enCode) codes.add(resource.enCode);
    if (Array.isArray(resource.children)) pending.push(...resource.children);
  }
  return codes;
}
 
export async function getAssociatedButtonCodes(menuId: string): Promise<null | Set<string>> {
  try {
    const response = await getButtonAuthorizeSelector(menuId);
    const resources = response?.data?.list;
    return Array.isArray(resources) ? collectButtonCodes(resources) : null;
  } catch {
    return null;
  }
}
 
export function canShowButton(code: string, granted: boolean, associatedCodes: null | Set<string>): boolean {
  return granted || (associatedCodes !== null && !associatedCodes.has(code));
}