index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <script setup lang="ts">
  2. import { ref, unref } from 'vue'
  3. import dayjs from 'dayjs'
  4. import { ElMessage } 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 { FormVO } from '@/api/bpm/form/types'
  10. import { rules, allSchemas } from './form.data'
  11. import * as FormApi from '@/api/bpm/form'
  12. const { t } = useI18n() // 国际化
  13. // ========== 列表相关 ==========
  14. const { register, tableObject, methods } = useTable<FormVO>({
  15. getListApi: FormApi.getFormPageApi,
  16. delListApi: FormApi.deleteFormApi
  17. })
  18. const { getList, setSearchParams, delList } = methods
  19. // ========== CRUD 相关 ==========
  20. const actionLoading = ref(false) // 遮罩层
  21. const actionType = ref('') // 操作按钮的类型
  22. const dialogVisible = ref(false) // 是否显示弹出层
  23. const dialogTitle = ref('edit') // 弹出层标题
  24. const formRef = ref<FormExpose>() // 表单 Ref
  25. // 设置标题
  26. const setDialogTile = (type: string) => {
  27. dialogTitle.value = t('action.' + type)
  28. actionType.value = type
  29. dialogVisible.value = true
  30. }
  31. // 新增操作
  32. const handleCreate = () => {
  33. setDialogTile('create')
  34. }
  35. // 修改操作
  36. const handleUpdate = async (row: FormVO) => {
  37. setDialogTile('update')
  38. // 设置数据
  39. const res = await FormApi.getFormApi(row.id)
  40. unref(formRef)?.setValues(res)
  41. }
  42. // 提交按钮
  43. const submitForm = async () => {
  44. const elForm = unref(formRef)?.getElFormRef()
  45. if (!elForm) return
  46. elForm.validate(async (valid) => {
  47. if (valid) {
  48. actionLoading.value = true
  49. // 提交请求
  50. try {
  51. const data = unref(formRef)?.formModel as FormVO
  52. if (actionType.value === 'create') {
  53. await FormApi.createFormApi(data)
  54. ElMessage.success(t('common.createSuccess'))
  55. } else {
  56. await FormApi.updateFormApi(data)
  57. ElMessage.success(t('common.updateSuccess'))
  58. }
  59. // 操作成功,重新加载列表
  60. dialogVisible.value = false
  61. await getList()
  62. } finally {
  63. actionLoading.value = false
  64. }
  65. }
  66. })
  67. }
  68. // ========== 详情相关 ==========
  69. const detailRef = ref() // 详情 Ref
  70. // 详情操作
  71. const handleDetail = async (row: FormVO) => {
  72. // 设置数据
  73. detailRef.value = row
  74. setDialogTile('detail')
  75. }
  76. // ========== 初始化 ==========
  77. getList()
  78. </script>
  79. <template>
  80. <!-- 搜索工作区 -->
  81. <ContentWrap>
  82. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  83. </ContentWrap>
  84. <ContentWrap>
  85. <!-- 操作工具栏 -->
  86. <div class="mb-10px">
  87. <el-button type="primary" v-hasPermi="['bpm:form:create']" @click="handleCreate()">
  88. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  89. </el-button>
  90. </div>
  91. <!-- 列表 -->
  92. <Table
  93. :columns="allSchemas.tableColumns"
  94. :selection="false"
  95. :data="tableObject.tableList"
  96. :loading="tableObject.loading"
  97. :pagination="{
  98. total: tableObject.total
  99. }"
  100. v-model:pageSize="tableObject.pageSize"
  101. v-model:currentPage="tableObject.currentPage"
  102. @register="register"
  103. >
  104. <template #status="{ row }">
  105. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  106. </template>
  107. <template #createTime="{ row }">
  108. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  109. </template>
  110. <template #action="{ row }">
  111. <el-button link type="primary" v-hasPermi="['bpm:form:update']" @click="handleUpdate(row)">
  112. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  113. </el-button>
  114. <el-button link type="primary" v-hasPermi="['bpm:form:update']" @click="handleDetail(row)">
  115. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  116. </el-button>
  117. <el-button
  118. link
  119. type="primary"
  120. v-hasPermi="['bpm:form:delete']"
  121. @click="delList(row.id, false)"
  122. >
  123. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  124. </el-button>
  125. </template>
  126. </Table>
  127. </ContentWrap>
  128. <XModal v-model="dialogVisible" :title="dialogTitle">
  129. <!-- 对话框(添加 / 修改) -->
  130. <Form
  131. v-if="['create', 'update'].includes(actionType)"
  132. :schema="allSchemas.formSchema"
  133. :rules="rules"
  134. ref="formRef"
  135. />
  136. <!-- 对话框(详情) -->
  137. <Descriptions
  138. v-if="actionType === 'detail'"
  139. :schema="allSchemas.detailSchema"
  140. :data="detailRef"
  141. />
  142. <!-- 操作按钮 -->
  143. <template #footer>
  144. <!-- 按钮:保存 -->
  145. <XButton
  146. v-if="['create', 'update'].includes(actionType)"
  147. type="primary"
  148. :title="t('action.save')"
  149. :loading="actionLoading"
  150. @click="submitForm()"
  151. />
  152. <!-- 按钮:关闭 -->
  153. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  154. </template>
  155. </XModal>
  156. </template>