index.vue.vm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <XTable @register="registerTable">
  5. <template #toolbar_buttons>
  6. <!-- 操作:新增 -->
  7. <XButton
  8. type="primary"
  9. preIcon="ep:zoom-in"
  10. :title="t('action.add')"
  11. v-hasPermi="['${permissionPrefix}:create']"
  12. @click="handleCreate()"
  13. />
  14. <!-- 操作:导出 -->
  15. <XButton
  16. type="warning"
  17. preIcon="ep:download"
  18. :title="t('action.export')"
  19. v-hasPermi="['${permissionPrefix}:export']"
  20. @click="exportList('${table.classComment}.xls')"
  21. />
  22. </template>
  23. <template #actionbtns_default="{ row }">
  24. <!-- 操作:修改 -->
  25. <XTextButton
  26. preIcon="ep:edit"
  27. :title="t('action.edit')"
  28. v-hasPermi="['${permissionPrefix}:update']"
  29. @click="handleUpdate(row.id)"
  30. />
  31. <!-- 操作:详情 -->
  32. <XTextButton
  33. preIcon="ep:view"
  34. :title="t('action.detail')"
  35. v-hasPermi="['${permissionPrefix}:query']"
  36. @click="handleDetail(row.id)"
  37. />
  38. <!-- 操作:删除 -->
  39. <XTextButton
  40. preIcon="ep:delete"
  41. :title="t('action.del')"
  42. v-hasPermi="['${permissionPrefix}:delete']"
  43. @click="deleteData(row.id)"
  44. />
  45. </template>
  46. </XTable>
  47. </ContentWrap>
  48. <!-- 弹窗 -->
  49. <XModal id="${classNameVar}Model" :loading="modelLoading" v-model="modelVisible" :title="modelTitle">
  50. <!-- 表单:添加/修改 -->
  51. <Form
  52. ref="formRef"
  53. v-if="['create', 'update'].includes(actionType)"
  54. :schema="allSchemas.formSchema"
  55. :rules="rules"
  56. />
  57. <!-- 表单:详情 -->
  58. <Descriptions
  59. v-if="actionType === 'detail'"
  60. :schema="allSchemas.detailSchema"
  61. :data="detailData"
  62. />
  63. <template #footer>
  64. <!-- 按钮:保存 -->
  65. <XButton
  66. v-if="['create', 'update'].includes(actionType)"
  67. type="primary"
  68. :title="t('action.save')"
  69. :loading="actionLoading"
  70. @click="submitForm()"
  71. />
  72. <!-- 按钮:关闭 -->
  73. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="modelVisible = false" />
  74. </template>
  75. </XModal>
  76. </template>
  77. <script setup lang="ts" name="${simpleClassName}">
  78. import { FormExpose } from '@/components/Form'
  79. // 业务相关的 import
  80. import { rules, allSchemas } from './${classNameVar}.data'
  81. import * as ${simpleClassName}Api from '@/api/${table.moduleName}/${classNameVar}'
  82. const { t } = useI18n() // 国际化
  83. const message = useMessage() // 消息弹窗
  84. // 列表相关的变量
  85. const [registerTable, { reload, deleteData, exportList }] = useXTable({
  86. allSchemas: allSchemas,
  87. getListApi: ${simpleClassName}Api.get${simpleClassName}PageApi,
  88. deleteApi: ${simpleClassName}Api.delete${simpleClassName}Api,
  89. exportListApi: ${simpleClassName}Api.export${simpleClassName}Api
  90. })
  91. // 弹窗相关的变量
  92. const modelVisible = ref(false) // 是否显示弹出层
  93. const modelTitle = ref('edit') // 弹出层标题
  94. const modelLoading = ref(false) // 弹出层loading
  95. const actionType = ref('') // 操作按钮的类型
  96. const actionLoading = ref(false) // 按钮 Loading
  97. const formRef = ref<FormExpose>() // 表单 Ref
  98. const detailData = ref() // 详情 Ref
  99. // 设置标题
  100. const setDialogTile = (type: string) => {
  101. modelLoading.value = true
  102. modelTitle.value = t('action.' + type)
  103. actionType.value = type
  104. modelVisible.value = true
  105. }
  106. // 新增操作
  107. const handleCreate = () => {
  108. setDialogTile('create')
  109. modelLoading.value = false
  110. }
  111. // 修改操作
  112. const handleUpdate = async (rowId: number) => {
  113. setDialogTile('update')
  114. // 设置数据
  115. const res = await ${simpleClassName}Api.get${simpleClassName}Api(rowId)
  116. unref(formRef)?.setValues(res)
  117. modelLoading.value = false
  118. }
  119. // 详情操作
  120. const handleDetail = async (rowId: number) => {
  121. setDialogTile('detail')
  122. const res = await ${simpleClassName}Api.get${simpleClassName}Api(rowId)
  123. detailData.value = res
  124. modelLoading.value = false
  125. }
  126. // 提交按钮
  127. const submitForm = async () => {
  128. const elForm = unref(formRef)?.getElFormRef()
  129. if (!elForm) return
  130. elForm.validate(async (valid) => {
  131. if (valid) {
  132. actionLoading.value = true
  133. // 提交请求
  134. try {
  135. const data = unref(formRef)?.formModel as ${simpleClassName}Api.${simpleClassName}VO
  136. if (actionType.value === 'create') {
  137. await ${simpleClassName}Api.create${simpleClassName}Api(data)
  138. message.success(t('common.createSuccess'))
  139. } else {
  140. await ${simpleClassName}Api.update${simpleClassName}Api(data)
  141. message.success(t('common.updateSuccess'))
  142. }
  143. modelVisible.value = false
  144. } finally {
  145. actionLoading.value = false
  146. // 刷新列表
  147. await reload()
  148. }
  149. }
  150. })
  151. }
  152. </script>