index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <Dialog v-model="dialogVisible" title="人员选择" width="900">
  3. <el-row>
  4. <el-col :span="6">
  5. <el-tree
  6. ref="treeRef"
  7. :data="deptList"
  8. :expand-on-click-node="false"
  9. :props="defaultProps"
  10. default-expand-all
  11. highlight-current
  12. node-key="id"
  13. @node-click="handleNodeClick"
  14. />
  15. </el-col>
  16. <el-col :span="17" :offset="1">
  17. <el-transfer
  18. v-model="selectedUserIdList"
  19. :titles="['未选', '已选']"
  20. filterable
  21. filter-placeholder="搜索成员"
  22. :data="userList"
  23. :props="{ label: 'nickname', key: 'id' }"
  24. />
  25. </el-col>
  26. </el-row>
  27. <template #footer>
  28. <el-button
  29. :disabled="formLoading || !selectedUserIdList?.length"
  30. type="primary"
  31. @click="submitForm"
  32. >
  33. 确 定
  34. </el-button>
  35. <el-button @click="dialogVisible = false">取 消</el-button>
  36. </template>
  37. </Dialog>
  38. </template>
  39. <script lang="ts" setup>
  40. import { defaultProps, handleTree } from '@/utils/tree'
  41. import * as DeptApi from '@/api/system/dept'
  42. import * as UserApi from '@/api/system/user'
  43. defineOptions({ name: 'UserSelectForm' })
  44. const emit = defineEmits<{
  45. confirm: [id: any, userList: any[]]
  46. }>()
  47. const { t } = useI18n() // 国际
  48. const message = useMessage() // 消息弹窗
  49. const deptList = ref<Tree[]>([]) // 部门树形结构化
  50. const userList: any = ref([]) // 用户列表
  51. const selectedUserIdList: any = ref([]) // 选中的用户列表
  52. const dialogVisible = ref(false) // 弹窗的是否展示
  53. const formLoading = ref(false) // 表单的加载中
  54. const activityId = ref() // 关联的主键编号 TODO @goldenzqqq:这个 activityId 有没可能不传递。在使用 @submitForm="xxx()" 时,传递的参数。目的是,更加解耦一些。
  55. /** 打开弹窗 */
  56. const open = async (id: number, selectedList?: any[]) => {
  57. activityId.value = id
  58. // 重置表单
  59. resetForm()
  60. // 加载相关数据
  61. deptList.value = handleTree(await DeptApi.getSimpleDeptList())
  62. await getUserList()
  63. // 设置选中的用户列表
  64. selectedUserIdList.value = selectedList?.map((item: any) => item.id)
  65. // 设置可见
  66. dialogVisible.value = true
  67. }
  68. /** 获取用户列表 */
  69. const getUserList = async (deptId?: number) => {
  70. try {
  71. // @ts-ignore
  72. // TODO @芋艿:替换到 simple List
  73. const data = await UserApi.getUserPage({ pageSize: 100, pageNo: 1, deptId })
  74. userList.value = data.list
  75. } finally {
  76. }
  77. }
  78. /** 提交选择 */
  79. const submitForm = async () => {
  80. // 提交请求
  81. formLoading.value = true
  82. try {
  83. message.success(t('common.updateSuccess'))
  84. dialogVisible.value = false
  85. const emitUserList = userList.value.filter((user: any) =>
  86. selectedUserIdList.value.includes(user.id)
  87. )
  88. // 发送操作成功的事件
  89. emit('confirm', activityId.value, emitUserList)
  90. } finally {
  91. formLoading.value = false
  92. }
  93. }
  94. /** 重置表单 */
  95. const resetForm = () => {
  96. deptList.value = []
  97. userList.value = []
  98. selectedUserIdList.value = []
  99. }
  100. /** 处理部门被点击 */
  101. const handleNodeClick = (row: { [key: string]: any }) => {
  102. getUserList(row.id)
  103. }
  104. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  105. </script>