index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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" multiple>
  55. <el-option v-for="item in users" :key="item.id" :label="item.nickname" :value="item.id" />
  56. </el-select>
  57. </template>
  58. </Form>
  59. <!-- 对话框(详情) -->
  60. <Descriptions
  61. v-if="actionType === 'detail'"
  62. :schema="allSchemas.detailSchema"
  63. :data="detailData"
  64. >
  65. <template #memberUserIds="{ row }">
  66. <span v-for="userId in row.memberUserIds" :key="userId">
  67. {{ getUserNickname(userId) }} &nbsp;
  68. </span>
  69. </template>
  70. </Descriptions>
  71. <!-- 操作按钮 -->
  72. <template #footer>
  73. <!-- 按钮:保存 -->
  74. <XButton
  75. v-if="['create', 'update'].includes(actionType)"
  76. type="primary"
  77. :title="t('action.save')"
  78. :loading="actionLoading"
  79. @click="submitForm()"
  80. />
  81. <!-- 按钮:关闭 -->
  82. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  83. </template>
  84. </XModal>
  85. </template>
  86. <script setup lang="ts">
  87. import { onMounted, ref } from 'vue'
  88. // 业务相关的 import
  89. import * as UserGroupApi from '@/api/bpm/userGroup'
  90. import { getListSimpleUsersApi, UserVO } from '@/api/system/user'
  91. import { allSchemas } from './group.data'
  92. import { FormExpose } from '@/components/Form'
  93. const { t } = useI18n() // 国际化
  94. const message = useMessage() // 消息弹窗
  95. // 列表相关的变量
  96. const [registerTable, { reload, deleteData }] = useXTable({
  97. allSchemas: allSchemas,
  98. getListApi: UserGroupApi.getUserGroupPageApi,
  99. deleteApi: UserGroupApi.deleteUserGroupApi
  100. })
  101. // 用户列表
  102. const users = ref<UserVO[]>([])
  103. const getUserNickname = (userId) => {
  104. for (const user of users.value) {
  105. if (user.id === userId) {
  106. return user.nickname
  107. }
  108. }
  109. return '未知(' + userId + ')'
  110. }
  111. // ========== CRUD 相关 ==========
  112. const actionLoading = ref(false) // 遮罩层
  113. const actionType = ref('') // 操作按钮的类型
  114. const dialogVisible = ref(false) // 是否显示弹出层
  115. const dialogTitle = ref('edit') // 弹出层标题
  116. const formRef = ref<FormExpose>() // 表单 Ref
  117. const detailData = ref() // 详情 Ref
  118. // 设置标题
  119. const setDialogTile = (type: string) => {
  120. dialogTitle.value = t('action.' + type)
  121. actionType.value = type
  122. dialogVisible.value = true
  123. }
  124. // 新增操作
  125. const handleCreate = () => {
  126. setDialogTile('create')
  127. }
  128. // 修改操作
  129. const handleUpdate = async (rowId: number) => {
  130. setDialogTile('update')
  131. // 设置数据
  132. const res = await UserGroupApi.getUserGroupApi(rowId)
  133. unref(formRef)?.setValues(res)
  134. }
  135. // 详情操作
  136. const handleDetail = async (rowId: number) => {
  137. setDialogTile('detail')
  138. detailData.value = await UserGroupApi.getUserGroupApi(rowId)
  139. }
  140. // 提交按钮
  141. const submitForm = async () => {
  142. const elForm = unref(formRef)?.getElFormRef()
  143. if (!elForm) return
  144. elForm.validate(async (valid) => {
  145. if (valid) {
  146. actionLoading.value = true
  147. // 提交请求
  148. try {
  149. const data = unref(formRef)?.formModel as UserGroupApi.UserGroupVO
  150. if (actionType.value === 'create') {
  151. await UserGroupApi.createUserGroupApi(data)
  152. message.success(t('common.createSuccess'))
  153. } else {
  154. await UserGroupApi.updateUserGroupApi(data)
  155. message.success(t('common.updateSuccess'))
  156. }
  157. dialogVisible.value = false
  158. } finally {
  159. actionLoading.value = false
  160. // 刷新列表
  161. await reload()
  162. }
  163. }
  164. })
  165. }
  166. // ========== 初始化 ==========
  167. onMounted(() => {
  168. getListSimpleUsersApi().then((data) => {
  169. users.value = data
  170. })
  171. })
  172. </script>