ScoreReportPreview.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <el-dialog
  3. class="report-preview"
  4. :visible.sync="modalIsShow"
  5. top="0"
  6. :close-on-click-modal="false"
  7. :close-on-press-escape="false"
  8. :show-close="false"
  9. append-to-body
  10. fullscreen
  11. >
  12. <div class="box-justify" slot="title">
  13. <h2 class="el-dialog__title">成绩报告</h2>
  14. <span>{{ title }}</span>
  15. <div>
  16. <el-button type="primary" :loading="downloading" @click="toExportScore"
  17. >下载报告</el-button
  18. >
  19. <el-button type="danger" @click="cancel">取消</el-button>
  20. </div>
  21. </div>
  22. <report v-if="modalIsShow" ref="reportRef" :baseinfo="instance"></report>
  23. <div slot="footer"></div>
  24. </el-dialog>
  25. </template>
  26. <script>
  27. import Report from "./report/Report.vue";
  28. import { downloadByApi } from "@/plugins/download";
  29. import { scoreReportExport } from "../api";
  30. export default {
  31. name: "score-report-preview",
  32. components: { Report },
  33. props: {
  34. instance: {
  35. type: Object,
  36. default() {
  37. return {};
  38. },
  39. },
  40. },
  41. data() {
  42. return {
  43. modalIsShow: false,
  44. downloading: false,
  45. };
  46. },
  47. computed: {
  48. title() {
  49. const course = `${this.instance.courseName}(${this.instance.courseCode})`;
  50. return course;
  51. },
  52. },
  53. methods: {
  54. cancel() {
  55. this.modalIsShow = false;
  56. },
  57. open() {
  58. this.modalIsShow = true;
  59. },
  60. async toExportScore() {
  61. if (this.downloading) return;
  62. this.downloading = true;
  63. const filename = `${this.title}.pdf`;
  64. const res = await downloadByApi(() => {
  65. const formData = new FormData();
  66. const htmlFile = new File(
  67. [this.$refs.reportRef.getTemp()],
  68. `${Date.now()}.html`,
  69. { type: "text/html" }
  70. );
  71. formData.append("file", htmlFile);
  72. return scoreReportExport(formData);
  73. }, filename).catch((e) => {
  74. this.$message.error(e || "下载失败,请重新尝试!");
  75. });
  76. this.downloading = false;
  77. if (!res) return;
  78. this.$message.success("下载成功!");
  79. },
  80. },
  81. };
  82. </script>