Blame view

美国版/Food Labeling Management Platform/src/services/rbacRoleService.ts 5.24 KB
de8956f8   杨鑫   平台端 门店,菜单,角色
1
  import { createApiClient } from "../lib/apiClient";
ef6b3255   杨鑫   修改BUG
2
  import { normalizeRoleFromApi, type PagedResultDto, type RoleDto, type RoleGetListInput } from "../types/role";
de8956f8   杨鑫   平台端 门店,菜单,角色
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  
  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;
ef6b3255   杨鑫   修改BUG
23
24
25
26
    /** 访问权限编码(与表单一致) */
    accessPermissionCodes?: string[] | null;
    /** 与 `accessPermissionCodes` 同源,提交时合并为 `accessPermissions` */
    accessPermissions?: string[] | null;
de8956f8   杨鑫   平台端 门店,菜单,角色
27
28
  };
  
ef6b3255   杨鑫   修改BUG
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
  function mergeUpsertAccessPermissions(input: RbacRoleUpsertInput): string[] {
    const merged = [...(input.accessPermissionCodes ?? []), ...(input.accessPermissions ?? [])];
    const seen = new Set<string>();
    const out: string[] = [];
    for (const x of merged) {
      if (typeof x !== "string") continue;
      const t = x.trim();
      if (!t || seen.has(t)) continue;
      seen.add(t);
      out.push(t);
    }
    return out;
  }
  
  /** 创建/更新请求体:`accessPermissions` 为 JSON 数组字符串 */
  function buildRbacRoleRequestBody(input: RbacRoleUpsertInput): Record<string, unknown> {
    const codes = mergeUpsertAccessPermissions(input);
    return {
      roleName: input.roleName,
      roleCode: input.roleCode,
      remark: input.remark ?? null,
      dataScope: input.dataScope ?? 0,
      state: input.state,
      orderNum: input.orderNum ?? null,
      accessPermissions: codes.length > 0 ? JSON.stringify(codes) : null,
    };
  }
  
  /**
   * 角色分页列表(与创建/更新同一套 `/rbac-role` 数据源)
   */
  export async function getRbacRoles(
    input: RoleGetListInput,
    signal?: AbortSignal,
  ): Promise<PagedResultDto<RoleDto>> {
    const raw = await api.requestJson<PagedResultDto<Record<string, unknown>>>({
      path: PATH,
      method: "GET",
      query: {
        SkipCount: input.skipCount,
        MaxResultCount: input.maxResultCount,
        RoleName: input.roleName,
        RoleCode: input.roleCode,
        State: input.state,
      },
      signal,
    });
    const itemsRaw = raw.items ?? (raw as { Items?: unknown[] }).Items ?? [];
    const items = (Array.isArray(itemsRaw) ? itemsRaw : []).map((x) =>
      normalizeRoleFromApi(x as Record<string, unknown>),
    );
    const total =
      typeof raw.totalCount === "number"
        ? raw.totalCount
        : typeof (raw as { TotalCount?: number }).TotalCount === "number"
          ? (raw as { TotalCount: number }).TotalCount
          : items.length;
    return { totalCount: total, items };
  }
  
de8956f8   杨鑫   平台端 门店,菜单,角色
89
  export async function createRbacRole(input: RbacRoleUpsertInput): Promise<RoleDto> {
ef6b3255   杨鑫   修改BUG
90
    const raw = await api.requestJson<unknown>({
de8956f8   杨鑫   平台端 门店,菜单,角色
91
92
      path: PATH,
      method: "POST",
ef6b3255   杨鑫   修改BUG
93
      body: buildRbacRoleRequestBody(input),
de8956f8   杨鑫   平台端 门店,菜单,角色
94
    });
ef6b3255   杨鑫   修改BUG
95
    return normalizeRoleFromApi(raw);
de8956f8   杨鑫   平台端 门店,菜单,角色
96
97
98
  }
  
  export async function updateRbacRole(id: string, input: RbacRoleUpsertInput): Promise<RoleDto> {
ef6b3255   杨鑫   修改BUG
99
    const raw = await api.requestJson<unknown>({
de8956f8   杨鑫   平台端 门店,菜单,角色
100
101
      path: `${PATH}/${encodeURIComponent(id)}`,
      method: "PUT",
ef6b3255   杨鑫   修改BUG
102
      body: buildRbacRoleRequestBody(input),
de8956f8   杨鑫   平台端 门店,菜单,角色
103
    });
ef6b3255   杨鑫   修改BUG
104
    return normalizeRoleFromApi(raw);
de8956f8   杨鑫   平台端 门店,菜单,角色
105
106
  }
  
abb6bea5   杨鑫   用户管理
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  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 [];
  }
  
de8956f8   杨鑫   平台端 门店,菜单,角色
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  /**
   * 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]);
  }
  
abb6bea5   杨鑫   用户管理
158
159
160
161
162
163
164
165
166
167
168
169
  /**
   * 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);
  }