index.vue.vm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 { ${simpleClassName}VO } from '@/api/system/post/types'
  10. import { rules, allSchemas } from './post.data'
  11. import * as ${simpleClassName}Api from '@/api/system/post'
  12. const { t } = useI18n() // 国际化
  13. // ========== 列表相关 ==========
  14. const { register, tableObject, methods } = useTable<${simpleClassName}VO>({
  15. getListApi: ${simpleClassName}Api.get${simpleClassName}PageApi,
  16. delListApi: ${simpleClassName}Api.delete${simpleClassName}Api,
  17. exportListApi: ${simpleClassName}Api.export${simpleClassName}Api
  18. })
  19. const { getList, setSearchParams, delList, exportList } = methods
  20. // 导出操作
  21. const handleExport = async () => {
  22. await exportList('数据.xls')
  23. }
  24. // ========== CRUD 相关 ==========
  25. const actionLoading = ref(false) // 遮罩层
  26. const actionType = ref('') // 操作按钮的类型
  27. const dialogVisible = ref(false) // 是否显示弹出层
  28. const dialogTitle = ref('edit') // 弹出层标题
  29. const formRef = ref<FormExpose>() // 表单 Ref
  30. // 设置标题
  31. const setDialogTile = (type: string) => {
  32. dialogTitle.value = t('action.' + type)
  33. actionType.value = type
  34. dialogVisible.value = true
  35. }
  36. // 新增操作
  37. const handleCreate = () => {
  38. setDialogTile('create')
  39. // 重置表单
  40. unref(formRef)?.getElFormRef()?.resetFields()
  41. }
  42. // 修改操作
  43. const handleUpdate = async (row: ${simpleClassName}VO) => {
  44. setDialogTile('update')
  45. // 设置数据
  46. const res = await ${simpleClassName}Api.get${simpleClassName}Api(row.id)
  47. unref(formRef)?.setValues(res)
  48. }
  49. // 提交按钮
  50. const submitForm = async () => {
  51. actionLoading.value = true
  52. // 提交请求
  53. try {
  54. const data = unref(formRef)?.formModel as ${simpleClassName}VO
  55. if (actionType.value === 'create') {
  56. await ${simpleClassName}Api.create${simpleClassName}Api(data)
  57. ElMessage.success(t('common.createSuccess'))
  58. } else {
  59. await ${simpleClassName}Api.update${simpleClassName}Api(data)
  60. ElMessage.success(t('common.updateSuccess'))
  61. }
  62. // 操作成功,重新加载列表
  63. dialogVisible.value = false
  64. await getList()
  65. } finally {
  66. actionLoading.value = false
  67. }
  68. }
  69. // ========== 详情相关 ==========
  70. const detailRef = ref() // 详情 Ref
  71. // 详情操作
  72. const handleDetail = async (row: ${simpleClassName}VO) => {
  73. // 设置数据
  74. detailRef.value = row
  75. setDialogTile('detail')
  76. }
  77. // ========== 初始化 ==========
  78. getList()
  79. </script>
  80. <template>
  81. <!-- 搜索工作区 -->
  82. <ContentWrap>
  83. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  84. </ContentWrap>
  85. <ContentWrap>
  86. <!-- 操作工具栏 -->
  87. <div class="mb-10px">
  88. <el-button type="primary" v-hasPermi="['${permissionPrefix}:create']" @click="handleCreate">
  89. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  90. </el-button>
  91. <el-button
  92. type="warning"
  93. v-hasPermi="['${permissionPrefix}:export']"
  94. :loading="tableObject.exportLoading"
  95. @click="handleExport"
  96. >
  97. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  98. </el-button>
  99. </div>
  100. <!-- 列表 -->
  101. <Table
  102. :columns="allSchemas.tableColumns"
  103. :selection="false"
  104. :data="tableObject.tableList"
  105. :loading="tableObject.loading"
  106. :pagination="{
  107. total: tableObject.total
  108. }"
  109. v-model:pageSize="tableObject.pageSize"
  110. v-model:currentPage="tableObject.currentPage"
  111. @register="register"
  112. >
  113. #foreach($column in $columns)
  114. #if ($column.listOperationResult)
  115. #set ($dictType=$column.dictType)
  116. #if ($column.javaType == "Date")## 时间类型
  117. <template #${column.javaField}="{ row }">
  118. <span>{{ dayjs(row.${column.javaField}).format('YYYY-MM-DD HH:mm:ss') }}</span>
  119. </template>
  120. #elseif("" != $column.dictType)## 数据字典
  121. <template #${column.javaField}="{ row }">
  122. <DictTag :type="DICT_TYPE.$dictType.toUpperCase()" :value="row.${column.javaField}" />
  123. </template>
  124. #end
  125. #end
  126. #end
  127. <template #action="{ row }">
  128. <el-button
  129. link
  130. type="primary"
  131. v-hasPermi="['${permissionPrefix}:update']"
  132. @click="handleUpdate(row)"
  133. >
  134. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  135. </el-button>
  136. <el-button
  137. link
  138. type="primary"
  139. v-hasPermi="['${permissionPrefix}:update']"
  140. @click="handleDetail(row)"
  141. >
  142. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  143. </el-button>
  144. <el-button
  145. link
  146. type="primary"
  147. v-hasPermi="['${permissionPrefix}:delete']"
  148. @click="delList(row.id, false)"
  149. >
  150. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  151. </el-button>
  152. </template>
  153. </Table>
  154. </ContentWrap>
  155. <Dialog v-model="dialogVisible" :title="dialogTitle">
  156. <!-- 对话框(添加 / 修改) -->
  157. <Form
  158. v-if="['create', 'update'].includes(actionType)"
  159. :schema="allSchemas.formSchema"
  160. :rules="rules"
  161. ref="formRef"
  162. />
  163. <!-- 对话框(详情) -->
  164. <Descriptions
  165. v-if="actionType === 'detail'"
  166. :schema="allSchemas.detailSchema"
  167. :data="detailRef"
  168. >
  169. #foreach($column in $columns)
  170. #if ($column.listOperationResult)
  171. #set ($dictType=$column.dictType)
  172. #if ($column.javaType == "Date")## 时间类型
  173. <template #${column.javaField}="{ row }">
  174. <span>{{ dayjs(row.${column.javaField}).format('YYYY-MM-DD HH:mm:ss') }}</span>
  175. </template>
  176. #elseif("" != $column.dictType)## 数据字典
  177. <template #${column.javaField}="{ row }">
  178. <DictTag :type="DICT_TYPE.$dictType.toUpperCase()" :value="row.${column.javaField}" />
  179. </template>
  180. #end
  181. #end
  182. #end
  183. </Descriptions>
  184. <!-- 操作按钮 -->
  185. <template #footer>
  186. <el-button
  187. v-if="['create', 'update'].includes(actionType)"
  188. type="primary"
  189. :loading="actionLoading"
  190. @click="submitForm"
  191. >
  192. {{ t('action.save') }}
  193. </el-button>
  194. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  195. </template>
  196. </Dialog>
  197. </template>