rbacRoleService.ts 1.27 KB
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,
  });
}

/**
 * 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]);
}