ImportPaperInfo.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <section class="content">
  3. <!-- 正文信息 -->
  4. <div
  5. v-loading.body="fileLoading"
  6. class="part-box"
  7. element-loading-text="试题上传中,请稍后..."
  8. >
  9. <h2 class="part-box-title">文件上传</h2>
  10. <el-form class="padding-tb-20" :model="formSearch" label-width="100px">
  11. <el-form-item label="导入类型">
  12. <el-radio v-model="importType" label="word">word</el-radio>
  13. <el-radio v-model="importType" label="zip">zip</el-radio>
  14. </el-form-item>
  15. <el-form-item v-show="importType == 'word'" label="课程名称">
  16. <el-select
  17. v-model="formSearch.courseNo"
  18. class="form_width"
  19. filterable
  20. :remote-method="getCourses"
  21. remote
  22. clearable
  23. placeholder="请选择"
  24. @change="searchCourseName"
  25. @clear="getCourses('')"
  26. >
  27. <el-option
  28. v-for="item in courseInfoSelect"
  29. :key="item.courseNo"
  30. :label="item.courseInfo"
  31. :value="item.courseNo"
  32. >
  33. </el-option>
  34. </el-select>
  35. </el-form-item>
  36. <el-form-item v-show="importType == 'word'" label="试卷名称">
  37. <el-input
  38. v-model="formSearch.name"
  39. placeholder="试卷名称"
  40. style="width: 223px"
  41. ></el-input>
  42. </el-form-item>
  43. <el-form-item label="总分校验">
  44. <el-radio v-model="scoreCheck" label="1">开启</el-radio>
  45. <el-radio v-model="scoreCheck" label="0">关闭</el-radio>
  46. </el-form-item>
  47. <el-form-item v-if="scoreCheck == 1" label="试卷总分">
  48. <el-input
  49. v-model="formSearch.totalScore"
  50. placeholder="试卷总分"
  51. class="form_width"
  52. ></el-input>
  53. </el-form-item>
  54. <!--
  55. <el-row>
  56. <el-form-item label="相同大题名称" label-width="120px" class="pull-left">
  57. <el-radio v-model="formSearch.sameName" label="1">合并</el-radio>
  58. <el-radio v-model="formSearch.sameName" label="0">不合并</el-radio>
  59. </el-form-item>
  60. </el-row>
  61. -->
  62. <el-form-item class="padding-tb-20">
  63. <el-upload
  64. ref="upload"
  65. :accept="importFileType"
  66. :action="uploadAction"
  67. :headers="uploadHeaders"
  68. :data="uploadData"
  69. :before-upload="beforeUpload"
  70. :on-progress="uploadProgress"
  71. :on-success="uploadSuccess"
  72. :on-error="uploadError"
  73. :file-list="fileList"
  74. :auto-upload="false"
  75. :multiple="false"
  76. >
  77. <el-button
  78. slot="trigger"
  79. type="primary"
  80. icon="icon icon-search-white"
  81. >选择文件</el-button
  82. >
  83. <el-button
  84. type="primary"
  85. icon="icon icon-save-white"
  86. @click="submitUpload"
  87. >确认上传
  88. </el-button>
  89. <el-button
  90. type="info"
  91. plain
  92. icon="icon el-icon-download"
  93. @click="donwTemplate"
  94. >下载word模板
  95. </el-button>
  96. <el-button
  97. type="danger"
  98. plain
  99. icon="icon icon-delete"
  100. @click="removeFile"
  101. >清空文件
  102. </el-button>
  103. <el-button type="danger" plain icon="icon icon-back" @click="back"
  104. >返回</el-button
  105. >
  106. <div slot="tip" class="el-upload__tip">只能上传docx和zip文件</div>
  107. </el-upload>
  108. </el-form-item>
  109. </el-form>
  110. <el-dialog title="错误提示" :visible.sync="errDialog" append-to-body>
  111. <span style="font-size: large">{{ errMessage }} !</span>
  112. <span slot="footer" class="dialog-footer">
  113. <el-button @click="errDialog = false">确定</el-button>
  114. </span>
  115. </el-dialog>
  116. </div>
  117. </section>
  118. </template>
  119. <script>
  120. import { QUESTION_API } from "@/constants/constants";
  121. import { mapState } from "vuex";
  122. export default {
  123. name: "ImportPaperInfo",
  124. data() {
  125. return {
  126. importType: "word",
  127. scoreCheck: "0",
  128. formSearch: {
  129. courseNo: "",
  130. couresName: "",
  131. name: "",
  132. totalScore: "",
  133. courseLevel: "",
  134. sameName: "0",
  135. },
  136. courseList: [],
  137. uploadAction: "",
  138. errMessage: "",
  139. uploadData: {},
  140. fileLoading: false,
  141. button_tpye: false,
  142. errDialog: false,
  143. fileList: [],
  144. uploadHeaders: {},
  145. };
  146. },
  147. computed: {
  148. ...mapState({ user: (state) => state.user }),
  149. courseInfoSelect() {
  150. var courseList = [];
  151. for (let course of this.courseList) {
  152. var courseInfo = course.name + "(" + course.code + ")";
  153. var courseNo = course.code;
  154. courseList.push({ courseNo: courseNo, courseInfo: courseInfo });
  155. }
  156. return courseList;
  157. },
  158. importFileType() {
  159. if (this.importType == "word") {
  160. return ".docx";
  161. } else if (this.importType == "zip") {
  162. return ".zip";
  163. }
  164. return ".docx";
  165. },
  166. },
  167. watch: {
  168. scoreCheck: function () {
  169. if (this.scoreCheck == 0) {
  170. this.formSearch.totalScore = "";
  171. }
  172. },
  173. },
  174. created() {
  175. this.uploadAction = QUESTION_API + "/importPaper";
  176. this.uploadData = { name: this.formSearch.name };
  177. this.uploadHeaders = {
  178. key: this.user.key,
  179. token: this.user.token,
  180. };
  181. },
  182. mounted() {
  183. setTimeout(() => {
  184. this.$store.commit("UPDATE_CURRENT_PATHS", [
  185. "题库管理",
  186. "题库列表",
  187. "文件上传",
  188. ]);
  189. }, 200);
  190. this.getCourses("");
  191. },
  192. methods: {
  193. searchCourseName() {
  194. for (let course of this.courseList) {
  195. if (course.code == this.formSearch.courseNo) {
  196. this.formSearch.courseName = course.name;
  197. this.formSearch.courseLevel = course.level;
  198. console.log(this.formSearch.courseLevel);
  199. }
  200. }
  201. },
  202. beforeUpload(file) {
  203. console.log(file);
  204. },
  205. uploadProgress() {
  206. console.log("uploadProgress");
  207. },
  208. uploadSuccess(res) {
  209. this.fileLoading = false;
  210. sessionStorage.setItem("question_back", "true");
  211. if ("PASS" == res) {
  212. this.$notify({
  213. message: "上传成功",
  214. type: "success",
  215. });
  216. this.$router.push({ path: "/questions/import_paper/0" });
  217. } else {
  218. this.$confirm("已开启审核功能.是否跳转到待审列表?", "上传成功", {
  219. confirmButtonText: "确定",
  220. cancelButtonText: "取消",
  221. type: "warning",
  222. })
  223. .then(() => {
  224. this.$router.push({ path: "/questions/paper_pending_trial/0" });
  225. })
  226. .catch(() => {
  227. this.$router.push({ path: "/questions/import_paper/0" });
  228. });
  229. }
  230. },
  231. uploadError(err) {
  232. var result = err.message.match(/\{.+}/);
  233. var errMessage = JSON.parse(result[0]).desc;
  234. this.errDialog = true;
  235. this.errMessage = errMessage;
  236. this.fileLoading = false;
  237. },
  238. initUpload() {
  239. this.fileList = [];
  240. this.formSearch.name = "";
  241. this.formSearch.courseNo = "";
  242. },
  243. checkUpload() {
  244. if (this.importType == "word" && !this.formSearch.courseNo) {
  245. this.$notify({
  246. message: "课程名称不能为空",
  247. type: "error",
  248. });
  249. return false;
  250. } else {
  251. this.uploadData.courseNo = this.formSearch.courseNo;
  252. this.uploadData.courseName = this.formSearch.courseName;
  253. this.uploadData.level = this.formSearch.courseLevel;
  254. this.uploadData.sameName = this.formSearch.sameName;
  255. }
  256. if (this.importType == "word" && !this.formSearch.name) {
  257. this.$notify({
  258. message: "试卷名称不能为空",
  259. type: "error",
  260. });
  261. return false;
  262. } else {
  263. this.uploadData.name = this.formSearch.name;
  264. }
  265. if (this.scoreCheck == 1 && !this.formSearch.totalScore) {
  266. this.$notify({
  267. message: "试卷总分不能为空",
  268. type: "error",
  269. });
  270. return false;
  271. } else {
  272. this.uploadData.totalScore = this.formSearch.totalScore;
  273. }
  274. var fileList = this.$refs.upload.uploadFiles;
  275. if (fileList.length == 0) {
  276. this.$notify({
  277. message: "上传文件不能为空",
  278. type: "error",
  279. });
  280. return false;
  281. }
  282. if (fileList.length > 1) {
  283. this.$notify({
  284. message: "每次只能上传一个word或zip文件",
  285. type: "error",
  286. });
  287. return false;
  288. }
  289. for (let file of fileList) {
  290. if (!file.name.endsWith(".docx") && !file.name.endsWith(".zip")) {
  291. this.$notify({
  292. message: "上传文件必须为docx或zip格式",
  293. type: "error",
  294. });
  295. this.initUpload();
  296. return false;
  297. }
  298. }
  299. return true;
  300. },
  301. donwTemplate() {
  302. window.location.href =
  303. QUESTION_API +
  304. "/import/paper/template?$key=" +
  305. this.user.key +
  306. "&$token=" +
  307. this.user.token;
  308. },
  309. //确定上传
  310. submitUpload() {
  311. console.log("this form:", this.formSearch);
  312. if (!this.checkUpload()) {
  313. return false;
  314. }
  315. this.$refs.upload.submit();
  316. this.fileLoading = true;
  317. },
  318. //清空文件
  319. removeFile() {
  320. // this.fileList = [];
  321. this.$refs.upload.clearFiles();
  322. },
  323. //返回
  324. back() {
  325. this.$router.push({ path: "/questions/import_paper/0" });
  326. },
  327. //查询所有课程
  328. getCourses(query) {
  329. query = query.trim();
  330. this.courseLoading = true;
  331. this.$http
  332. .get(QUESTION_API + "/course/query?name=" + query + "&enable=true")
  333. .then((response) => {
  334. this.courseList = response.data;
  335. this.courseLoading = false;
  336. });
  337. },
  338. },
  339. };
  340. </script>