index.vue.vm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. <XButton
  91. type="primary"
  92. preIcon="ep:zoom-in"
  93. :title="t('action.add')"
  94. v-hasPermi="['${permissionPrefix}:create']"
  95. @click="handleCreate()"
  96. />
  97. <XButton
  98. type="warning"
  99. preIcon="ep:download"
  100. :title="t('action.export')"
  101. v-hasPermi="['${permissionPrefix}:export']"
  102. @click="exportList('数据.xls')"
  103. />
  104. </div>
  105. <!-- 列表 -->
  106. <Table
  107. :columns="allSchemas.tableColumns"
  108. :selection="false"
  109. :data="tableObject.tableList"
  110. :loading="tableObject.loading"
  111. :pagination="{
  112. total: tableObject.total
  113. }"
  114. v-model:pageSize="tableObject.pageSize"
  115. v-model:currentPage="tableObject.currentPage"
  116. @register="register"
  117. >
  118. #foreach($column in $columns)
  119. #if ($column.listOperationResult)
  120. #set ($dictType=$column.dictType)
  121. #if ($column.javaType == "Date")## 时间类型
  122. <template #${column.javaField}="{ row }">
  123. <span>{{ dayjs(row.${column.javaField}).format('YYYY-MM-DD HH:mm:ss') }}</span>
  124. </template>
  125. #elseif("" != $column.dictType)## 数据字典
  126. <template #${column.javaField}="{ row }">
  127. <DictTag :type="DICT_TYPE.$dictType.toUpperCase()" :value="row.${column.javaField}" />
  128. </template>
  129. #end
  130. #end
  131. #end
  132. <template #action="{ row }">
  133. <XButton
  134. link
  135. type="primary"
  136. preIcon="ep:edit"
  137. :title="t('action.edit')"
  138. v-hasPermi="['${permissionPrefix}:update']"
  139. @click="handleUpdate(row.id)"
  140. />
  141. <XButton
  142. link
  143. type="primary"
  144. preIcon="ep:view"
  145. :title="t('action.detail')"
  146. v-hasPermi="['${permissionPrefix}:update']"
  147. @click="handleDetail(row)"
  148. />
  149. <XButton
  150. link
  151. type="primary"
  152. preIcon="ep:delete"
  153. :title="t('action.del')"
  154. v-hasPermi="['${permissionPrefix}:delete']"
  155. @click="handleDelete(row.id)"
  156. />
  157. </template>
  158. </Table>
  159. </ContentWrap>
  160. <Dialog v-model="dialogVisible" :title="dialogTitle">
  161. <!-- 对话框(添加 / 修改) -->
  162. <Form
  163. v-if="['create', 'update'].includes(actionType)"
  164. :schema="allSchemas.formSchema"
  165. :rules="rules"
  166. ref="formRef"
  167. />
  168. <!-- 对话框(详情) -->
  169. <Descriptions
  170. v-if="actionType === 'detail'"
  171. :schema="allSchemas.detailSchema"
  172. :data="detailRef"
  173. >
  174. #foreach($column in $columns)
  175. #if ($column.listOperationResult)
  176. #set ($dictType=$column.dictType)
  177. #if ($column.javaType == "Date")## 时间类型
  178. <template #${column.javaField}="{ row }">
  179. <span>{{ dayjs(row.${column.javaField}).format('YYYY-MM-DD HH:mm:ss') }}</span>
  180. </template>
  181. #elseif("" != $column.dictType)## 数据字典
  182. <template #${column.javaField}="{ row }">
  183. <DictTag :type="DICT_TYPE.$dictType.toUpperCase()" :value="row.${column.javaField}" />
  184. </template>
  185. #end
  186. #end
  187. #end
  188. </Descriptions>
  189. <!-- 操作按钮 -->
  190. <template #footer>
  191. <XButton
  192. v-if="['create', 'update'].includes(actionType)"
  193. :loading="actionLoading"
  194. :title="t('action.save')"
  195. type="primary"
  196. @click="submitForm"
  197. />
  198. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  199. </template>
  200. </Dialog>
  201. </template>