fix: 接入用户与权限后端接口
This commit is contained in:
@@ -8,11 +8,18 @@ import type {
|
||||
BackendCollection,
|
||||
BackendPage,
|
||||
CreateUserRequest,
|
||||
PasswordResetRequest,
|
||||
UpdateUserRequest,
|
||||
UserImportCommitRequest,
|
||||
UserImportJobResponseDto,
|
||||
UserImportPreviewResponseDto,
|
||||
UserManagementUpdateRequest,
|
||||
UserListResponse,
|
||||
UserQuery,
|
||||
UserRecord,
|
||||
UserResponseDto,
|
||||
UserRolesResponseDto,
|
||||
UserRolesUpdateRequest,
|
||||
} from './types'
|
||||
|
||||
const useMockData = import.meta.env.VITE_USE_MOCK !== 'false'
|
||||
@@ -70,24 +77,37 @@ function formatDateTime(value: string | null | undefined) {
|
||||
}
|
||||
|
||||
function normalizeStatus(value: string | null | undefined): UserRecord['status'] {
|
||||
if (value === 'LOCKED') {
|
||||
return 'locked'
|
||||
}
|
||||
|
||||
return value === 'DISABLED' || value === 'INACTIVE' ? 'disabled' : 'enabled'
|
||||
}
|
||||
|
||||
function mapUser(dto: UserResponseDto): UserRecord {
|
||||
const roles = dto.roles ?? []
|
||||
const roleNames = roles.map((role) => role.name).filter(Boolean)
|
||||
|
||||
return {
|
||||
id: dto.id,
|
||||
name: dto.displayName,
|
||||
account: dto.username,
|
||||
department: dto.departmentId ? '未知科室' : '未指定科室',
|
||||
role: '未返回角色',
|
||||
department: dto.departmentName ?? (dto.departmentId ? '未知科室' : '未指定科室'),
|
||||
role: roleNames.length ? roleNames.join('、') : '未分配角色',
|
||||
status: normalizeStatus(dto.status),
|
||||
lastLoginAt: formatDateTime(dto.lastLoginAt),
|
||||
employeeNo: dto.employeeNo,
|
||||
phone: dto.phone,
|
||||
email: dto.email,
|
||||
campusId: dto.campusId,
|
||||
campusName: dto.campusName ?? undefined,
|
||||
departmentId: dto.departmentId,
|
||||
departmentName: dto.departmentName ?? undefined,
|
||||
roleNames,
|
||||
roleIds: roles.map((role) => role.id),
|
||||
dataScope: dto.dataScope ?? '—',
|
||||
version: dto.version ?? undefined,
|
||||
passwordChangeRequired: dto.passwordChangeRequired,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,6 +223,113 @@ export function updateUser(id: string, payload: UpdateUserRequest): Promise<User
|
||||
.then(mapUser)
|
||||
}
|
||||
|
||||
export function updateUserManagement(
|
||||
id: string,
|
||||
payload: UserManagementUpdateRequest,
|
||||
): Promise<UserRecord> {
|
||||
if (useMockData) {
|
||||
return Promise.reject(new Error('Mock 模式不执行用户写操作'))
|
||||
}
|
||||
|
||||
return request
|
||||
.put<ApiResponse<UserResponseDto>>(`/v1/users/${encodeURIComponent(id)}/management`, payload)
|
||||
.then(unwrapApiResponse)
|
||||
.then(mapUser)
|
||||
}
|
||||
|
||||
export async function getUserRoles(userId: string): Promise<UserRolesResponseDto> {
|
||||
if (useMockData) {
|
||||
return {
|
||||
userId,
|
||||
roles: [],
|
||||
version: 0,
|
||||
updatedAt: '',
|
||||
}
|
||||
}
|
||||
|
||||
const response = await request.get<ApiResponse<UserRolesResponseDto>>(
|
||||
`/v1/users/${encodeURIComponent(userId)}/roles`,
|
||||
)
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
export async function updateUserRoles(
|
||||
userId: string,
|
||||
payload: UserRolesUpdateRequest,
|
||||
): Promise<UserRolesResponseDto> {
|
||||
if (useMockData) {
|
||||
return Promise.reject(new Error('Mock 模式不执行用户角色写操作'))
|
||||
}
|
||||
|
||||
const response = await request.put<ApiResponse<UserRolesResponseDto>>(
|
||||
`/v1/users/${encodeURIComponent(userId)}/roles`,
|
||||
payload,
|
||||
)
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
export async function resetUserPassword(
|
||||
userId: string,
|
||||
payload: PasswordResetRequest,
|
||||
): Promise<void> {
|
||||
if (useMockData) {
|
||||
throw new Error('Mock 模式不执行密码重置')
|
||||
}
|
||||
|
||||
const response = await request.post<ApiResponse<null>>(
|
||||
`/v1/users/${encodeURIComponent(userId)}/password/reset`,
|
||||
payload,
|
||||
)
|
||||
unwrapNullableApiResponse(response)
|
||||
}
|
||||
|
||||
export async function previewUserImport(
|
||||
file: File,
|
||||
campusId?: string,
|
||||
): Promise<UserImportPreviewResponseDto> {
|
||||
if (useMockData) {
|
||||
throw new Error('Mock 模式不执行用户导入')
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
|
||||
if (campusId) {
|
||||
formData.append('campusId', campusId)
|
||||
}
|
||||
|
||||
const response = await request.post<ApiResponse<UserImportPreviewResponseDto>>(
|
||||
'/v1/users/import/preview',
|
||||
formData,
|
||||
)
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
export async function commitUserImport(
|
||||
payload: UserImportCommitRequest,
|
||||
): Promise<UserImportJobResponseDto> {
|
||||
if (useMockData) {
|
||||
throw new Error('Mock 模式不执行用户导入')
|
||||
}
|
||||
|
||||
const response = await request.post<ApiResponse<UserImportJobResponseDto>>(
|
||||
'/v1/users/import/commit',
|
||||
payload,
|
||||
)
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
export async function getUserImportJob(jobId: string): Promise<UserImportJobResponseDto> {
|
||||
if (useMockData) {
|
||||
throw new Error('Mock 模式不执行用户导入')
|
||||
}
|
||||
|
||||
const response = await request.get<ApiResponse<UserImportJobResponseDto>>(
|
||||
`/v1/users/import/jobs/${encodeURIComponent(jobId)}`,
|
||||
)
|
||||
return unwrapApiResponse(response)
|
||||
}
|
||||
|
||||
export function deleteUser(id: string): Promise<void> {
|
||||
if (useMockData) {
|
||||
return Promise.reject(new Error('Mock 模式不执行用户写操作'))
|
||||
|
||||
Reference in New Issue
Block a user