feat: 接入权限接口
This commit is contained in:
@@ -1,113 +1,64 @@
|
||||
import { unwrapApiResponse } from '@/utils/api-response'
|
||||
import { mockFlags } from '@/utils/mock-flags'
|
||||
import { unwrapApiResponse, unwrapNullableApiResponse } from '@/utils/api-response'
|
||||
import { request } from '@/utils/request'
|
||||
import type { ApiResponse } from '@/types/common'
|
||||
|
||||
import type {
|
||||
BackendCollection,
|
||||
CreateRoleRequest,
|
||||
RolePermissionsResponseDto,
|
||||
RolePermissionsUpdateRequest,
|
||||
PermissionResourceNodeDto,
|
||||
RoleDataScopeRequest,
|
||||
RoleDataScopeResponseDto,
|
||||
RoleRecord,
|
||||
RoleQuery,
|
||||
RoleResponseDto,
|
||||
RoleSummaryResponseDto,
|
||||
UpdateRoleRequest,
|
||||
} from './types'
|
||||
|
||||
const useMockData = mockFlags.users
|
||||
|
||||
const fallbackColors = ['#6b5bd2', '#0e6e8c', '#0f9d6c', '#d9821f', '#c65d5d']
|
||||
|
||||
function normalizeRole(dto: RoleResponseDto, index: number, userCount?: number | null): RoleRecord {
|
||||
function normalizeRole(dto: RoleResponseDto, index: number): RoleRecord {
|
||||
return {
|
||||
id: dto.id,
|
||||
code: dto.code,
|
||||
name: dto.name,
|
||||
description: dto.description ?? '—',
|
||||
description: dto.description ?? '',
|
||||
color: fallbackColors[index % fallbackColors.length],
|
||||
userCount: userCount ?? undefined,
|
||||
status: dto.status ?? 'ENABLED',
|
||||
status: dto.status ?? 'DISABLED',
|
||||
menuPermissionCount: dto.menuPermissionCount ?? 0,
|
||||
buttonPermissionCount: dto.buttonPermissionCount ?? 0,
|
||||
dataScope: dto.dataScope ?? 'SELF',
|
||||
configured: dto.configured ?? false,
|
||||
createdAt: dto.createdAt,
|
||||
updatedAt: dto.updatedAt,
|
||||
version: dto.version ?? undefined,
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeRoleSummary(dto: RoleSummaryResponseDto, index: number): RoleRecord {
|
||||
return normalizeRole(
|
||||
{
|
||||
id: dto.roleId,
|
||||
code: dto.code,
|
||||
name: dto.name,
|
||||
description: dto.description,
|
||||
status: dto.status,
|
||||
},
|
||||
index,
|
||||
dto.userCount,
|
||||
function normalizeCollection<T>(collection: BackendCollection<T>): T[] {
|
||||
return Array.isArray(collection)
|
||||
? collection
|
||||
: (collection.records ?? collection.items ?? collection.content ?? [])
|
||||
}
|
||||
|
||||
export async function getRoles(): Promise<RoleRecord[]> {
|
||||
const response = await request.get<ApiResponse<BackendCollection<RoleResponseDto>>>('/v1/roles')
|
||||
return normalizeCollection(unwrapApiResponse(response)).map(normalizeRole)
|
||||
}
|
||||
|
||||
export async function getAssignableRoles(): Promise<RoleRecord[]> {
|
||||
const response =
|
||||
await request.get<ApiResponse<BackendCollection<RoleResponseDto>>>('/v1/roles/assignable')
|
||||
return normalizeCollection(unwrapApiResponse(response)).map(normalizeRole)
|
||||
}
|
||||
|
||||
export async function getRolePermissionTree(): Promise<PermissionResourceNodeDto[]> {
|
||||
const response = await request.get<ApiResponse<PermissionResourceNodeDto[]>>(
|
||||
'/v1/roles/permission-tree',
|
||||
)
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
function toBackendQuery(query: RoleQuery) {
|
||||
const params: Record<string, string | number> = {
|
||||
page: Math.max(query.page, 1),
|
||||
size: Math.min(Math.max(query.pageSize, 1), 200),
|
||||
}
|
||||
|
||||
if (query.keyword?.trim()) {
|
||||
params.keyword = query.keyword.trim()
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
export function getRoles(query: RoleQuery = { page: 1, pageSize: 200 }): Promise<RoleRecord[]> {
|
||||
if (useMockData) {
|
||||
return Promise.resolve([])
|
||||
}
|
||||
|
||||
return request
|
||||
.get<ApiResponse<BackendCollection<RoleResponseDto>>>('/v1/roles', {
|
||||
params: toBackendQuery(query),
|
||||
})
|
||||
.then(unwrapApiResponse)
|
||||
.then((data) => {
|
||||
const records = Array.isArray(data)
|
||||
? data
|
||||
: (data.records ?? data.items ?? data.content ?? [])
|
||||
return records.map((role, index) => normalizeRole(role, index))
|
||||
})
|
||||
}
|
||||
|
||||
export function getRoleSummaries(campusId?: string): Promise<RoleRecord[]> {
|
||||
if (useMockData) {
|
||||
return Promise.resolve([])
|
||||
}
|
||||
|
||||
return request
|
||||
.get<ApiResponse<BackendCollection<RoleSummaryResponseDto>>>('/v1/roles/summary', {
|
||||
params: campusId ? { campusId } : undefined,
|
||||
})
|
||||
.then(unwrapApiResponse)
|
||||
.then((data) => {
|
||||
const records = Array.isArray(data)
|
||||
? data
|
||||
: (data.records ?? data.items ?? data.content ?? [])
|
||||
return records.map(normalizeRoleSummary)
|
||||
})
|
||||
}
|
||||
|
||||
export async function getRolePermissions(roleId: string): Promise<RolePermissionsResponseDto> {
|
||||
if (useMockData) {
|
||||
return {
|
||||
roleId,
|
||||
permissions: [],
|
||||
version: 0,
|
||||
updatedAt: '',
|
||||
}
|
||||
}
|
||||
|
||||
const response = await request.get<ApiResponse<RolePermissionsResponseDto>>(
|
||||
export async function getRolePermissions(roleId: string): Promise<string[]> {
|
||||
const response = await request.get<ApiResponse<string[]>>(
|
||||
`/v1/roles/${encodeURIComponent(roleId)}/permissions`,
|
||||
)
|
||||
return unwrapApiResponse(response)
|
||||
@@ -115,51 +66,55 @@ export async function getRolePermissions(roleId: string): Promise<RolePermission
|
||||
|
||||
export async function updateRolePermissions(
|
||||
roleId: string,
|
||||
payload: RolePermissionsUpdateRequest,
|
||||
): Promise<RolePermissionsResponseDto> {
|
||||
if (useMockData) {
|
||||
throw new Error('Mock 模式不执行角色权限写操作')
|
||||
}
|
||||
|
||||
const response = await request.put<ApiResponse<RolePermissionsResponseDto>>(
|
||||
permissionIds: string[],
|
||||
): Promise<void> {
|
||||
const response = await request.put<ApiResponse<null>>(
|
||||
`/v1/roles/${encodeURIComponent(roleId)}/permissions`,
|
||||
payload,
|
||||
{ permissionIds },
|
||||
)
|
||||
unwrapNullableApiResponse(response)
|
||||
}
|
||||
|
||||
export async function getRoleDataScope(roleId: string): Promise<RoleDataScopeResponseDto> {
|
||||
const response = await request.get<ApiResponse<RoleDataScopeResponseDto>>(
|
||||
`/v1/roles/${encodeURIComponent(roleId)}/data-scope`,
|
||||
)
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
export function createRole(payload: CreateRoleRequest): Promise<RoleRecord> {
|
||||
if (useMockData) {
|
||||
return Promise.reject(new Error('Mock 模式不执行角色写操作'))
|
||||
}
|
||||
|
||||
return request
|
||||
.post<ApiResponse<RoleResponseDto>>('/v1/roles', payload)
|
||||
.then(unwrapApiResponse)
|
||||
.then((role) => normalizeRole(role, 0))
|
||||
export async function updateRoleDataScope(
|
||||
roleId: string,
|
||||
payload: RoleDataScopeRequest,
|
||||
): Promise<void> {
|
||||
const response = await request.put<ApiResponse<null>>(
|
||||
`/v1/roles/${encodeURIComponent(roleId)}/data-scope`,
|
||||
payload,
|
||||
)
|
||||
unwrapNullableApiResponse(response)
|
||||
}
|
||||
|
||||
export function updateRole(id: string, payload: UpdateRoleRequest): Promise<RoleRecord> {
|
||||
if (useMockData) {
|
||||
return Promise.reject(new Error('Mock 模式不执行角色写操作'))
|
||||
}
|
||||
|
||||
return request
|
||||
.put<ApiResponse<RoleResponseDto>>(`/v1/roles/${encodeURIComponent(id)}`, payload)
|
||||
.then(unwrapApiResponse)
|
||||
.then((role) => normalizeRole(role, 0))
|
||||
export async function createRole(payload: CreateRoleRequest): Promise<string> {
|
||||
const response = await request.post<ApiResponse<{ id: string }>>('/v1/roles', payload)
|
||||
return unwrapApiResponse(response).id
|
||||
}
|
||||
|
||||
export function deleteRole(id: string): Promise<void> {
|
||||
if (useMockData) {
|
||||
return Promise.reject(new Error('Mock 模式不执行角色写操作'))
|
||||
}
|
||||
|
||||
return request
|
||||
.delete<ApiResponse<null>>(`/v1/roles/${encodeURIComponent(id)}`)
|
||||
.then((response) => {
|
||||
if (response.code !== 0 && response.code !== '0') {
|
||||
throw new Error(response.message || '角色删除失败')
|
||||
}
|
||||
})
|
||||
export async function updateRole(id: string, payload: UpdateRoleRequest): Promise<void> {
|
||||
const response = await request.put<ApiResponse<null>>(
|
||||
`/v1/roles/${encodeURIComponent(id)}`,
|
||||
payload,
|
||||
)
|
||||
unwrapNullableApiResponse(response)
|
||||
}
|
||||
|
||||
export async function setRoleStatus(id: string, enabled: boolean): Promise<void> {
|
||||
const response = await request.put<ApiResponse<null>>(
|
||||
`/v1/roles/${encodeURIComponent(id)}/status`,
|
||||
{ enabled },
|
||||
)
|
||||
unwrapNullableApiResponse(response)
|
||||
}
|
||||
|
||||
export async function deleteRole(id: string): Promise<void> {
|
||||
const response = await request.delete<ApiResponse<null>>(`/v1/roles/${encodeURIComponent(id)}`)
|
||||
unwrapNullableApiResponse(response)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user