fix: 完善文档权限接口接入
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { unwrapApiResponse, unwrapNullableApiResponse } from '@/utils/api-response'
|
||||
import { request } from '@/utils/request'
|
||||
import type { ApiResponse } from '@/types/common'
|
||||
import type { ApiResponse, PageResult } from '@/types/common'
|
||||
|
||||
import type {
|
||||
BackendCollection,
|
||||
@@ -46,24 +46,54 @@ export function getDepartments(campusId?: string): Promise<DepartmentRecord[]> {
|
||||
return Promise.resolve([])
|
||||
}
|
||||
|
||||
const params = campusId ? { page: 1, size: 200, campusId } : { page: 1, size: 200 }
|
||||
return getDepartmentPage(campusId).then((page) => page.records)
|
||||
}
|
||||
|
||||
return request
|
||||
.get<ApiResponse<BackendCollection<OrganizationResponseDto>>>('/v1/departments', {
|
||||
params,
|
||||
})
|
||||
.then(unwrapApiResponse)
|
||||
.then((data) =>
|
||||
getRecords(data).map((item) => ({
|
||||
id: item.id,
|
||||
campusId: item.campusId ?? '',
|
||||
parentId: item.parentId,
|
||||
code: item.code ?? item.id,
|
||||
name: item.name ?? item.departmentName ?? item.id,
|
||||
status: item.status ?? 'ENABLED',
|
||||
sortNo: item.sortNo ?? 0,
|
||||
})),
|
||||
)
|
||||
async function getDepartmentPage(
|
||||
campusId: string | undefined,
|
||||
page = 1,
|
||||
pageSize = 200,
|
||||
): Promise<PageResult<DepartmentRecord>> {
|
||||
if (useMockData) {
|
||||
return { records: [], total: 0, page, pageSize }
|
||||
}
|
||||
|
||||
const params: Record<string, string | number> = { page, size: pageSize }
|
||||
|
||||
if (campusId) {
|
||||
params.campusId = campusId
|
||||
}
|
||||
|
||||
const response = await request.get<ApiResponse<BackendCollection<OrganizationResponseDto>>>(
|
||||
'/v1/departments',
|
||||
{ params },
|
||||
)
|
||||
const data = unwrapApiResponse(response)
|
||||
const records = getRecords(data).map(mapDepartment)
|
||||
|
||||
return {
|
||||
records,
|
||||
total: Array.isArray(data) ? records.length : (data.total ?? records.length),
|
||||
page: Array.isArray(data) ? page : (data.page ?? page),
|
||||
pageSize: Array.isArray(data) ? pageSize : (data.size ?? pageSize),
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllDepartments(campusId?: string): Promise<DepartmentRecord[]> {
|
||||
const firstPage = await getDepartmentPage(campusId, 1, 200)
|
||||
const pageCount = Math.ceil(firstPage.total / firstPage.pageSize)
|
||||
|
||||
if (pageCount <= 1) {
|
||||
return firstPage.records
|
||||
}
|
||||
|
||||
const remainingPages = await Promise.all(
|
||||
Array.from({ length: pageCount - 1 }, (_, index) =>
|
||||
getDepartmentPage(campusId, index + 2, firstPage.pageSize),
|
||||
),
|
||||
)
|
||||
|
||||
return [firstPage, ...remainingPages].flatMap((page) => page.records)
|
||||
}
|
||||
|
||||
function mapCampus(data: OrganizationResponseDto): CampusRecord {
|
||||
|
||||
Reference in New Issue
Block a user