index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <script setup lang="ts" name="Group">
  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'
  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 userOptions = ref<UserVO[]>([])
  29. const getUserOptions = async () => {
  30. const res = await getListSimpleUsersApi()
  31. userOptions.value.push(...res)
  32. }
  33. // 设置标题
  34. const setDialogTile = (type: string) => {
  35. dialogTitle.value = t('action.' + type)
  36. actionType.value = type
  37. dialogVisible.value = true
  38. }
  39. // 新增操作
  40. const handleCreate = () => {
  41. setDialogTile('create')
  42. }
  43. // 修改操作
  44. const handleUpdate = async (row: UserGroupVO) => {
  45. setDialogTile('update')
  46. // 设置数据
  47. const res = await UserGroupApi.getUserGroupApi(row.id)
  48. unref(formRef)?.setValues(res)
  49. }
  50. // 提交按钮
  51. const submitForm = async () => {
  52. const elForm = unref(formRef)?.getElFormRef()
  53. if (!elForm) return
  54. elForm.validate(async (valid) => {
  55. if (valid) {
  56. actionLoading.value = true
  57. // 提交请求
  58. try {
  59. const data = unref(formRef)?.formModel as UserGroupVO
  60. if (actionType.value === 'create') {
  61. await UserGroupApi.createUserGroupApi(data)
  62. ElMessage.success(t('common.createSuccess'))
  63. } else {
  64. await UserGroupApi.updateUserGroupApi(data)
  65. ElMessage.success(t('common.updateSuccess'))
  66. }
  67. // 操作成功,重新加载列表
  68. dialogVisible.value = false
  69. await getList()
  70. } finally {
  71. actionLoading.value = false
  72. }
  73. }
  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. <XModal 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="form">
  170. <el-select v-model="form['memberUserIds']" 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. </Descriptions>
  192. <template #footer>
  193. <!-- 按钮:保存 -->
  194. <XButton
  195. v-if="['create', 'update'].includes(actionType)"
  196. type="primary"
  197. :title="t('action.save')"
  198. :loading="actionLoading"
  199. @click="submitForm()"
  200. />
  201. <!-- 按钮:关闭 -->
  202. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  203. </template>
  204. </XModal>
  205. </template>