index.vue 5.0 KB

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