提交 | 用户 | 时间
|
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> |
|
42 |
import { defaultProps, findTreeNode, handleTree } from '@/utils/tree' |
|
43 |
import * as DeptApi from '@/api/system/dept' |
|
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[]>([]) // 部门树形结构化 |
|
53 |
const userList = ref<UserApi.UserVO[]>([]) // 所有用户列表 |
|
54 |
const filteredUserList = ref<UserApi.UserVO[]>([]) // 当前部门过滤后的用户列表 |
|
55 |
const selectedUserIdList: any = ref([]) // 选中的用户列表 |
|
56 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
57 |
const formLoading = ref(false) // 表单的加载中 |
|
58 |
const activityId = ref() |
|
59 |
|
|
60 |
/** 计算属性:合并已选择的用户和当前部门过滤后的用户 */ |
|
61 |
const transferUserList = computed(() => { |
|
62 |
// 1.1 获取所有已选择的用户 |
|
63 |
const selectedUsers = userList.value.filter((user: any) => |
|
64 |
selectedUserIdList.value.includes(user.id) |
|
65 |
) |
|
66 |
|
|
67 |
// 1.2 获取当前部门过滤后的未选择用户 |
|
68 |
const filteredUnselectedUsers = filteredUserList.value.filter( |
|
69 |
(user: any) => !selectedUserIdList.value.includes(user.id) |
|
70 |
) |
|
71 |
|
|
72 |
// 2. 合并并去重 |
|
73 |
return [...selectedUsers, ...filteredUnselectedUsers] |
|
74 |
}) |
|
75 |
|
|
76 |
/** 打开弹窗 */ |
|
77 |
const open = async (id: number, selectedList?: any[]) => { |
|
78 |
activityId.value = id |
|
79 |
resetForm() |
|
80 |
|
|
81 |
// 加载部门、用户列表 |
|
82 |
deptTree.value = handleTree(await DeptApi.getSimpleDeptList()) |
|
83 |
userList.value = await UserApi.getSimpleUserList() |
|
84 |
|
|
85 |
// 初始状态下,过滤列表等于所有用户列表 |
|
86 |
filteredUserList.value = [...userList.value] |
|
87 |
selectedUserIdList.value = selectedList?.map((item: any) => item.id) || [] |
|
88 |
dialogVisible.value = true |
|
89 |
} |
|
90 |
|
|
91 |
/** 获取部门过滤后的用户列表 */ |
|
92 |
const getUserList = async (deptId?: number) => { |
|
93 |
formLoading.value = true |
|
94 |
try { |
|
95 |
// @ts-ignore |
|
96 |
// TODO @芋艿:替换到 simple List 暂不支持 deptId 过滤 |
|
97 |
// TODO @Zqqq:这个,可以使用前端过滤么?通过 deptList 获取到 deptId 子节点,然后去 userList |
|
98 |
const data = await UserApi.getUserPage({ pageSize: 100, pageNo: 1, deptId }) |
|
99 |
// 更新过滤后的用户列表 |
|
100 |
filteredUserList.value = data.list |
|
101 |
} finally { |
|
102 |
formLoading.value = false |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
/** 提交选择 */ |
|
107 |
const submitForm = async () => { |
|
108 |
try { |
|
109 |
message.success(t('common.updateSuccess')) |
|
110 |
dialogVisible.value = false |
|
111 |
// 从所有用户列表中筛选出已选择的用户 |
|
112 |
const emitUserList = userList.value.filter((user: any) => |
|
113 |
selectedUserIdList.value.includes(user.id) |
|
114 |
) |
|
115 |
// 发送操作成功的事件 |
|
116 |
emit('confirm', activityId.value, emitUserList) |
|
117 |
} finally { |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
/** 重置表单 */ |
|
122 |
const resetForm = () => { |
|
123 |
deptTree.value = [] |
|
124 |
userList.value = [] |
|
125 |
filteredUserList.value = [] |
|
126 |
selectedUserIdList.value = [] |
|
127 |
} |
|
128 |
|
|
129 |
/** 处理部门被点击 */ |
|
130 |
const handleNodeClick = (row: { [key: string]: any }) => { |
|
131 |
getUserList(row.id) |
|
132 |
} |
|
133 |
|
|
134 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
135 |
</script> |
|
136 |
|
|
137 |
<style lang="scss" scoped> |
|
138 |
:deep() { |
|
139 |
.el-transfer { |
|
140 |
display: flex; |
|
141 |
} |
|
142 |
.el-transfer__buttons { |
|
143 |
display: flex !important; |
|
144 |
flex-direction: column-reverse; |
|
145 |
justify-content: center; |
|
146 |
gap: 20px; |
|
147 |
.el-transfer__button:nth-child(2) { |
|
148 |
margin: 0; |
|
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
</style> |