TaskProgressDialog.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <el-dialog
  3. custom-class="task-progress-dialog"
  4. :visible.sync="modalIsShow"
  5. :title="title"
  6. top="10vh"
  7. width="300px"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. :show-close="false"
  11. append-to-body
  12. @open="openHandle"
  13. @close="closeHandle"
  14. >
  15. <div class="progress-body">
  16. <p>{{ tips }}</p>
  17. <el-progress :percentage="curProgress"></el-progress>
  18. </div>
  19. <div slot="footer"></div>
  20. </el-dialog>
  21. </template>
  22. <script>
  23. import useLoop from "../plugins/useLoop";
  24. import { asyncTaskProgressApi } from "../modules/question/api";
  25. export default {
  26. name: "TaskProgressDialog",
  27. props: {
  28. task: {
  29. type: Object,
  30. default() {
  31. return {
  32. id: "",
  33. name: "",
  34. };
  35. },
  36. },
  37. downloadHandle: {
  38. type: Function,
  39. default: null,
  40. },
  41. },
  42. data() {
  43. return {
  44. modalIsShow: false,
  45. curProgress: 0,
  46. startLoop: null,
  47. stopLoop: null,
  48. };
  49. },
  50. computed: {
  51. title() {
  52. return `${this.task.name}进度`;
  53. },
  54. tips() {
  55. if (this.curProgress < 100) {
  56. return `${this.task.name}中……`;
  57. }
  58. if (this.downloadHandle) {
  59. return `文件生成完毕,正在下载……`;
  60. }
  61. return `任务执行成功!`;
  62. },
  63. },
  64. mounted() {
  65. const { start, stop } = useLoop(this.getProgress, 1000);
  66. this.startLoop = start;
  67. this.stopLoop = stop;
  68. },
  69. methods: {
  70. openHandle() {
  71. this.curProgress = 0;
  72. if (!this.task.id) {
  73. this.$message.error("任务丢失!");
  74. return;
  75. }
  76. this.startLoop();
  77. },
  78. closeHandle() {
  79. this.stopLoop();
  80. },
  81. cancel() {
  82. this.modalIsShow = false;
  83. },
  84. open() {
  85. this.modalIsShow = true;
  86. },
  87. delayClose() {
  88. setTimeout(() => {
  89. this.modalIsShow = false;
  90. }, 500);
  91. },
  92. async getProgress() {
  93. const res = await asyncTaskProgressApi(this.task.id).catch(() => {});
  94. const result = res?.data;
  95. if (!result) {
  96. this.stopLoop();
  97. this.delayClose();
  98. return;
  99. }
  100. if (result.status === "FAILED") {
  101. this.$message.error(result.errMsg);
  102. this.stopLoop();
  103. this.delayClose();
  104. return;
  105. }
  106. this.curProgress = Math.ceil(Number(result.progress || 0));
  107. // 文件生成成功,开始下载
  108. if (result.status === "SUCCESS") {
  109. this.stopLoop();
  110. if (this.downloadHandle) {
  111. await this.downloadHandle();
  112. }
  113. this.$emit("finished");
  114. this.delayClose();
  115. }
  116. },
  117. },
  118. };
  119. </script>