92 lines
2.3 KiB
TypeScript
92 lines
2.3 KiB
TypeScript
import request from '../request'
|
|
|
|
export interface Dept {
|
|
deptId?: number
|
|
parentId?: number
|
|
ancestors?: string
|
|
deptCode?: string
|
|
deptName?: string
|
|
orderNum?: number
|
|
leader?: string
|
|
phone?: string
|
|
email?: string
|
|
status?: string
|
|
delFlag?: string
|
|
createBy?: string
|
|
createTime?: string
|
|
updateBy?: string
|
|
updateTime?: string
|
|
children?: Dept[]
|
|
}
|
|
|
|
export interface DeptQuery {
|
|
deptName?: string
|
|
status?: string
|
|
}
|
|
|
|
const BASE = '/system/dept'
|
|
|
|
/** 查询部门列表 */
|
|
export function listDept(query?: DeptQuery): Promise<{ data: Dept[] }> {
|
|
return request.get(`${BASE}/list`, { params: query })
|
|
}
|
|
|
|
/** 查询部门列表(排除节点) */
|
|
export function listDeptExcludeChild(deptId: number): Promise<{ data: Dept[] }> {
|
|
return request.get(`${BASE}/list/exclude/${deptId}`)
|
|
}
|
|
|
|
/** 查询部门详细 */
|
|
export function getDept(deptId: number): Promise<{ data: Dept }> {
|
|
return request.get(`${BASE}/${deptId}`)
|
|
}
|
|
|
|
/** 查询部门下拉树结构 */
|
|
export function getDeptTreeselect(): Promise<{ data: any[] }> {
|
|
return request.get(`${BASE}/treeselect`)
|
|
}
|
|
|
|
/** 根据角色ID查询部门树结构 */
|
|
export function getRoleDeptTreeselect(roleId: number): Promise<{ data: any }> {
|
|
return request.get(`${BASE}/roleDeptTreeselect/${roleId}`)
|
|
}
|
|
|
|
/** 新增部门 */
|
|
export function addDept(data: Partial<Dept>): Promise<void> {
|
|
return request.post(BASE, data)
|
|
}
|
|
|
|
/** 修改部门 */
|
|
export function updateDept(data: Partial<Dept>): Promise<void> {
|
|
return request.put(BASE, data)
|
|
}
|
|
|
|
/** 删除部门 */
|
|
export function delDept(deptId: number): Promise<void> {
|
|
return request.delete(`${BASE}/${deptId}`)
|
|
}
|
|
|
|
/** 构建部门树 */
|
|
export function handleTree(data: Dept[], idField = 'deptId', parentField = 'parentId'): Dept[] {
|
|
const map = new Map<number, Dept>()
|
|
const result: Dept[] = []
|
|
|
|
data.forEach(item => {
|
|
map.set(item[idField as keyof Dept] as number, { ...item, children: [] })
|
|
})
|
|
|
|
data.forEach(item => {
|
|
const current = map.get(item[idField as keyof Dept] as number)!
|
|
const parentId = item[parentField as keyof Dept] as number
|
|
if (parentId && map.has(parentId)) {
|
|
const parent = map.get(parentId)!
|
|
parent.children = parent.children || []
|
|
parent.children.push(current)
|
|
} else {
|
|
result.push(current)
|
|
}
|
|
})
|
|
|
|
return result
|
|
}
|