index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <script lang="ts" setup>
  2. import { ref, unref } from 'vue'
  3. import DictTag from '@/components/DictTag/src/DictTag.vue'
  4. import * as JobApi from '@/api/infra/job'
  5. import { JobVO } from '@/api/infra/job/types'
  6. import Icon from '@/components/Icon/src/Icon.vue'
  7. import { DICT_TYPE } from '@/utils/dict'
  8. import { useTable } from '@/hooks/web/useTable'
  9. import { useI18n } from '@/hooks/web/useI18n'
  10. import { FormExpose } from '@/components/Form'
  11. import { rules, allSchemas } from './job.data'
  12. import { useRouter } from 'vue-router'
  13. import { useMessage } from '@/hooks/web/useMessage'
  14. const message = useMessage()
  15. const { t } = useI18n() // 国际化
  16. const { push } = useRouter()
  17. // ========== 列表相关 ==========
  18. const { register, tableObject, methods } = useTable<JobVO>({
  19. getListApi: JobApi.getJobPageApi,
  20. delListApi: JobApi.deleteJobApi,
  21. exportListApi: JobApi.exportJobApi
  22. })
  23. const { getList, setSearchParams, delList, exportList } = methods
  24. // 导出操作
  25. const handleExport = async () => {
  26. await exportList('定时任务.xls')
  27. }
  28. // ========== CRUD 相关 ==========
  29. const actionLoading = ref(false) // 遮罩层
  30. const actionType = ref('') // 操作按钮的类型
  31. const dialogVisible = ref(false) // 是否显示弹出层
  32. const dialogTitle = ref('edit') // 弹出层标题
  33. const formRef = ref<FormExpose>() // 表单 Ref
  34. // 设置标题
  35. const setDialogTile = (type: string) => {
  36. dialogTitle.value = t('action.' + type)
  37. actionType.value = type
  38. dialogVisible.value = true
  39. }
  40. // 新增操作
  41. const handleCreate = () => {
  42. setDialogTile('create')
  43. // 重置表单
  44. unref(formRef)?.getElFormRef()?.resetFields()
  45. }
  46. // 修改操作
  47. const handleUpdate = async (row: JobVO) => {
  48. setDialogTile('update')
  49. // 设置数据
  50. const res = await JobApi.getJobApi(row.id)
  51. unref(formRef)?.setValues(res)
  52. }
  53. // 执行日志
  54. const handleJobLog = (row: JobVO) => {
  55. if (row.id) {
  56. push('/job/job-log?id=' + row.id)
  57. } else {
  58. push('/job/job-log')
  59. }
  60. }
  61. // 执行一次
  62. const handleRun = (row: JobVO) => {
  63. message.confirm('确认要立即执行一次' + row.name + '?', t('common.reminder')).then(async () => {
  64. await JobApi.runJobApi(row.id)
  65. message.success('执行成功')
  66. getList()
  67. })
  68. }
  69. // 提交按钮
  70. const submitForm = async () => {
  71. actionLoading.value = true
  72. // 提交请求
  73. try {
  74. const data = unref(formRef)?.formModel as JobVO
  75. if (actionType.value === 'create') {
  76. await JobApi.createJobApi(data)
  77. message.success(t('common.createSuccess'))
  78. } else {
  79. await JobApi.updateJobApi(data)
  80. message.success(t('common.updateSuccess'))
  81. }
  82. // 操作成功,重新加载列表
  83. dialogVisible.value = false
  84. await getList()
  85. } finally {
  86. actionLoading.value = false
  87. }
  88. }
  89. // 删除操作
  90. const handleDelete = (row: JobVO) => {
  91. delList(row.id, false)
  92. }
  93. // ========== 详情相关 ==========
  94. const detailRef = ref() // 详情 Ref
  95. // 详情操作
  96. const handleDetail = async (row: JobVO) => {
  97. // 设置数据
  98. const res = JobApi.getJobApi(row.id)
  99. detailRef.value = res
  100. setDialogTile('detail')
  101. }
  102. // ========== 初始化 ==========
  103. getList()
  104. </script>
  105. <template>
  106. <!-- 搜索工作区 -->
  107. <ContentWrap>
  108. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  109. </ContentWrap>
  110. <ContentWrap>
  111. <!-- 操作工具栏 -->
  112. <div class="mb-10px">
  113. <el-button type="primary" v-hasPermi="['infra:job:create']" @click="handleCreate">
  114. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  115. </el-button>
  116. <el-button
  117. type="warning"
  118. v-hasPermi="['infra:job:export']"
  119. :loading="tableObject.exportLoading"
  120. @click="handleExport"
  121. >
  122. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  123. </el-button>
  124. <el-button type="info" v-hasPermi="['infra:job:query']" @click="handleJobLog">
  125. <Icon icon="ep:zoom-in" class="mr-5px" /> 执行日志
  126. </el-button>
  127. </div>
  128. <!-- 列表 -->
  129. <Table
  130. :columns="allSchemas.tableColumns"
  131. :selection="false"
  132. :data="tableObject.tableList"
  133. :loading="tableObject.loading"
  134. :pagination="{
  135. total: tableObject.total
  136. }"
  137. v-model:pageSize="tableObject.pageSize"
  138. v-model:currentPage="tableObject.currentPage"
  139. @register="register"
  140. >
  141. <template #status="{ row }">
  142. <DictTag :type="DICT_TYPE.INFRA_JOB_STATUS" :value="row.status" />
  143. </template>
  144. <template #action="{ row }">
  145. <el-button link type="primary" v-hasPermi="['infra:job:update']" @click="handleUpdate(row)">
  146. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  147. </el-button>
  148. <el-button link type="primary" v-hasPermi="['infra:job:query']" @click="handleDetail(row)">
  149. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  150. </el-button>
  151. <el-button link type="primary" v-hasPermi="['infra:job:delete']" @click="handleDelete(row)">
  152. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  153. </el-button>
  154. <el-button link type="primary" v-hasPermi="['infra:job:trigger']" @click="handleRun(row)">
  155. <Icon icon="ep:view" class="mr-1px" /> 执行一次
  156. </el-button>
  157. <el-button link type="primary" v-hasPermi="['infra:job:query']" @click="handleJobLog(row)">
  158. <Icon icon="ep:view" class="mr-1px" /> 调度日志
  159. </el-button>
  160. </template>
  161. </Table>
  162. </ContentWrap>
  163. <Dialog v-model="dialogVisible" :title="dialogTitle">
  164. <!-- 对话框(添加 / 修改) -->
  165. <Form
  166. v-if="['create', 'update'].includes(actionType)"
  167. :schema="allSchemas.formSchema"
  168. :rules="rules"
  169. ref="formRef"
  170. />
  171. <!-- 对话框(详情) -->
  172. <Descriptions
  173. v-if="actionType === 'detail'"
  174. :schema="allSchemas.detailSchema"
  175. :data="detailRef"
  176. >
  177. <template #status="{ row }">
  178. <DictTag :type="DICT_TYPE.INFRA_JOB_STATUS" :value="row.status" />
  179. </template>
  180. <template #retryInterval="{ row }">
  181. <span>{{ row.retryInterval + '毫秒' }} </span>
  182. </template>
  183. <template #monitorTimeout="{ row }">
  184. <span>{{ row.monitorTimeout > 0 ? row.monitorTimeout + ' 毫秒' : '未开启' }}</span>
  185. </template>
  186. </Descriptions>
  187. <!-- 操作按钮 -->
  188. <template #footer>
  189. <el-button
  190. v-if="['create', 'update'].includes(actionType)"
  191. type="primary"
  192. :loading="actionLoading"
  193. @click="submitForm"
  194. >
  195. {{ t('action.save') }}
  196. </el-button>
  197. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  198. </template>
  199. </Dialog>
  200. </template>