rbacRoleService.ts
2.94 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { createApiClient } from "../lib/apiClient";
import type { RoleDto } from "../types/role";
const api = createApiClient({
getToken: () => {
try {
return localStorage.getItem("access_token") ?? localStorage.getItem("token") ?? null;
} catch {
return null;
}
},
});
const PATH = "/rbac-role";
export type RbacRoleUpsertInput = {
roleName: string;
roleCode: string;
remark?: string | null;
dataScope?: number | null;
state: boolean;
orderNum?: number | null;
};
export async function createRbacRole(input: RbacRoleUpsertInput): Promise<RoleDto> {
return api.requestJson<RoleDto>({
path: PATH,
method: "POST",
body: input,
});
}
export async function updateRbacRole(id: string, input: RbacRoleUpsertInput): Promise<RoleDto> {
return api.requestJson<RoleDto>({
path: `${PATH}/${encodeURIComponent(id)}`,
method: "PUT",
body: input,
});
}
function extractMenuIdsFromRoleDetail(raw: unknown): string[] {
if (!raw || typeof raw !== "object") return [];
const r = raw as Record<string, unknown>;
// 常见命名:menuIds / MenuIds
const direct =
Array.isArray(r.menuIds) ? r.menuIds :
Array.isArray((r as any).MenuIds) ? (r as any).MenuIds :
undefined;
if (direct) return (direct as unknown[]).map(String);
// 包一层:{ data: { menuIds } }
const data = (r as any).data ?? (r as any).Data;
if (data && typeof data === "object") {
const dd = data as Record<string, unknown>;
const fromData = Array.isArray(dd.menuIds) ? dd.menuIds : Array.isArray(dd.MenuIds) ? dd.MenuIds : undefined;
if (fromData) return (fromData as unknown[]).map(String);
}
// 可能是 roleMenus: [{ menuId: '...' }] 之类
const roleMenus = Array.isArray((r as any).roleMenus) ? (r as any).roleMenus : Array.isArray((r as any).RoleMenus) ? (r as any).RoleMenus : undefined;
if (Array.isArray(roleMenus)) {
const ids: string[] = [];
for (const x of roleMenus) {
if (!x || typeof x !== "object") continue;
const rr = x as Record<string, unknown>;
const mid = rr.menuId ?? rr.MenuId ?? rr.id ?? rr.Id;
if (mid) ids.push(String(mid));
}
return ids;
}
return [];
}
/**
* Swagger(常见约定):DELETE /api/app/rbac-role,Body 为 ID 数组
*/
export async function deleteRbacRoles(ids: string[]): Promise<void> {
if (!ids.length) return;
await api.requestJson<unknown>({
path: PATH,
method: "DELETE",
body: ids,
});
}
export async function deleteRbacRole(id: string): Promise<void> {
await deleteRbacRoles([id]);
}
/**
* Swagger: GET /api/app/rbac-role/{id}
* 用于“查看当前角色菜单权限”,尽量从返回对象里提取 menuIds 字段。
*/
export async function getRbacRoleMenuIds(roleId: string, signal?: AbortSignal): Promise<string[]> {
const raw = await api.requestJson<unknown>({
path: `${PATH}/${encodeURIComponent(roleId)}`,
method: "GET",
signal,
});
return extractMenuIdsFromRoleDetail(raw);
}