index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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="exportList('定时任务.xls')"
  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="deleteData(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 type { FormExpose } from '@/components/Form'
  133. import * as JobApi from '@/api/infra/job'
  134. import { rules, allSchemas } from './job.data'
  135. import { InfraJobStatusEnum } from '@/utils/constants'
  136. const { t } = useI18n() // 国际化
  137. const message = useMessage() // 消息弹窗
  138. const { push } = useRouter()
  139. // 列表相关的变量
  140. const [registerTable, { reload, deleteData, exportList }] = useXTable({
  141. allSchemas: allSchemas,
  142. getListApi: JobApi.getJobPageApi,
  143. deleteApi: JobApi.deleteJobApi,
  144. exportListApi: JobApi.exportJobApi
  145. })
  146. // ========== CRUD 相关 ==========
  147. const actionLoading = ref(false) // 遮罩层
  148. const actionType = ref('') // 操作按钮的类型
  149. const dialogVisible = ref(false) // 是否显示弹出层
  150. const dialogTitle = ref('edit') // 弹出层标题
  151. const formRef = ref<FormExpose>() // 表单 Ref
  152. const detailData = ref() // 详情 Ref
  153. const nextTimes = ref([])
  154. const shortcuts = ref([
  155. {
  156. text: '每天8点和12点 (自定义追加)',
  157. value: '0 0 8,12 * * ?'
  158. }
  159. ])
  160. // 设置标题
  161. const setDialogTile = (type: string) => {
  162. dialogTitle.value = t('action.' + type)
  163. actionType.value = type
  164. dialogVisible.value = true
  165. }
  166. // 新增操作
  167. const handleCreate = () => {
  168. setDialogTile('create')
  169. }
  170. // 修改操作
  171. const handleUpdate = async (rowId: number) => {
  172. setDialogTile('update')
  173. // 设置数据
  174. const res = await JobApi.getJobApi(rowId)
  175. unref(formRef)?.setValues(res)
  176. }
  177. // 详情操作
  178. const handleDetail = async (rowId: number) => {
  179. // 设置数据
  180. const res = await JobApi.getJobApi(rowId)
  181. detailData.value = res
  182. // 后续执行时长
  183. const jobNextTime = await JobApi.getJobNextTimesApi(rowId)
  184. nextTimes.value = jobNextTime
  185. setDialogTile('detail')
  186. }
  187. const parseTime = (time) => {
  188. if (!time) {
  189. return null
  190. }
  191. const format = '{y}-{m}-{d} {h}:{i}:{s}'
  192. let date
  193. if (typeof time === 'object') {
  194. date = time
  195. } else {
  196. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  197. time = parseInt(time)
  198. } else if (typeof time === 'string') {
  199. time = time
  200. .replace(new RegExp(/-/gm), '/')
  201. .replace('T', ' ')
  202. .replace(new RegExp(/\.[\d]{3}/gm), '')
  203. }
  204. if (typeof time === 'number' && time.toString().length === 10) {
  205. time = time * 1000
  206. }
  207. date = new Date(time)
  208. }
  209. const formatObj = {
  210. y: date.getFullYear(),
  211. m: date.getMonth() + 1,
  212. d: date.getDate(),
  213. h: date.getHours(),
  214. i: date.getMinutes(),
  215. s: date.getSeconds(),
  216. a: date.getDay()
  217. }
  218. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  219. let value = formatObj[key]
  220. // Note: getDay() returns 0 on Sunday
  221. if (key === 'a') {
  222. return ['日', '一', '二', '三', '四', '五', '六'][value]
  223. }
  224. if (result.length > 0 && value < 10) {
  225. value = '0' + value
  226. }
  227. return value || 0
  228. })
  229. return time_str
  230. }
  231. const handleChangeStatus = async (row: JobApi.JobVO) => {
  232. const text = row.status === InfraJobStatusEnum.STOP ? '开启' : '关闭'
  233. const status =
  234. row.status === InfraJobStatusEnum.STOP ? InfraJobStatusEnum.NORMAL : InfraJobStatusEnum.STOP
  235. message
  236. .confirm('确认要' + text + '定时任务编号为"' + row.id + '"的数据项?', t('common.reminder'))
  237. .then(async () => {
  238. row.status =
  239. row.status === InfraJobStatusEnum.NORMAL
  240. ? InfraJobStatusEnum.NORMAL
  241. : InfraJobStatusEnum.STOP
  242. await JobApi.updateJobStatusApi(row.id, status)
  243. message.success(text + '成功')
  244. await reload()
  245. })
  246. .catch(() => {
  247. row.status =
  248. row.status === InfraJobStatusEnum.NORMAL
  249. ? InfraJobStatusEnum.STOP
  250. : InfraJobStatusEnum.NORMAL
  251. })
  252. }
  253. // 执行日志
  254. const handleJobLog = (rowId?: number) => {
  255. if (rowId) {
  256. push('/job/job-log?id=' + rowId)
  257. } else {
  258. push('/job/job-log')
  259. }
  260. }
  261. // 执行一次
  262. const handleRun = (row: JobApi.JobVO) => {
  263. message.confirm('确认要立即执行一次' + row.name + '?', t('common.reminder')).then(async () => {
  264. await JobApi.runJobApi(row.id)
  265. message.success('执行成功')
  266. await reload()
  267. })
  268. }
  269. // 提交按钮
  270. const submitForm = async () => {
  271. const elForm = unref(formRef)?.getElFormRef()
  272. if (!elForm) return
  273. elForm.validate(async (valid) => {
  274. if (valid) {
  275. actionLoading.value = true
  276. // 提交请求
  277. try {
  278. const data = unref(formRef)?.formModel as JobApi.JobVO
  279. if (actionType.value === 'create') {
  280. await JobApi.createJobApi(data)
  281. message.success(t('common.createSuccess'))
  282. } else {
  283. await JobApi.updateJobApi(data)
  284. message.success(t('common.updateSuccess'))
  285. }
  286. dialogVisible.value = false
  287. } finally {
  288. actionLoading.value = false
  289. await reload()
  290. }
  291. }
  292. })
  293. }
  294. </script>