index.vue 9.7 KB

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