index.vue.vm 6.5 KB

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