index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 { AppVO } from '@/api/pay/app/types'
  10. import { rules, allSchemas } from './app.data'
  11. import * as AppApi from '@/api/pay/app'
  12. const { t } = useI18n() // 国际化
  13. // ========== 列表相关 ==========
  14. const { register, tableObject, methods } = useTable<AppVO>({
  15. getListApi: AppApi.getAppPageApi,
  16. delListApi: AppApi.deleteAppApi,
  17. exportListApi: AppApi.exportAppApi
  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: AppVO) => {
  40. setDialogTile('update')
  41. // 设置数据
  42. const res = await AppApi.getAppApi(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 AppVO
  55. if (actionType.value === 'create') {
  56. await AppApi.createAppApi(data)
  57. ElMessage.success(t('common.createSuccess'))
  58. } else {
  59. await AppApi.updateAppApi(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: AppVO) => {
  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="['system:post: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="['system:post: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. <template #status="{ row }">
  116. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  117. </template>
  118. <template #createTime="{ row }">
  119. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  120. </template>
  121. <template #action="{ row }">
  122. <el-button
  123. link
  124. type="primary"
  125. v-hasPermi="['system:post:update']"
  126. @click="handleUpdate(row)"
  127. >
  128. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  129. </el-button>
  130. <el-button
  131. link
  132. type="primary"
  133. v-hasPermi="['system:post:update']"
  134. @click="handleDetail(row)"
  135. >
  136. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  137. </el-button>
  138. <el-button
  139. link
  140. type="primary"
  141. v-hasPermi="['system:post:delete']"
  142. @click="delList(row.id, false)"
  143. >
  144. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  145. </el-button>
  146. </template>
  147. </Table>
  148. </ContentWrap>
  149. <Dialog v-model="dialogVisible" :title="dialogTitle">
  150. <!-- 对话框(添加 / 修改) -->
  151. <Form
  152. v-if="['create', 'update'].includes(actionType)"
  153. :schema="allSchemas.formSchema"
  154. :rules="rules"
  155. ref="formRef"
  156. />
  157. <!-- 对话框(详情) -->
  158. <Descriptions
  159. v-if="actionType === 'detail'"
  160. :schema="allSchemas.detailSchema"
  161. :data="detailRef"
  162. >
  163. <template #status="{ row }">
  164. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  165. </template>
  166. <template #createTime="{ row }">
  167. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  168. </template>
  169. </Descriptions>
  170. <!-- 操作按钮 -->
  171. <template #footer>
  172. <el-button
  173. v-if="['create', 'update'].includes(actionType)"
  174. type="primary"
  175. :loading="actionLoading"
  176. @click="submitForm"
  177. >
  178. {{ t('action.save') }}
  179. </el-button>
  180. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  181. </template>
  182. </Dialog>
  183. </template>