index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <XTable @register="registerTable">
  5. <template #toolbar_buttons>
  6. <!-- 操作:新增 -->
  7. <XButton
  8. type="primary"
  9. preIcon="ep:zoom-in"
  10. :title="t('action.add')"
  11. v-hasPermi="['bpm:user-group:create']"
  12. @click="handleCreate()"
  13. />
  14. </template>
  15. <template #memberUserIds_default="{ row }">
  16. <span v-for="userId in row.memberUserIds" :key="userId">
  17. {{ getUserNickname(userId) }} &nbsp;
  18. </span>
  19. </template>
  20. <template #actionbtns_default="{ row }">
  21. <!-- 操作:修改 -->
  22. <XTextButton
  23. preIcon="ep:edit"
  24. :title="t('action.edit')"
  25. v-hasPermi="['bpm:user-group:update']"
  26. @click="handleUpdate(row.id)"
  27. />
  28. <!-- 操作:详情 -->
  29. <XTextButton
  30. preIcon="ep:view"
  31. :title="t('action.detail')"
  32. v-hasPermi="['bpm:user-group:query']"
  33. @click="handleDetail(row.id)"
  34. />
  35. <!-- 操作:删除 -->
  36. <XTextButton
  37. preIcon="ep:delete"
  38. :title="t('action.del')"
  39. v-hasPermi="['bpm:user-group:delete']"
  40. @click="deleteData(row.id)"
  41. />
  42. </template>
  43. </XTable>
  44. </ContentWrap>
  45. <XModal v-model="dialogVisible" :title="dialogTitle">
  46. <!-- 对话框(添加 / 修改) -->
  47. <Form
  48. v-if="['create', 'update'].includes(actionType)"
  49. :schema="allSchemas.formSchema"
  50. :rules="rules"
  51. ref="formRef"
  52. />
  53. <template #memberUserIds="form">
  54. <el-select v-model="form['memberUserIds']">
  55. <el-option v-for="item in users" :key="item.id" :label="item.nickname" :value="item.id" />
  56. </el-select>
  57. </template>
  58. <!-- 对话框(详情) -->
  59. <Descriptions
  60. v-if="actionType === 'detail'"
  61. :schema="allSchemas.detailSchema"
  62. :data="detailData"
  63. />
  64. <!-- 操作按钮 -->
  65. <template #footer>
  66. <!-- 按钮:保存 -->
  67. <XButton
  68. v-if="['create', 'update'].includes(actionType)"
  69. type="primary"
  70. :title="t('action.save')"
  71. :loading="actionLoading"
  72. @click="submitForm()"
  73. />
  74. <!-- 按钮:关闭 -->
  75. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  76. </template>
  77. </XModal>
  78. </template>
  79. <script setup lang="ts">
  80. import { onMounted, ref } from 'vue'
  81. // 业务相关的 import
  82. import * as UserGroupApi from '@/api/bpm/userGroup'
  83. import { getListSimpleUsersApi, UserVO } from '@/api/system/user'
  84. import { allSchemas } from './group.data'
  85. import { FormExpose } from '@/components/Form'
  86. const { t } = useI18n() // 国际化
  87. const message = useMessage() // 消息弹窗
  88. // 列表相关的变量
  89. const [registerTable, { reload, deleteData }] = useXTable({
  90. allSchemas: allSchemas,
  91. getListApi: UserGroupApi.getUserGroupPageApi,
  92. deleteApi: UserGroupApi.deleteUserGroupApi
  93. })
  94. // 用户列表
  95. const users = ref<UserVO[]>([])
  96. const getUserNickname = (userId) => {
  97. for (const user of users.value) {
  98. if (user.id === userId) {
  99. return user.nickname
  100. }
  101. }
  102. return '未知(' + userId + ')'
  103. }
  104. // ========== CRUD 相关 ==========
  105. const actionLoading = ref(false) // 遮罩层
  106. const actionType = ref('') // 操作按钮的类型
  107. const dialogVisible = ref(false) // 是否显示弹出层
  108. const dialogTitle = ref('edit') // 弹出层标题
  109. const formRef = ref<FormExpose>() // 表单 Ref
  110. const detailData = ref() // 详情 Ref
  111. // 设置标题
  112. const setDialogTile = (type: string) => {
  113. dialogTitle.value = t('action.' + type)
  114. actionType.value = type
  115. dialogVisible.value = true
  116. }
  117. // 新增操作
  118. const handleCreate = () => {
  119. setDialogTile('create')
  120. }
  121. // 修改操作
  122. const handleUpdate = async (rowId: number) => {
  123. setDialogTile('update')
  124. // 设置数据
  125. const res = await UserGroupApi.getUserGroupApi(rowId)
  126. unref(formRef)?.setValues(res)
  127. }
  128. // 详情操作
  129. const handleDetail = async (rowId: number) => {
  130. setDialogTile('detail')
  131. detailData.value = await UserGroupApi.getUserGroupApi(rowId)
  132. }
  133. // 提交按钮
  134. const submitForm = async () => {
  135. const elForm = unref(formRef)?.getElFormRef()
  136. if (!elForm) return
  137. elForm.validate(async (valid) => {
  138. if (valid) {
  139. actionLoading.value = true
  140. // 提交请求
  141. try {
  142. const data = unref(formRef)?.formModel as UserGroupApi.UserGroupVO
  143. if (actionType.value === 'create') {
  144. await UserGroupApi.createUserGroupApi(data)
  145. message.success(t('common.createSuccess'))
  146. } else {
  147. await UserGroupApi.updateUserGroupApi(data)
  148. message.success(t('common.updateSuccess'))
  149. }
  150. dialogVisible.value = false
  151. } finally {
  152. actionLoading.value = false
  153. // 刷新列表
  154. await reload()
  155. }
  156. }
  157. })
  158. }
  159. // ========== 初始化 ==========
  160. onMounted(() => {
  161. getListSimpleUsersApi().then((data) => {
  162. users.value = data
  163. })
  164. })
  165. </script>