index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 handleDelete = (row: UserGroupVO) => {
  78. delList(row.id, false)
  79. }
  80. // 根据用户名获取用户真实名
  81. const getUserNickName = (userId: number) => {
  82. for (const user of userOptions.value) {
  83. if (user.id === userId) return user.nickname
  84. }
  85. return '未知(' + userId + ')'
  86. }
  87. // ========== 详情相关 ==========
  88. const detailRef = ref() // 详情 Ref
  89. // 详情操作
  90. const handleDetail = async (row: UserGroupVO) => {
  91. // 设置数据
  92. detailRef.value = row
  93. setDialogTile('detail')
  94. }
  95. // ========== 初始化 ==========
  96. onMounted(async () => {
  97. await getList()
  98. await getUserOptions()
  99. })
  100. </script>
  101. <template>
  102. <!-- 搜索工作区 -->
  103. <ContentWrap>
  104. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  105. </ContentWrap>
  106. <ContentWrap>
  107. <!-- 操作工具栏 -->
  108. <div class="mb-10px">
  109. <el-button type="primary" v-hasPermi="['bpm:user-group:create']" @click="handleCreate">
  110. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  111. </el-button>
  112. </div>
  113. <!-- 列表 -->
  114. <Table
  115. :columns="allSchemas.tableColumns"
  116. :selection="false"
  117. :data="tableObject.tableList"
  118. :loading="tableObject.loading"
  119. :pagination="{
  120. total: tableObject.total
  121. }"
  122. v-model:pageSize="tableObject.pageSize"
  123. v-model:currentPage="tableObject.currentPage"
  124. @register="register"
  125. >
  126. <template #status="{ row }">
  127. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  128. </template>
  129. <template #memberUserIds="{ row }">
  130. <span v-for="userId in row.memberUserIds" :key="userId">
  131. {{ getUserNickName(userId) + ' ' }}
  132. </span>
  133. </template>
  134. <template #createTime="{ row }">
  135. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  136. </template>
  137. <template #action="{ row }">
  138. <el-button
  139. link
  140. type="primary"
  141. v-hasPermi="['bpm:user-group:update']"
  142. @click="handleUpdate(row)"
  143. >
  144. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  145. </el-button>
  146. <el-button
  147. link
  148. type="primary"
  149. v-hasPermi="['bpm:user-group:update']"
  150. @click="handleDetail(row)"
  151. >
  152. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  153. </el-button>
  154. <el-button
  155. link
  156. type="primary"
  157. v-hasPermi="['bpm:user-group:delete']"
  158. @click="handleDelete(row)"
  159. >
  160. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  161. </el-button>
  162. </template>
  163. </Table>
  164. </ContentWrap>
  165. <Dialog v-model="dialogVisible" :title="dialogTitle">
  166. <!-- 对话框(添加 / 修改) -->
  167. <Form
  168. v-if="['create', 'update'].includes(actionType)"
  169. :schema="allSchemas.formSchema"
  170. :rules="rules"
  171. ref="formRef"
  172. >
  173. <template #memberUserIds>
  174. <el-select v-model="userIds" multiple>
  175. <el-option
  176. v-for="item in userOptions"
  177. :key="item.id"
  178. :label="item.nickname"
  179. :value="item.id"
  180. />
  181. </el-select>
  182. </template>
  183. </Form>
  184. <!-- 对话框(详情) -->
  185. <Descriptions
  186. v-if="actionType === 'detail'"
  187. :schema="allSchemas.detailSchema"
  188. :data="detailRef"
  189. >
  190. <template #memberUserIds="{ row }">
  191. <span v-for="userId in row.memberUserIds" :key="userId">
  192. {{ getUserNickName(userId) + ' ' }}
  193. </span>
  194. </template>
  195. <template #status="{ row }">
  196. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  197. </template>
  198. <template #createTime="{ row }">
  199. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  200. </template>
  201. </Descriptions>
  202. <!-- 操作按钮 -->
  203. <template #footer>
  204. <el-button
  205. v-if="['create', 'update'].includes(actionType)"
  206. type="primary"
  207. :loading="actionLoading"
  208. @click="submitForm"
  209. >
  210. {{ t('action.save') }}
  211. </el-button>
  212. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  213. </template>
  214. </Dialog>
  215. </template>