QuestionImportDialog.vue 6.2 KB

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