778b846fb3
dept.ts API: CRUD + buildTree 树形构建; DeptManagement 页面: 树形表格 + 新增/编辑/删除 + 权限守卫; 修复 validation-rules.ts 的 as const 导致 readonly 类型不兼容问题。
76 lines
1.7 KiB
TypeScript
76 lines
1.7 KiB
TypeScript
import request from '@/utils/request'
|
|
|
|
export interface DeptItem {
|
|
id: number
|
|
parentId: number
|
|
deptName: string
|
|
orderNum: number
|
|
leader: string
|
|
phone: string
|
|
email: string
|
|
status: number
|
|
createdAt: string
|
|
updatedAt: string
|
|
children?: DeptItem[]
|
|
}
|
|
|
|
export interface CreateDeptRequest {
|
|
parentId?: number
|
|
deptName: string
|
|
orderNum?: number
|
|
leader?: string
|
|
phone?: string
|
|
email?: string
|
|
status?: number
|
|
}
|
|
|
|
export interface UpdateDeptRequest {
|
|
parentId?: number
|
|
deptName?: string
|
|
orderNum?: number
|
|
leader?: string
|
|
phone?: string
|
|
email?: string
|
|
status?: number
|
|
}
|
|
|
|
function buildTree(list: DeptItem[]): DeptItem[] {
|
|
const map = new Map<number, DeptItem>()
|
|
const roots: DeptItem[] = []
|
|
for (const item of list) {
|
|
map.set(item.id, { ...item, children: [] })
|
|
}
|
|
for (const item of list) {
|
|
const node = map.get(item.id)!
|
|
if (item.parentId === 0 || !map.has(item.parentId)) {
|
|
roots.push(node)
|
|
} else {
|
|
const parent = map.get(item.parentId)
|
|
parent?.children?.push(node)
|
|
}
|
|
}
|
|
return roots
|
|
}
|
|
|
|
export const deptApi = {
|
|
getAll: async (): Promise<DeptItem[]> => {
|
|
const res = await request.get<DeptItem[]>('/depts')
|
|
const list = Array.isArray(res) ? res : []
|
|
return buildTree(list)
|
|
},
|
|
|
|
getById: async (id: number): Promise<DeptItem> => {
|
|
const res = await request.get<DeptItem>(`/depts/${id}`)
|
|
return res as unknown as DeptItem
|
|
},
|
|
|
|
create: (data: CreateDeptRequest) =>
|
|
request.post<DeptItem>('/depts', data),
|
|
|
|
update: (id: number, data: UpdateDeptRequest) =>
|
|
request.put<DeptItem>(`/depts/${id}`, data),
|
|
|
|
delete: (id: number) =>
|
|
request.delete<void>(`/depts/${id}`),
|
|
}
|