QuestionImportDialog.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <el-dialog
  3. custom-class="side-dialog"
  4. :visible.sync="modalIsShow"
  5. title="试题导入"
  6. width="700px"
  7. :modal="false"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. append-to-body
  11. destroy-on-close
  12. @open="visibleChange"
  13. >
  14. <el-form
  15. ref="modalFormComp"
  16. :model="modalForm"
  17. :rules="rules"
  18. label-width="120px"
  19. >
  20. <el-form-item label="导入类型">
  21. <el-radio-group v-model="importType">
  22. <el-radio label="docx">word</el-radio>
  23. <el-radio label="zip">zip</el-radio>
  24. </el-radio-group>
  25. </el-form-item>
  26. <el-form-item
  27. v-if="importType === 'docx'"
  28. prop="courseId"
  29. label="课程名称"
  30. >
  31. <course-select v-model="modalForm.courseId"> </course-select>
  32. </el-form-item>
  33. <el-form-item label="是否使用原卷">
  34. <el-radio-group v-model="modalForm.useOriginalPaper">
  35. <el-radio :label="true">是</el-radio>
  36. <el-radio :label="false">否</el-radio>
  37. </el-radio-group>
  38. </el-form-item>
  39. <el-form-item
  40. v-if="modalForm.useOriginalPaper && importType === 'docx'"
  41. prop="name"
  42. label="试卷名称"
  43. >
  44. <el-input
  45. v-model="modalForm.name"
  46. placeholder="请输入试卷名称"
  47. clearable
  48. >
  49. </el-input>
  50. </el-form-item>
  51. <el-form-item v-if="modalForm.useOriginalPaper" label="总分校验">
  52. <el-radio-group v-model="modalForm.checkTotalScore">
  53. <el-radio :label="true">开启</el-radio>
  54. <el-radio :label="false">关闭</el-radio>
  55. </el-radio-group>
  56. </el-form-item>
  57. <el-form-item
  58. v-if="modalForm.checkTotalScore && modalForm.useOriginalPaper"
  59. label="试卷总分"
  60. prop="totalScore"
  61. >
  62. <el-input-number
  63. v-model="modalForm.totalScore"
  64. style="width: 125px"
  65. :min="1"
  66. :max="1000"
  67. :step="1"
  68. step-strictly
  69. :controls="false"
  70. ></el-input-number
  71. ></el-form-item>
  72. <el-form-item prop="file">
  73. <import-file
  74. ref="ImportFile"
  75. :format="importFileTypes"
  76. :template-url="templateUrl"
  77. only-fetch-file
  78. @file-change="fileChange"
  79. @confirm="confirm"
  80. ></import-file>
  81. </el-form-item>
  82. </el-form>
  83. <div slot="footer"></div>
  84. </el-dialog>
  85. </template>
  86. <script>
  87. import ImportFile from "@/components/ImportFile.vue";
  88. import { questionImportFileUpload } from "../api";
  89. import { QUESTION_API } from "@/constants/constants";
  90. import { mapState } from "vuex";
  91. const initModalForm = {
  92. courseId: null,
  93. name: "",
  94. checkTotalScore: false,
  95. useOriginalPaper: false,
  96. totalScore: 0,
  97. };
  98. export default {
  99. name: "QuestionImportDialog",
  100. components: { ImportFile },
  101. data() {
  102. return {
  103. modalIsShow: false,
  104. importType: "docx",
  105. modalForm: {
  106. ...initModalForm,
  107. },
  108. rules: {
  109. courseId: [
  110. {
  111. required: true,
  112. message: "请选择课程",
  113. trigger: "change",
  114. },
  115. ],
  116. name: [
  117. {
  118. required: true,
  119. message: "请输入试卷名称",
  120. trigger: "change",
  121. },
  122. ],
  123. totalScore: [
  124. {
  125. required: true,
  126. message: "请输入试卷总分",
  127. trigger: "change",
  128. },
  129. ],
  130. file: [
  131. {
  132. validate: (rule, value, callback) => {
  133. if (!this.fileData.file) {
  134. return callback(new Error(`请输入选择文件`));
  135. }
  136. callback();
  137. },
  138. trigger: "change",
  139. },
  140. ],
  141. },
  142. fileData: {},
  143. templateUrl: "",
  144. loading: false,
  145. };
  146. },
  147. computed: {
  148. ...mapState({ user: (state) => state.user }),
  149. importFileTypes() {
  150. return this.importType === "docx" ? ["docx", "doc"] : [this.importType];
  151. },
  152. },
  153. watch: {
  154. "modalForm.useOriginalPaper": {
  155. handler(val) {
  156. if (!val) {
  157. this.modalForm.name = "";
  158. this.modalForm.totalScore = 0;
  159. }
  160. },
  161. },
  162. "modalForm.checkTotalScore": {
  163. handler(val) {
  164. if (!val) {
  165. this.modalForm.totalScore = 0;
  166. }
  167. },
  168. },
  169. importType(val) {
  170. if (val === "zip") {
  171. this.modalForm.courseId = null;
  172. }
  173. },
  174. },
  175. mounted() {
  176. this.templateUrl = `${QUESTION_API}/import/paper/template?$key=${this.user.key}&$token=${this.user.token}`;
  177. },
  178. methods: {
  179. visibleChange() {
  180. this.modalForm = { ...initModalForm };
  181. this.fileData = {};
  182. },
  183. cancel() {
  184. this.modalIsShow = false;
  185. },
  186. open() {
  187. this.modalIsShow = true;
  188. },
  189. fileChange(fileData) {
  190. this.fileData = fileData;
  191. this.$refs.modalFormComp.validateField("file", (err) => {
  192. console.log(err);
  193. });
  194. },
  195. async confirm() {
  196. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  197. if (!valid) return;
  198. if (this.loading) return;
  199. this.loading = true;
  200. this.$refs.ImportFile.setLoading(true);
  201. let formData = new FormData();
  202. Object.entries(this.modalForm).forEach(([key, val]) => {
  203. if (val !== null) formData.append(key, val);
  204. });
  205. formData.append("file", this.fileData.file);
  206. const res = await questionImportFileUpload(formData, {
  207. md5: this.fileData.md5,
  208. }).catch(() => {});
  209. this.loading = false;
  210. this.$refs.ImportFile.setLoading(false);
  211. if (!res) return;
  212. this.$message.success("导入成功!");
  213. this.$emit("modified", {
  214. ...res.data,
  215. importData: this.modalForm,
  216. });
  217. this.cancel();
  218. },
  219. },
  220. };
  221. </script>