InvigilateImportDialog.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <el-dialog
  3. ref="dialog"
  4. :title="'导入' + '监考设置'"
  5. width="450px"
  6. :visible.sync="visible"
  7. @close="closeDialog"
  8. >
  9. <el-form
  10. :model="form"
  11. ref="form"
  12. :rules="rules"
  13. label-position="right"
  14. label-width="120px"
  15. >
  16. <el-row>
  17. <el-form-item label="选择文件">
  18. <input @change="selectFile" type="file" />
  19. </el-form-item>
  20. </el-row>
  21. <el-row class="d-flex justify-content-center">
  22. <el-button type="primary" @click="submitForm" :loading="loading"
  23. >导入</el-button
  24. >
  25. <el-button @click="closeDialog">取消</el-button>
  26. </el-row>
  27. </el-form>
  28. </el-dialog>
  29. </template>
  30. <script>
  31. import { importInvigilator } from "@/api/examwork-task";
  32. import MD5 from "js-md5";
  33. export default {
  34. name: "InvigilateImportDialog",
  35. props: {
  36. examId: String,
  37. },
  38. data() {
  39. return {
  40. visible: false,
  41. form: {
  42. file: "",
  43. fileName: "",
  44. },
  45. rules: {},
  46. loading: false,
  47. };
  48. },
  49. watch: {
  50. examId: {
  51. immediate: true,
  52. handler() {
  53. this.form = {
  54. processPaper: false,
  55. processAnswer: false,
  56. objectiveShuffle: false,
  57. optionShuffle: false,
  58. audioPlayCount: 0,
  59. file: "",
  60. fileName: "",
  61. };
  62. },
  63. },
  64. },
  65. methods: {
  66. openDialog() {
  67. this.visible = true;
  68. },
  69. closeDialog() {
  70. this.visible = false;
  71. },
  72. selectFile(e) {
  73. this.form.file = e.target.files[0];
  74. this.form.fileName = this.form.file?.name;
  75. },
  76. async submitForm() {
  77. async function blobToArray(blob) {
  78. return new Promise((resolve) => {
  79. var reader = new FileReader();
  80. reader.addEventListener("loadend", function () {
  81. // reader.result contains the contents of blob as a typed array
  82. resolve(reader.result);
  83. });
  84. reader.readAsArrayBuffer(blob);
  85. });
  86. }
  87. const ab = await blobToArray(this.form.file);
  88. const md5 = MD5(ab);
  89. try {
  90. this.loading = true;
  91. await importInvigilator({
  92. examId: this.examId,
  93. file: this.form.file,
  94. fileName: this.form.fileName,
  95. md5,
  96. });
  97. this.$emit("reload");
  98. this.$notify({ title: "导入任务已成功启动", type: "success" });
  99. this.closeDialog();
  100. } finally {
  101. this.loading = false;
  102. }
  103. },
  104. },
  105. };
  106. </script>
  107. <style></style>