index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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="['infra:job:create']"
  12. @click="handleCreate()"
  13. />
  14. <!-- 操作:导出 -->
  15. <XButton
  16. type="warning"
  17. preIcon="ep:download"
  18. :title="t('action.export')"
  19. v-hasPermi="['infra:job:export']"
  20. @click="handleExport()"
  21. />
  22. <XButton
  23. type="info"
  24. preIcon="ep:zoom-in"
  25. title="执行日志"
  26. v-hasPermi="['infra:job:query']"
  27. @click="handleJobLog"
  28. />
  29. </template>
  30. <template #actionbtns_default="{ row }">
  31. <!-- 操作:修改 -->
  32. <XTextButton
  33. preIcon="ep:edit"
  34. :title="t('action.edit')"
  35. v-hasPermi="['infra:job:update']"
  36. @click="handleUpdate(row.id)"
  37. />
  38. <XTextButton
  39. preIcon="ep:edit"
  40. :title="row.status === InfraJobStatusEnum.STOP ? '开启' : '暂停'"
  41. v-hasPermi="['infra:job:update']"
  42. @click="handleChangeStatus(row)"
  43. />
  44. <!-- 操作:删除 -->
  45. <XTextButton
  46. preIcon="ep:delete"
  47. :title="t('action.del')"
  48. v-hasPermi="['infra:job:delete']"
  49. @click="handleDelete(row.id)"
  50. />
  51. <el-dropdown class="p-0.5" v-hasPermi="['infra:job:trigger', 'infra:job:query']">
  52. <XTextButton :title="t('action.more')" postIcon="ep:arrow-down" />
  53. <template #dropdown>
  54. <el-dropdown-menu>
  55. <el-dropdown-item>
  56. <!-- 操作:执行 -->
  57. <XTextButton
  58. preIcon="ep:view"
  59. title="执行一次"
  60. v-hasPermi="['infra:job:trigger']"
  61. @click="handleRun(row)"
  62. />
  63. </el-dropdown-item>
  64. <el-dropdown-item>
  65. <!-- 操作:详情 -->
  66. <XTextButton
  67. preIcon="ep:view"
  68. :title="t('action.detail')"
  69. v-hasPermi="['infra:job:query']"
  70. @click="handleDetail(row.id)"
  71. />
  72. </el-dropdown-item>
  73. <el-dropdown-item>
  74. <!-- 操作:日志 -->
  75. <XTextButton
  76. preIcon="ep:view"
  77. title="调度日志"
  78. v-hasPermi="['infra:job:query']"
  79. @click="handleJobLog(row.id)"
  80. />
  81. </el-dropdown-item>
  82. </el-dropdown-menu>
  83. </template>
  84. </el-dropdown>
  85. </template>
  86. </XTable>
  87. </ContentWrap>
  88. <XModal v-model="dialogVisible" :title="dialogTitle">
  89. <!-- 对话框(添加 / 修改) -->
  90. <Form
  91. v-if="['create', 'update'].includes(actionType)"
  92. :schema="allSchemas.formSchema"
  93. :rules="rules"
  94. ref="formRef"
  95. >
  96. <template #cronExpression="form">
  97. <Crontab v-model="form['cronExpression']" :shortcuts="shortcuts" />
  98. </template>
  99. </Form>
  100. <!-- 对话框(详情) -->
  101. <Descriptions
  102. v-if="actionType === 'detail'"
  103. :schema="allSchemas.detailSchema"
  104. :data="detailData"
  105. >
  106. <template #retryInterval="{ row }">
  107. <span>{{ row.retryInterval + '毫秒' }} </span>
  108. </template>
  109. <template #monitorTimeout="{ row }">
  110. <span>{{ row.monitorTimeout > 0 ? row.monitorTimeout + ' 毫秒' : '未开启' }}</span>
  111. </template>
  112. <template #nextTimes>
  113. <span>{{ Array.from(nextTimes, (x) => parseTime(x)).join('; ') }}</span>
  114. </template>
  115. </Descriptions>
  116. <!-- 操作按钮 -->
  117. <template #footer>
  118. <!-- 按钮:保存 -->
  119. <XButton
  120. v-if="['create', 'update'].includes(actionType)"
  121. type="primary"
  122. :title="t('action.save')"
  123. :loading="actionLoading"
  124. @click="submitForm()"
  125. />
  126. <!-- 按钮:关闭 -->
  127. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  128. </template>
  129. </XModal>
  130. </template>
  131. <script setup lang="ts" name="Job">
  132. import { ref, unref } from 'vue'
  133. import { useRouter } from 'vue-router'
  134. import { ElDropdown, ElDropdownMenu, ElDropdownItem } from 'element-plus'
  135. import { useI18n } from '@/hooks/web/useI18n'
  136. import { useMessage } from '@/hooks/web/useMessage'
  137. import { useXTable } from '@/hooks/web/useXTable'
  138. import { FormExpose } from '@/components/Form'
  139. import { Crontab } from '@/components/Crontab'
  140. import * as JobApi from '@/api/infra/job'
  141. import { rules, allSchemas } from './job.data'
  142. import { InfraJobStatusEnum } from '@/utils/constants'
  143. const { t } = useI18n() // 国际化
  144. const message = useMessage() // 消息弹窗
  145. const { push } = useRouter()
  146. // 列表相关的变量
  147. const [registerTable, { reload, deleteData, exportList }] = useXTable({
  148. allSchemas: allSchemas,
  149. getListApi: JobApi.getJobPageApi,
  150. deleteApi: JobApi.deleteJobApi,
  151. exportListApi: JobApi.exportJobApi
  152. })
  153. // ========== CRUD 相关 ==========
  154. const actionLoading = ref(false) // 遮罩层
  155. const actionType = ref('') // 操作按钮的类型
  156. const dialogVisible = ref(false) // 是否显示弹出层
  157. const dialogTitle = ref('edit') // 弹出层标题
  158. const formRef = ref<FormExpose>() // 表单 Ref
  159. const detailData = ref() // 详情 Ref
  160. const nextTimes = ref([])
  161. const shortcuts = ref([
  162. {
  163. text: '每天8点和12点 (自定义追加)',
  164. value: '0 0 8,12 * * ?'
  165. }
  166. ])
  167. // 设置标题
  168. const setDialogTile = (type: string) => {
  169. dialogTitle.value = t('action.' + type)
  170. actionType.value = type
  171. dialogVisible.value = true
  172. }
  173. // 新增操作
  174. const handleCreate = () => {
  175. setDialogTile('create')
  176. }
  177. // 导出操作
  178. const handleExport = async () => {
  179. await exportList('定时任务.xls')
  180. }
  181. // 修改操作
  182. const handleUpdate = async (rowId: number) => {
  183. setDialogTile('update')
  184. // 设置数据
  185. const res = await JobApi.getJobApi(rowId)
  186. unref(formRef)?.setValues(res)
  187. }
  188. // 详情操作
  189. const handleDetail = async (rowId: number) => {
  190. // 设置数据
  191. const res = await JobApi.getJobApi(rowId)
  192. detailData.value = res
  193. // 后续执行时长
  194. const jobNextTime = await JobApi.getJobNextTimesApi(rowId)
  195. nextTimes.value = jobNextTime
  196. setDialogTile('detail')
  197. }
  198. const parseTime = (time) => {
  199. if (!time) {
  200. return null
  201. }
  202. const format = '{y}-{m}-{d} {h}:{i}:{s}'
  203. let date
  204. if (typeof time === 'object') {
  205. date = time
  206. } else {
  207. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  208. time = parseInt(time)
  209. } else if (typeof time === 'string') {
  210. time = time
  211. .replace(new RegExp(/-/gm), '/')
  212. .replace('T', ' ')
  213. .replace(new RegExp(/\.[\d]{3}/gm), '')
  214. }
  215. if (typeof time === 'number' && time.toString().length === 10) {
  216. time = time * 1000
  217. }
  218. date = new Date(time)
  219. }
  220. const formatObj = {
  221. y: date.getFullYear(),
  222. m: date.getMonth() + 1,
  223. d: date.getDate(),
  224. h: date.getHours(),
  225. i: date.getMinutes(),
  226. s: date.getSeconds(),
  227. a: date.getDay()
  228. }
  229. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  230. let value = formatObj[key]
  231. // Note: getDay() returns 0 on Sunday
  232. if (key === 'a') {
  233. return ['日', '一', '二', '三', '四', '五', '六'][value]
  234. }
  235. if (result.length > 0 && value < 10) {
  236. value = '0' + value
  237. }
  238. return value || 0
  239. })
  240. return time_str
  241. }
  242. // 删除操作
  243. const handleDelete = async (rowId: number) => {
  244. await deleteData(rowId)
  245. }
  246. const handleChangeStatus = async (row: JobApi.JobVO) => {
  247. const text = row.status === InfraJobStatusEnum.STOP ? '开启' : '关闭'
  248. const status =
  249. row.status === InfraJobStatusEnum.STOP ? InfraJobStatusEnum.NORMAL : InfraJobStatusEnum.STOP
  250. message
  251. .confirm('确认要' + text + '定时任务编号为"' + row.id + '"的数据项?', t('common.reminder'))
  252. .then(async () => {
  253. row.status =
  254. row.status === InfraJobStatusEnum.NORMAL
  255. ? InfraJobStatusEnum.NORMAL
  256. : InfraJobStatusEnum.STOP
  257. await JobApi.updateJobStatusApi(row.id, status)
  258. message.success(text + '成功')
  259. await reload()
  260. })
  261. .catch(() => {
  262. row.status =
  263. row.status === InfraJobStatusEnum.NORMAL
  264. ? InfraJobStatusEnum.STOP
  265. : InfraJobStatusEnum.NORMAL
  266. })
  267. }
  268. // 执行日志
  269. const handleJobLog = (rowId: number) => {
  270. if (rowId) {
  271. push('/job/job-log?id=' + rowId)
  272. } else {
  273. push('/job/job-log')
  274. }
  275. }
  276. // 执行一次
  277. const handleRun = (row: JobApi.JobVO) => {
  278. message.confirm('确认要立即执行一次' + row.name + '?', t('common.reminder')).then(async () => {
  279. await JobApi.runJobApi(row.id)
  280. message.success('执行成功')
  281. await reload()
  282. })
  283. }
  284. // 提交按钮
  285. const submitForm = async () => {
  286. const elForm = unref(formRef)?.getElFormRef()
  287. if (!elForm) return
  288. elForm.validate(async (valid) => {
  289. if (valid) {
  290. actionLoading.value = true
  291. // 提交请求
  292. try {
  293. const data = unref(formRef)?.formModel as JobApi.JobVO
  294. if (actionType.value === 'create') {
  295. await JobApi.createJobApi(data)
  296. message.success(t('common.createSuccess'))
  297. } else {
  298. await JobApi.updateJobApi(data)
  299. message.success(t('common.updateSuccess'))
  300. }
  301. dialogVisible.value = false
  302. } finally {
  303. actionLoading.value = false
  304. await reload()
  305. }
  306. }
  307. })
  308. }
  309. </script>