index.vue.vm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/${table.moduleName}/${classNameVar}/types'
  10. import { rules, allSchemas } from './${classNameVar}.data'
  11. import * as ${simpleClassName}Api from '@/api/${table.moduleName}/${classNameVar}'
  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. const elForm = unref(formRef)?.getElFormRef()
  48. if (!elForm) return
  49. elForm.validate(async (valid) => {
  50. if (valid) {
  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. }
  71. // ========== 详情相关 ==========
  72. const detailRef = ref() // 详情 Ref
  73. // 详情操作
  74. const handleDetail = async (row: ${simpleClassName}VO) => {
  75. // 设置数据
  76. detailRef.value = row
  77. setDialogTile('detail')
  78. }
  79. // ========== 初始化 ==========
  80. getList()
  81. </script>
  82. <template>
  83. <!-- 搜索工作区 -->
  84. <ContentWrap>
  85. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  86. </ContentWrap>
  87. <ContentWrap>
  88. <!-- 操作工具栏 -->
  89. <div class="mb-10px">
  90. <el-button type="primary" v-hasPermi="['${permissionPrefix}:create']" @click="handleCreate">
  91. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  92. </el-button>
  93. <el-button
  94. type="warning"
  95. v-hasPermi="['${permissionPrefix}:export']"
  96. :loading="tableObject.exportLoading"
  97. @click="exportList('数据.xls')"
  98. >
  99. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  100. </el-button>
  101. </div>
  102. <!-- 列表 -->
  103. <Table
  104. :columns="allSchemas.tableColumns"
  105. :selection="false"
  106. :data="tableObject.tableList"
  107. :loading="tableObject.loading"
  108. :pagination="{
  109. total: tableObject.total
  110. }"
  111. v-model:pageSize="tableObject.pageSize"
  112. v-model:currentPage="tableObject.currentPage"
  113. @register="register"
  114. >
  115. #foreach($column in $columns)
  116. #if ($column.listOperationResult)
  117. #set ($dictType=$column.dictType)
  118. #if ($column.javaType == "Date")## 时间类型
  119. <template #${column.javaField}="{ row }">
  120. <span>{{ dayjs(row.${column.javaField}).format('YYYY-MM-DD HH:mm:ss') }}</span>
  121. </template>
  122. #elseif("" != $column.dictType)## 数据字典
  123. <template #${column.javaField}="{ row }">
  124. <DictTag :type="DICT_TYPE.$dictType.toUpperCase()" :value="row.${column.javaField}" />
  125. </template>
  126. #end
  127. #end
  128. #end
  129. <template #action="{ row }">
  130. <el-button
  131. link
  132. type="primary"
  133. v-hasPermi="['${permissionPrefix}:update']"
  134. @click="handleUpdate(row)"
  135. >
  136. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  137. </el-button>
  138. <el-button
  139. link
  140. type="primary"
  141. v-hasPermi="['${permissionPrefix}:update']"
  142. @click="handleDetail(row)"
  143. >
  144. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  145. </el-button>
  146. <el-button
  147. link
  148. type="primary"
  149. v-hasPermi="['${permissionPrefix}:delete']"
  150. @click="delList(row.id, false)"
  151. >
  152. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  153. </el-button>
  154. </template>
  155. </Table>
  156. </ContentWrap>
  157. <Dialog v-model="dialogVisible" :title="dialogTitle">
  158. <!-- 对话框(添加 / 修改) -->
  159. <Form
  160. v-if="['create', 'update'].includes(actionType)"
  161. :schema="allSchemas.formSchema"
  162. :rules="rules"
  163. ref="formRef"
  164. />
  165. <!-- 对话框(详情) -->
  166. <Descriptions
  167. v-if="actionType === 'detail'"
  168. :schema="allSchemas.detailSchema"
  169. :data="detailRef"
  170. >
  171. #foreach($column in $columns)
  172. #if ($column.listOperationResult)
  173. #set ($dictType=$column.dictType)
  174. #if ($column.javaType == "Date")## 时间类型
  175. <template #${column.javaField}="{ row }">
  176. <span>{{ dayjs(row.${column.javaField}).format('YYYY-MM-DD HH:mm:ss') }}</span>
  177. </template>
  178. #elseif("" != $column.dictType)## 数据字典
  179. <template #${column.javaField}="{ row }">
  180. <DictTag :type="DICT_TYPE.$dictType.toUpperCase()" :value="row.${column.javaField}" />
  181. </template>
  182. #end
  183. #end
  184. #end
  185. </Descriptions>
  186. <!-- 操作按钮 -->
  187. <template #footer>
  188. <el-button
  189. v-if="['create', 'update'].includes(actionType)"
  190. type="primary"
  191. :loading="actionLoading"
  192. @click="submitForm"
  193. >
  194. {{ t('action.save') }}
  195. </el-button>
  196. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  197. </template>
  198. </Dialog>
  199. </template>