提交 | 用户 | 时间
|
3e359e
|
1 |
<template> |
H |
2 |
<Dialog v-model="dialogVisible" title="人员选择" width="800"> |
|
3 |
<el-row class="gap2" v-loading="formLoading"> |
|
4 |
<el-col :span="6"> |
|
5 |
<ContentWrap class="h-1/1"> |
|
6 |
<el-tree |
|
7 |
ref="treeRef" |
|
8 |
:data="deptTree" |
|
9 |
:expand-on-click-node="false" |
|
10 |
:props="defaultProps" |
|
11 |
default-expand-all |
|
12 |
highlight-current |
|
13 |
node-key="id" |
|
14 |
@node-click="handleNodeClick" |
|
15 |
/> |
|
16 |
</ContentWrap> |
|
17 |
</el-col> |
|
18 |
<el-col :span="17"> |
|
19 |
<el-transfer |
|
20 |
v-model="selectedUserIdList" |
|
21 |
:titles="['未选', '已选']" |
|
22 |
filterable |
|
23 |
filter-placeholder="搜索成员" |
|
24 |
:data="transferUserList" |
|
25 |
:props="{ label: 'nickname', key: 'id' }" |
|
26 |
/> |
|
27 |
</el-col> |
|
28 |
</el-row> |
|
29 |
<template #footer> |
|
30 |
<el-button |
|
31 |
:disabled="formLoading || !selectedUserIdList?.length" |
|
32 |
type="primary" |
|
33 |
@click="submitForm" |
|
34 |
> |
|
35 |
确 定 |
|
36 |
</el-button> |
|
37 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
38 |
</template> |
|
39 |
</Dialog> |
|
40 |
</template> |
|
41 |
<script lang="ts" setup> |
c9a6f7
|
42 |
import { defaultProps, handleTree } from '@/utils/tree' |
3e359e
|
43 |
import * as DeptApi from '@/api/system/dept' |
H |
44 |
import * as UserApi from '@/api/system/user' |
|
45 |
|
|
46 |
defineOptions({ name: 'UserSelectForm' }) |
|
47 |
const emit = defineEmits<{ |
|
48 |
confirm: [id: any, userList: any[]] |
|
49 |
}>() |
|
50 |
const { t } = useI18n() // 国际 |
|
51 |
const message = useMessage() // 消息弹窗 |
|
52 |
const deptTree = ref<Tree[]>([]) // 部门树形结构化 |
c9a6f7
|
53 |
const deptList = ref<any[]>([]) // 保存扁平化的部门列表数据 |
3e359e
|
54 |
const userList = ref<UserApi.UserVO[]>([]) // 所有用户列表 |
H |
55 |
const filteredUserList = ref<UserApi.UserVO[]>([]) // 当前部门过滤后的用户列表 |
|
56 |
const selectedUserIdList: any = ref([]) // 选中的用户列表 |
|
57 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
58 |
const formLoading = ref(false) // 表单的加载中 |
|
59 |
const activityId = ref() |
|
60 |
|
|
61 |
/** 计算属性:合并已选择的用户和当前部门过滤后的用户 */ |
|
62 |
const transferUserList = computed(() => { |
|
63 |
// 1.1 获取所有已选择的用户 |
|
64 |
const selectedUsers = userList.value.filter((user: any) => |
|
65 |
selectedUserIdList.value.includes(user.id) |
|
66 |
) |
|
67 |
|
|
68 |
// 1.2 获取当前部门过滤后的未选择用户 |
|
69 |
const filteredUnselectedUsers = filteredUserList.value.filter( |
|
70 |
(user: any) => !selectedUserIdList.value.includes(user.id) |
|
71 |
) |
|
72 |
|
|
73 |
// 2. 合并并去重 |
|
74 |
return [...selectedUsers, ...filteredUnselectedUsers] |
|
75 |
}) |
|
76 |
|
|
77 |
/** 打开弹窗 */ |
|
78 |
const open = async (id: number, selectedList?: any[]) => { |
|
79 |
activityId.value = id |
|
80 |
resetForm() |
|
81 |
|
|
82 |
// 加载部门、用户列表 |
c9a6f7
|
83 |
const deptData = await DeptApi.getSimpleDeptList() |
H |
84 |
deptList.value = deptData // 保存扁平结构的部门数据 |
|
85 |
deptTree.value = handleTree(deptData) // 转换成树形结构 |
3e359e
|
86 |
userList.value = await UserApi.getSimpleUserList() |
H |
87 |
|
|
88 |
// 初始状态下,过滤列表等于所有用户列表 |
|
89 |
filteredUserList.value = [...userList.value] |
|
90 |
selectedUserIdList.value = selectedList?.map((item: any) => item.id) || [] |
|
91 |
dialogVisible.value = true |
|
92 |
} |
|
93 |
|
c9a6f7
|
94 |
/** 获取指定部门及其所有子部门的ID列表 */ |
H |
95 |
const getChildDeptIds = (deptId: number, deptList: any[]): number[] => { |
|
96 |
const ids = [deptId] |
|
97 |
const children = deptList.filter((dept) => dept.parentId === deptId) |
|
98 |
children.forEach((child) => { |
|
99 |
ids.push(...getChildDeptIds(child.id, deptList)) |
|
100 |
}) |
|
101 |
return ids |
|
102 |
} |
|
103 |
|
3e359e
|
104 |
/** 获取部门过滤后的用户列表 */ |
c9a6f7
|
105 |
const filterUserList = async (deptId?: number) => { |
3e359e
|
106 |
formLoading.value = true |
H |
107 |
try { |
c9a6f7
|
108 |
if (!deptId) { |
H |
109 |
// 如果没有选择部门,显示所有用户 |
|
110 |
filteredUserList.value = [...userList.value] |
|
111 |
return |
|
112 |
} |
|
113 |
|
|
114 |
// 直接使用已保存的部门列表数据进行过滤 |
|
115 |
const deptIds = getChildDeptIds(deptId, deptList.value) |
|
116 |
|
|
117 |
// 过滤出这些部门下的用户 |
|
118 |
filteredUserList.value = userList.value.filter((user) => deptIds.includes(user.deptId)) |
3e359e
|
119 |
} finally { |
H |
120 |
formLoading.value = false |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
/** 提交选择 */ |
|
125 |
const submitForm = async () => { |
|
126 |
try { |
|
127 |
message.success(t('common.updateSuccess')) |
|
128 |
dialogVisible.value = false |
|
129 |
// 从所有用户列表中筛选出已选择的用户 |
|
130 |
const emitUserList = userList.value.filter((user: any) => |
|
131 |
selectedUserIdList.value.includes(user.id) |
|
132 |
) |
|
133 |
// 发送操作成功的事件 |
|
134 |
emit('confirm', activityId.value, emitUserList) |
|
135 |
} finally { |
|
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
/** 重置表单 */ |
|
140 |
const resetForm = () => { |
|
141 |
deptTree.value = [] |
c9a6f7
|
142 |
deptList.value = [] |
3e359e
|
143 |
userList.value = [] |
H |
144 |
filteredUserList.value = [] |
|
145 |
selectedUserIdList.value = [] |
|
146 |
} |
|
147 |
|
|
148 |
/** 处理部门被点击 */ |
|
149 |
const handleNodeClick = (row: { [key: string]: any }) => { |
c9a6f7
|
150 |
filterUserList(row.id) |
3e359e
|
151 |
} |
H |
152 |
|
|
153 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
154 |
</script> |
|
155 |
|
|
156 |
<style lang="scss" scoped> |
|
157 |
:deep() { |
|
158 |
.el-transfer { |
|
159 |
display: flex; |
|
160 |
} |
|
161 |
.el-transfer__buttons { |
|
162 |
display: flex !important; |
|
163 |
flex-direction: column-reverse; |
|
164 |
justify-content: center; |
|
165 |
gap: 20px; |
|
166 |
.el-transfer__button:nth-child(2) { |
|
167 |
margin: 0; |
|
168 |
} |
|
169 |
} |
|
170 |
} |
|
171 |
</style> |