index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <script setup lang="ts">
  2. import { ref, unref, onMounted } from 'vue'
  3. import dayjs from 'dayjs'
  4. import { ElMessage, ElSelect, ElOption } from 'element-plus'
  5. import { DICT_TYPE } from '@/utils/dict'
  6. import { useTable } from '@/hooks/web/useTable'
  7. import { useI18n } from '@/hooks/web/useI18n'
  8. import { FormExpose } from '@/components/Form'
  9. import type { UserGroupVO } from '@/api/bpm/userGroup/types'
  10. import { rules, allSchemas } from './group.data'
  11. import * as UserGroupApi from '@/api/bpm/userGroup'
  12. import { getListSimpleUsersApi } from '@/api/system/user'
  13. import { UserVO } from '@/api/system/user/types'
  14. const { t } = useI18n() // 国际化
  15. // ========== 列表相关 ==========
  16. const { register, tableObject, methods } = useTable<UserGroupVO>({
  17. getListApi: UserGroupApi.getUserGroupPageApi,
  18. delListApi: UserGroupApi.deleteUserGroupApi
  19. })
  20. const { getList, setSearchParams, delList } = methods
  21. // ========== CRUD 相关 ==========
  22. const actionLoading = ref(false) // 遮罩层
  23. const actionType = ref('') // 操作按钮的类型
  24. const dialogVisible = ref(false) // 是否显示弹出层
  25. const dialogTitle = ref('edit') // 弹出层标题
  26. const formRef = ref<FormExpose>() // 表单 Ref
  27. // ========== 用户选择 ==========
  28. const userIds = ref<number[]>([])
  29. const userOptions = ref<UserVO[]>([])
  30. const getUserOptions = async () => {
  31. const res = await getListSimpleUsersApi()
  32. userOptions.value.push(...res)
  33. }
  34. // 设置标题
  35. const setDialogTile = (type: string) => {
  36. dialogTitle.value = t('action.' + type)
  37. actionType.value = type
  38. dialogVisible.value = true
  39. }
  40. // 新增操作
  41. const handleCreate = () => {
  42. setDialogTile('create')
  43. userIds.value = []
  44. // 重置表单
  45. unref(formRef)?.getElFormRef()?.resetFields()
  46. }
  47. // 修改操作
  48. const handleUpdate = async (row: UserGroupVO) => {
  49. setDialogTile('update')
  50. // 设置数据
  51. const res = await UserGroupApi.getUserGroupApi(row.id)
  52. userIds.value = res.memberUserIds
  53. unref(formRef)?.setValues(res)
  54. }
  55. // 提交按钮
  56. const submitForm = async () => {
  57. actionLoading.value = true
  58. // 提交请求
  59. try {
  60. const data = unref(formRef)?.formModel as UserGroupVO
  61. data.memberUserIds = userIds.value
  62. if (actionType.value === 'create') {
  63. await UserGroupApi.createUserGroupApi(data)
  64. ElMessage.success(t('common.createSuccess'))
  65. } else {
  66. await UserGroupApi.updateUserGroupApi(data)
  67. ElMessage.success(t('common.updateSuccess'))
  68. }
  69. // 操作成功,重新加载列表
  70. dialogVisible.value = false
  71. await getList()
  72. } finally {
  73. actionLoading.value = false
  74. }
  75. }
  76. // 根据用户名获取用户真实名
  77. const getUserNickName = (userId: number) => {
  78. for (const user of userOptions.value) {
  79. if (user.id === userId) return user.nickname
  80. }
  81. return '未知(' + userId + ')'
  82. }
  83. // ========== 详情相关 ==========
  84. const detailRef = ref() // 详情 Ref
  85. // 详情操作
  86. const handleDetail = async (row: UserGroupVO) => {
  87. // 设置数据
  88. detailRef.value = row
  89. setDialogTile('detail')
  90. }
  91. // ========== 初始化 ==========
  92. onMounted(async () => {
  93. await getList()
  94. await getUserOptions()
  95. })
  96. </script>
  97. <template>
  98. <!-- 搜索工作区 -->
  99. <ContentWrap>
  100. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  101. </ContentWrap>
  102. <ContentWrap>
  103. <!-- 操作工具栏 -->
  104. <div class="mb-10px">
  105. <el-button type="primary" v-hasPermi="['bpm:user-group:create']" @click="handleCreate">
  106. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  107. </el-button>
  108. </div>
  109. <!-- 列表 -->
  110. <Table
  111. :columns="allSchemas.tableColumns"
  112. :selection="false"
  113. :data="tableObject.tableList"
  114. :loading="tableObject.loading"
  115. :pagination="{
  116. total: tableObject.total
  117. }"
  118. v-model:pageSize="tableObject.pageSize"
  119. v-model:currentPage="tableObject.currentPage"
  120. @register="register"
  121. >
  122. <template #status="{ row }">
  123. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  124. </template>
  125. <template #memberUserIds="{ row }">
  126. <span v-for="userId in row.memberUserIds" :key="userId">
  127. {{ getUserNickName(userId) + ' ' }}
  128. </span>
  129. </template>
  130. <template #createTime="{ row }">
  131. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  132. </template>
  133. <template #action="{ row }">
  134. <el-button
  135. link
  136. type="primary"
  137. v-hasPermi="['bpm:user-group:update']"
  138. @click="handleUpdate(row)"
  139. >
  140. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  141. </el-button>
  142. <el-button
  143. link
  144. type="primary"
  145. v-hasPermi="['bpm:user-group:update']"
  146. @click="handleDetail(row)"
  147. >
  148. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  149. </el-button>
  150. <el-button
  151. link
  152. type="primary"
  153. v-hasPermi="['bpm:user-group:delete']"
  154. @click="delList(row.id, false)"
  155. >
  156. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  157. </el-button>
  158. </template>
  159. </Table>
  160. </ContentWrap>
  161. <Dialog v-model="dialogVisible" :title="dialogTitle">
  162. <!-- 对话框(添加 / 修改) -->
  163. <Form
  164. v-if="['create', 'update'].includes(actionType)"
  165. :schema="allSchemas.formSchema"
  166. :rules="rules"
  167. ref="formRef"
  168. >
  169. <template #memberUserIds>
  170. <el-select v-model="userIds" multiple>
  171. <el-option
  172. v-for="item in userOptions"
  173. :key="item.id"
  174. :label="item.nickname"
  175. :value="item.id"
  176. />
  177. </el-select>
  178. </template>
  179. </Form>
  180. <!-- 对话框(详情) -->
  181. <Descriptions
  182. v-if="actionType === 'detail'"
  183. :schema="allSchemas.detailSchema"
  184. :data="detailRef"
  185. >
  186. <template #memberUserIds="{ row }">
  187. <span v-for="userId in row.memberUserIds" :key="userId">
  188. {{ getUserNickName(userId) + ' ' }}
  189. </span>
  190. </template>
  191. <template #status="{ row }">
  192. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  193. </template>
  194. <template #createTime="{ row }">
  195. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  196. </template>
  197. </Descriptions>
  198. <!-- 操作按钮 -->
  199. <template #footer>
  200. <el-button
  201. v-if="['create', 'update'].includes(actionType)"
  202. type="primary"
  203. :loading="actionLoading"
  204. @click="submitForm"
  205. >
  206. {{ t('action.save') }}
  207. </el-button>
  208. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  209. </template>
  210. </Dialog>
  211. </template>