PaperPreviewDialog.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <el-dialog
  3. class="paper-preview-dialog"
  4. :visible.sync="modalIsShow"
  5. :title="title"
  6. :close-on-click-modal="false"
  7. :close-on-press-escape="false"
  8. append-to-body
  9. fullscreen
  10. @open="visibleChange"
  11. >
  12. <div v-if="loading" class="preview-loading">
  13. <i class="el-icon-loading"></i>
  14. </div>
  15. <div ref="PaperPreview" class="paper-preview"></div>
  16. <div slot="footer">
  17. <el-button type="primary" :disabled="loading" @click="toOrigin"
  18. >实际大小</el-button
  19. >
  20. <el-button
  21. type="primary"
  22. :disabled="loading || rate >= maxScale"
  23. @click="toMagnify"
  24. >放大</el-button
  25. >
  26. <el-button
  27. type="primary"
  28. :disabled="loading || rate <= minScale"
  29. @click="toShrink"
  30. >缩小</el-button
  31. >
  32. <el-button type="success" :loading="loading" @click="toDownload"
  33. >下载</el-button
  34. >
  35. </div>
  36. </el-dialog>
  37. </template>
  38. <script>
  39. import { buildCanvas, downloadPaper } from "./downloadPaper";
  40. import papers from "./paper";
  41. export default {
  42. name: "modify-user",
  43. props: {
  44. instance: {
  45. type: Object,
  46. default() {
  47. return {};
  48. }
  49. }
  50. },
  51. computed: {
  52. title() {
  53. return "试卷预览" + this.instance.studentName;
  54. }
  55. },
  56. data() {
  57. return {
  58. modalIsShow: false,
  59. loading: false,
  60. papers: [],
  61. rate: 1,
  62. minScale: 0.5,
  63. maxScale: 2,
  64. canvas: null
  65. };
  66. },
  67. methods: {
  68. visibleChange() {
  69. this.papers = papers;
  70. this.rebuild();
  71. },
  72. cancel() {
  73. this.modalIsShow = false;
  74. },
  75. open() {
  76. this.modalIsShow = true;
  77. },
  78. async rebuild() {
  79. this.loading = true;
  80. this.canvas = null;
  81. const canvas = await buildCanvas(papers, this.rate).catch(() => {});
  82. if (!canvas) {
  83. this.$message.error("试卷绘制错误!");
  84. this.loading = false;
  85. return;
  86. }
  87. const previewDom = this.$refs.PaperPreview;
  88. previewDom.childNodes.forEach(node => {
  89. previewDom.removeChild(node);
  90. });
  91. previewDom.appendChild(canvas);
  92. this.canvas = canvas;
  93. this.loading = false;
  94. },
  95. toOrigin() {
  96. this.rate = 1;
  97. this.rebuild();
  98. },
  99. toMagnify() {
  100. const rate = this.rate * 1.2;
  101. this.rate = rate >= this.maxScale ? this.maxScale : rate;
  102. this.rebuild();
  103. },
  104. toShrink() {
  105. const rate = this.rate * 0.75;
  106. this.rate = rate <= this.minScale ? this.minScale : rate;
  107. this.rebuild();
  108. },
  109. async toDownload() {
  110. if (this.loading) return;
  111. this.loading = true;
  112. const filename = ``;
  113. const res = await downloadPaper(this.papers, filename).catch(() => {});
  114. this.loading = false;
  115. if (!res) {
  116. this.$message.error("下载失败,请重新尝试!");
  117. return;
  118. }
  119. this.$message.success("下载成功!");
  120. }
  121. }
  122. };
  123. </script>