index.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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="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 { 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 detailData = ref() // 详情 Ref
  162. const nextTimes = ref([])
  163. const shortcuts = ref([
  164. {
  165. text: '每天8点和12点 (自定义追加)',
  166. value: '0 0 8,12 * * ?'
  167. }
  168. ])
  169. // 设置标题
  170. const setDialogTile = (type: string) => {
  171. dialogTitle.value = t('action.' + type)
  172. actionType.value = type
  173. dialogVisible.value = true
  174. }
  175. // 新增操作
  176. const handleCreate = () => {
  177. setDialogTile('create')
  178. }
  179. // 导出操作
  180. const handleExport = async () => {
  181. await exportList(xGrid, '定时任务.xls')
  182. }
  183. // 修改操作
  184. const handleUpdate = async (rowId: number) => {
  185. setDialogTile('update')
  186. // 设置数据
  187. const res = await JobApi.getJobApi(rowId)
  188. unref(formRef)?.setValues(res)
  189. }
  190. // 详情操作
  191. const handleDetail = async (rowId: number) => {
  192. // 设置数据
  193. const res = await JobApi.getJobApi(rowId)
  194. detailData.value = res
  195. // 后续执行时长
  196. const jobNextTime = await JobApi.getJobNextTimesApi(rowId)
  197. nextTimes.value = jobNextTime
  198. setDialogTile('detail')
  199. }
  200. const parseTime = (time) => {
  201. if (!time) {
  202. return null
  203. }
  204. const format = '{y}-{m}-{d} {h}:{i}:{s}'
  205. let date
  206. if (typeof time === 'object') {
  207. date = time
  208. } else {
  209. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  210. time = parseInt(time)
  211. } else if (typeof time === 'string') {
  212. time = time
  213. .replace(new RegExp(/-/gm), '/')
  214. .replace('T', ' ')
  215. .replace(new RegExp(/\.[\d]{3}/gm), '')
  216. }
  217. if (typeof time === 'number' && time.toString().length === 10) {
  218. time = time * 1000
  219. }
  220. date = new Date(time)
  221. }
  222. const formatObj = {
  223. y: date.getFullYear(),
  224. m: date.getMonth() + 1,
  225. d: date.getDate(),
  226. h: date.getHours(),
  227. i: date.getMinutes(),
  228. s: date.getSeconds(),
  229. a: date.getDay()
  230. }
  231. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  232. let value = formatObj[key]
  233. // Note: getDay() returns 0 on Sunday
  234. if (key === 'a') {
  235. return ['日', '一', '二', '三', '四', '五', '六'][value]
  236. }
  237. if (result.length > 0 && value < 10) {
  238. value = '0' + value
  239. }
  240. return value || 0
  241. })
  242. return time_str
  243. }
  244. // 删除操作
  245. const handleDelete = async (rowId: number) => {
  246. await deleteData(xGrid, rowId)
  247. }
  248. const handleChangeStatus = async (row: JobApi.JobVO) => {
  249. const text = row.status === InfraJobStatusEnum.STOP ? '开启' : '关闭'
  250. const status =
  251. row.status === InfraJobStatusEnum.STOP ? InfraJobStatusEnum.NORMAL : InfraJobStatusEnum.STOP
  252. message
  253. .confirm('确认要' + text + '定时任务编号为"' + row.id + '"的数据项?', t('common.reminder'))
  254. .then(async () => {
  255. row.status =
  256. row.status === InfraJobStatusEnum.NORMAL
  257. ? InfraJobStatusEnum.NORMAL
  258. : InfraJobStatusEnum.STOP
  259. await JobApi.updateJobStatusApi(row.id, status)
  260. message.success(text + '成功')
  261. await getList(xGrid)
  262. })
  263. .catch(() => {
  264. row.status =
  265. row.status === InfraJobStatusEnum.NORMAL
  266. ? InfraJobStatusEnum.STOP
  267. : InfraJobStatusEnum.NORMAL
  268. })
  269. }
  270. // 执行日志
  271. const handleJobLog = (rowId: number) => {
  272. if (rowId) {
  273. push('/job/job-log?id=' + rowId)
  274. } else {
  275. push('/job/job-log')
  276. }
  277. }
  278. // 执行一次
  279. const handleRun = (row: JobApi.JobVO) => {
  280. message.confirm('确认要立即执行一次' + row.name + '?', t('common.reminder')).then(async () => {
  281. await JobApi.runJobApi(row.id)
  282. message.success('执行成功')
  283. await getList(xGrid)
  284. })
  285. }
  286. // 提交按钮
  287. const submitForm = async () => {
  288. const elForm = unref(formRef)?.getElFormRef()
  289. if (!elForm) return
  290. elForm.validate(async (valid) => {
  291. if (valid) {
  292. actionLoading.value = true
  293. // 提交请求
  294. try {
  295. const data = unref(formRef)?.formModel as JobApi.JobVO
  296. if (actionType.value === 'create') {
  297. await JobApi.createJobApi(data)
  298. message.success(t('common.createSuccess'))
  299. } else {
  300. await JobApi.updateJobApi(data)
  301. message.success(t('common.updateSuccess'))
  302. }
  303. dialogVisible.value = false
  304. } finally {
  305. actionLoading.value = false
  306. await getList(xGrid)
  307. }
  308. }
  309. })
  310. }
  311. </script>