index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. unref(formRef)?.getElFormRef()?.resetFields()
  36. }
  37. // 修改操作
  38. const handleUpdate = async (row: FormVO) => {
  39. setDialogTile('update')
  40. // 设置数据
  41. const res = await FormApi.getFormApi(row.id)
  42. unref(formRef)?.setValues(res)
  43. }
  44. // 提交按钮
  45. const submitForm = async () => {
  46. const elForm = unref(formRef)?.getElFormRef()
  47. if (!elForm) return
  48. elForm.validate(async (valid) => {
  49. if (valid) {
  50. actionLoading.value = true
  51. // 提交请求
  52. try {
  53. const data = unref(formRef)?.formModel as FormVO
  54. if (actionType.value === 'create') {
  55. await FormApi.createFormApi(data)
  56. ElMessage.success(t('common.createSuccess'))
  57. } else {
  58. await FormApi.updateFormApi(data)
  59. ElMessage.success(t('common.updateSuccess'))
  60. }
  61. // 操作成功,重新加载列表
  62. dialogVisible.value = false
  63. await getList()
  64. } finally {
  65. actionLoading.value = false
  66. }
  67. }
  68. })
  69. }
  70. // ========== 详情相关 ==========
  71. const detailRef = ref() // 详情 Ref
  72. // 详情操作
  73. const handleDetail = async (row: FormVO) => {
  74. // 设置数据
  75. detailRef.value = row
  76. setDialogTile('detail')
  77. }
  78. // ========== 初始化 ==========
  79. getList()
  80. </script>
  81. <template>
  82. <!-- 搜索工作区 -->
  83. <ContentWrap>
  84. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  85. </ContentWrap>
  86. <ContentWrap>
  87. <!-- 操作工具栏 -->
  88. <div class="mb-10px">
  89. <el-button type="primary" v-hasPermi="['bpm:form:create']" @click="handleCreate">
  90. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  91. </el-button>
  92. </div>
  93. <!-- 列表 -->
  94. <Table
  95. :columns="allSchemas.tableColumns"
  96. :selection="false"
  97. :data="tableObject.tableList"
  98. :loading="tableObject.loading"
  99. :pagination="{
  100. total: tableObject.total
  101. }"
  102. v-model:pageSize="tableObject.pageSize"
  103. v-model:currentPage="tableObject.currentPage"
  104. @register="register"
  105. >
  106. <template #status="{ row }">
  107. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  108. </template>
  109. <template #createTime="{ row }">
  110. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  111. </template>
  112. <template #action="{ row }">
  113. <el-button link type="primary" v-hasPermi="['bpm:form:update']" @click="handleUpdate(row)">
  114. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  115. </el-button>
  116. <el-button link type="primary" v-hasPermi="['bpm:form:update']" @click="handleDetail(row)">
  117. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  118. </el-button>
  119. <el-button
  120. link
  121. type="primary"
  122. v-hasPermi="['bpm:form:delete']"
  123. @click="delList(row.id, false)"
  124. >
  125. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  126. </el-button>
  127. </template>
  128. </Table>
  129. </ContentWrap>
  130. <Dialog v-model="dialogVisible" :title="dialogTitle">
  131. <!-- 对话框(添加 / 修改) -->
  132. <Form
  133. v-if="['create', 'update'].includes(actionType)"
  134. :schema="allSchemas.formSchema"
  135. :rules="rules"
  136. ref="formRef"
  137. />
  138. <!-- 对话框(详情) -->
  139. <Descriptions
  140. v-if="actionType === 'detail'"
  141. :schema="allSchemas.detailSchema"
  142. :data="detailRef"
  143. >
  144. <template #status="{ row }">
  145. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  146. </template>
  147. <template #createTime="{ row }">
  148. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  149. </template>
  150. </Descriptions>
  151. <!-- 操作按钮 -->
  152. <template #footer>
  153. <el-button
  154. v-if="['create', 'update'].includes(actionType)"
  155. type="primary"
  156. :loading="actionLoading"
  157. @click="submitForm"
  158. >
  159. {{ t('action.save') }}
  160. </el-button>
  161. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  162. </template>
  163. </Dialog>
  164. </template>