Explorar o código

文件上传调整

zhangjie hai 1 ano
pai
achega
b54e9e9787

+ 1 - 1
src/components/ImportFile.vue

@@ -187,7 +187,7 @@ export default {
       this.loading = false;
       this.res = {
         success: false,
-        message: error.response.data.desc,
+        message: error.response.data.message,
       };
       this.uploadDataDict = {};
       this.filename = "";

+ 1 - 1
src/components/ImportFileDialog.vue

@@ -203,7 +203,7 @@ export default {
       this.loading = false;
       this.res = {
         success: false,
-        message: error.response.data.desc,
+        message: error.response.data.message,
       };
       this.uploadDataDict = {};
       this.filename = "";

+ 73 - 28
src/components/UploadButton.vue

@@ -1,28 +1,38 @@
 <template>
-  <el-upload
-    :action="uploadUrl"
-    :headers="headers"
-    :max-size="maxSize"
-    :format="format"
-    :accept="accept"
-    :data="uploadDataDict"
-    :before-upload="handleBeforeUpload"
-    :on-error="handleError"
-    :on-success="handleSuccess"
-    :http-request="upload"
-    :disabled="disabled"
-    :show-file-list="false"
-    style="display: inline-block; margin: 0 10px"
-    ref="UploadComp"
-  >
-    <el-button
-      :type="btnType"
-      :icon="btnIcon"
-      :loading="loading"
-      :disabled="disabled"
-      >{{ btnContent }}</el-button
+  <div>
+    <el-input
+      v-if="!autoUpload"
+      :style="{ width: inputWidth, marginRight: '10px' }"
+      v-model.trim="filename"
+      placeholder="文件名称"
+      readonly
+    ></el-input>
+    <el-upload
+      :action="uploadUrl"
+      :headers="headers"
+      :max-size="maxSize"
+      :format="format"
+      :accept="accept"
+      :data="uploadDataDict"
+      :on-error="handleError"
+      :on-success="handleSuccess"
+      :on-change="fileChange"
+      :http-request="upload"
+      :disabled="disabled || loading"
+      :auto-upload="autoUpload"
+      :show-file-list="false"
+      style="display: inline-block"
+      ref="UploadComp"
     >
-  </el-upload>
+      <el-button
+        :type="btnType"
+        :icon="btnIcon"
+        :loading="loading"
+        :disabled="disabled || loading"
+        >{{ btnContent }}</el-button
+      >
+    </el-upload>
+  </div>
 </template>
 
 <script>
@@ -32,6 +42,10 @@ import { $httpWithMsg } from "@/plugins/axios";
 export default {
   name: "UploadButton",
   props: {
+    inputWidth: {
+      type: String,
+      default: "203px",
+    },
     btnIcon: {
       type: String,
     },
@@ -65,10 +79,15 @@ export default {
       type: Number,
       default: 20 * 1024 * 1024,
     },
+    addFileParam: {
+      type: String,
+      default: "file",
+    },
     addFilenameParam: {
       type: String,
       default: "filename",
     },
+    autoUpload: { type: Boolean, default: true },
     disabled: { type: Boolean, default: false },
   },
   data() {
@@ -79,6 +98,8 @@ export default {
       res: {},
       loading: false,
       uploadDataDict: {},
+      filename: "",
+      fileValid: false,
     };
   },
   methods: {
@@ -88,7 +109,14 @@ export default {
         ? this.format.some((item) => item.toLocaleLowerCase() === _file_format)
         : true;
     },
+    fileChange(fileObj) {
+      if (fileObj.status === "ready") {
+        this.handleBeforeUpload(fileObj.raw).catch(() => {});
+      }
+    },
     async handleBeforeUpload(file) {
+      this.res = {};
+      this.filename = file.name;
       this.uploadDataDict = {
         ...this.uploadData,
       };
@@ -96,19 +124,34 @@ export default {
 
       if (!this.checkFileFormat(file.name)) {
         this.handleFormatError();
+        this.fileValid = false;
         return Promise.reject();
       }
 
       if (file.size > this.maxSize) {
         this.handleExceededSize();
+        this.fileValid = false;
         return Promise.reject();
       }
 
       const md5 = await fileMD5(file);
       this.headers["md5"] = md5;
-      this.loading = true;
+      this.fileValid = true;
+
+      if (this.autoUpload) {
+        this.loading = true;
+        return true;
+      }
 
-      return true;
+      return false;
+    },
+    submit() {
+      if (!this.fileValid) {
+        this.$message.error("文件错误,请重新选择文件!");
+        return;
+      }
+      this.loading = true;
+      this.$refs.UploadComp.submit();
     },
     upload(options) {
       if (!options.file) return Promise.reject("文件丢失");
@@ -117,7 +160,7 @@ export default {
       Object.entries(options.data).forEach(([k, v]) => {
         formData.append(k, v);
       });
-      formData.append("file", options.file);
+      formData.append(this.addFileParam, options.file);
       this.$emit("uploading");
 
       return $httpWithMsg.post(options.action, formData, {
@@ -128,11 +171,13 @@ export default {
       this.loading = false;
       this.res = {
         success: false,
-        message: error.response.data.desc,
+        message: error.response.data.message,
       };
       this.uploadDataDict = {};
+      this.filename = "";
+      this.fileValid = false;
       this.$refs.UploadComp.clearFiles();
-      this.$emit("upload-error", error);
+      this.$emit("upload-error", error.response);
     },
     handleSuccess(res) {
       this.loading = false;

+ 5 - 2
src/modules/question/components/QuestionImportEdit.vue

@@ -24,7 +24,7 @@
             :upload-data="uploadData"
             :upload-url="uploadUrl"
             :format="importFileTypes"
-            @valid-error="uploadError"
+            @valid-error="validError"
             @upload-error="uploadError"
             @upload-success="uploaded"
           ></upload-button>
@@ -962,7 +962,10 @@ export default {
         detailInfo: res.data.detailInfo,
       });
     },
-    uploadError(error) {
+    uploadError(res) {
+      this.$message.error(res.data.message || "上传错误");
+    },
+    validError(error) {
       this.$message.error(error.message);
     },
     // rich test editor

+ 18 - 163
src/modules/questions/views/Course.vue

@@ -1,10 +1,5 @@
 <template>
-  <section
-    v-loading.body="fileLoading"
-    v-loading.fullscreen="loading"
-    element-loading-text="请稍后..."
-    class="content"
-  >
+  <section element-loading-text="请稍后..." class="content">
     <div class="part-box">
       <h2 class="part-box-title">课程列表</h2>
 
@@ -53,7 +48,7 @@
             type="primary"
             plain
             icon="icon icon-import"
-            @click="impCourse"
+            @click="importCourse"
             >导入</el-button
           >
           <el-button
@@ -197,69 +192,13 @@
     </el-dialog>
 
     <!-- 导入弹窗 -->
-    <el-dialog
-      title="导入课程"
-      width="520px"
-      :visible.sync="impDialog"
-      :modal="false"
-      append-to-body
-      custom-class="side-dialog"
-    >
-      <el-form>
-        <el-form-item style="margin-left: 20px">
-          <el-upload
-            ref="upload"
-            class="form_left"
-            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
-            :action="uploadAction"
-            :headers="uploadHeaders"
-            :data="uploadData"
-            :before-upload="beforeUpload"
-            :on-progress="uploadProgress"
-            :on-success="uploadSuccess"
-            :on-error="uploadError"
-            :file-list="fileList"
-            :auto-upload="false"
-            :multiple="false"
-          >
-            <el-button
-              slot="trigger"
-              size="small"
-              type="primary"
-              icon="icon icon-search-white"
-            >
-              选择文件
-            </el-button>
-            <el-button
-              size="small"
-              type="primary"
-              icon="icon icon-save-white"
-              @click="submitUpload"
-            >
-              确认上传
-            </el-button>
-            <el-button
-              size="small"
-              type="primary"
-              icon="icon icon-delete-white"
-              @click="removeFile"
-            >
-              清空文件
-            </el-button>
-            <el-button
-              size="small"
-              type="primary"
-              icon="icon icon-export-white"
-              :loading="tempDownloading"
-              @click="exportFile"
-            >
-              下载模板
-            </el-button>
-            <div slot="tip" class="el-upload__tip">只能上传xlsx文件</div>
-          </el-upload>
-        </el-form-item>
-      </el-form>
-    </el-dialog>
+    <import-file-dialog
+      ref="ImportCourseDialog"
+      dialog-title="导入课程"
+      :template-url="courseTemplateUrl"
+      :upload-url="uploadCourseUrl"
+      @uploaded="uploadSuccess"
+    ></import-file-dialog>
 
     <!-- 导入错误信息列表 -->
     <el-dialog title="错误提示" :visible.sync="errDialog" append-to-body>
@@ -283,9 +222,11 @@ import { ENABLE_TYPE, LEVEL_TYPE } from "../constants/constants.js";
 import { mapState } from "vuex";
 import { courseExportApi } from "../api";
 import { downloadByApi } from "@/plugins/download";
+import ImportFileDialog from "@/components/ImportFileDialog.vue";
 
 export default {
   name: "Course",
+  components: { ImportFileDialog },
   data() {
     return {
       formSearch: {
@@ -309,20 +250,15 @@ export default {
       pageSize: 10,
       total: 10,
 
-      impDialog: false,
-      uploadAction: QUESTION_API + "/course/import",
+      uploadCourseUrl: `${QUESTION_API}/course/import`,
+      courseTemplateUrl: `${QUESTION_API}/course/importTemplate`,
       uploadHeaders: {},
-      uploadData: {},
       errMessages: [],
       errDialog: false,
-      fileLoading: false,
-      loading: false,
-      fileList: [],
 
       courseDialog: false,
       relationDialog: false,
       downloading: false,
-      tempDownloading: false,
 
       rules: {
         name: [
@@ -708,103 +644,22 @@ export default {
       });
     },
     //导入
-    impCourse() {
-      this.impDialog = true;
-      this.initUpload();
-    },
-    initUpload() {
-      this.fileList = [];
-    },
-    beforeUpload(file) {
-      console.log(file);
-    },
-    uploadProgress() {
-      console.log("uploadProgress");
+    importCourse() {
+      this.$refs.ImportCourseDialog.open();
     },
     uploadSuccess(response) {
-      if (!response.hasError) {
+      const { hasError, failRecords } = response.data;
+      if (!hasError) {
         this.$notify({
           message: "上传成功",
           type: "success",
         });
-        this.fileLoading = false;
-        this.impDialog = false;
         this.searchForm();
       } else {
-        this.fileLoading = false;
-        this.impDialog = false;
-        this.errMessages = response.failRecords;
+        this.errMessages = failRecords;
         this.errDialog = true;
       }
     },
-    uploadError(response) {
-      var json = JSON.parse(response.message);
-      if (response.status == 500) {
-        this.$notify({
-          message: json.desc,
-          type: "error",
-        });
-      }
-      this.fileLoading = false;
-    },
-    //确定上传
-    submitUpload() {
-      if (!this.checkUpload()) {
-        return false;
-      }
-      this.$refs.upload.submit();
-      this.fileLoading = true;
-    },
-    checkUpload() {
-      var fileList = this.$refs.upload.uploadFiles;
-      if (fileList.length == 0) {
-        this.$notify({
-          message: "上传文件不能为空",
-          type: "error",
-        });
-        return false;
-      }
-      if (fileList.length > 1) {
-        this.$notify({
-          message: "每次只能上传一个文件",
-          type: "error",
-        });
-        return false;
-      }
-      for (let file of fileList) {
-        if (!file.name.endsWith(".xlsx")) {
-          this.$notify({
-            message: "上传文件必须为xlsx格式",
-            type: "error",
-          });
-          this.initUpload();
-          return false;
-        }
-      }
-      return true;
-    },
-    //清空文件
-    removeFile() {
-      // this.fileList = [];
-      this.$refs.upload.clearFiles();
-    },
-    //下载模板
-    async exportFile() {
-      if (this.tempDownloading) return;
-      this.tempDownloading = true;
-
-      const res = await downloadByApi(() => {
-        return this.$httpWithMsg.get(`${QUESTION_API}/course/importTemplate`, {
-          responseType: "blob",
-        });
-      }).catch((e) => {
-        this.$message.error(e || "下载失败,请重新尝试!");
-      });
-      this.tempDownloading = false;
-
-      if (!res) return;
-      this.$message.success("下载成功!");
-    },
   },
 };
 </script>

+ 16 - 158
src/modules/questions/views/CourseProperty.vue

@@ -201,70 +201,15 @@
         >
       </div>
     </el-dialog>
+
     <!-- 导入弹窗 -->
-    <el-dialog
-      title="导入课程"
-      width="520px"
-      :visible.sync="impDialog"
-      :modal="false"
-      append-to-body
-      custom-class="side-dialog"
-    >
-      <el-form>
-        <el-form-item style="margin-left: 20px">
-          <el-upload
-            ref="upload"
-            class="form_left"
-            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
-            :action="uploadAction"
-            :headers="uploadHeaders"
-            :data="uploadData"
-            :before-upload="beforeUpload"
-            :on-progress="uploadProgress"
-            :on-success="uploadSuccess"
-            :on-error="uploadError"
-            :file-list="fileList"
-            :auto-upload="false"
-            :multiple="false"
-          >
-            <el-button
-              slot="trigger"
-              size="small"
-              type="primary"
-              icon="icon icon-search-white"
-            >
-              选择文件
-            </el-button>
-            <el-button
-              size="small"
-              type="primary"
-              icon="icon icon-save-white"
-              @click="submitUpload"
-            >
-              确认上传
-            </el-button>
-            <el-button
-              size="small"
-              type="primary"
-              icon="icon icon-delete-white"
-              @click="removeFile"
-            >
-              清空文件
-            </el-button>
-            <el-button
-              size="small"
-              type="primary"
-              icon="icon icon-export-white"
-              :loading="tempDownloading"
-              @click="exportFile"
-            >
-              下载模板
-            </el-button>
-            <div slot="tip" class="el-upload__tip">只能上传xlsx文件</div>
-          </el-upload>
-        </el-form-item>
-      </el-form>
-    </el-dialog>
+    <import-file-dialog
+      ref="ImportCourseDialog"
+      dialog-title="导入课程"
+      :template-url="courseTemplateUrl"
+      :upload-url="uploadCourseUrl"
+      @uploaded="uploadSuccess"
+    ></import-file-dialog>
 
     <!-- 导入错误信息列表 -->
     <el-dialog title="错误提示" :visible.sync="errDialog" append-to-body>
@@ -286,8 +231,10 @@
 import { QUESTION_API } from "@/constants/constants";
 import { mapState } from "vuex";
 import { downloadByApi } from "@/plugins/download";
+import ImportFileDialog from "@/components/ImportFileDialog.vue";
 
 export default {
+  components: { ImportFileDialog },
   data() {
     return {
       courseAddSearch: false,
@@ -297,14 +244,10 @@ export default {
         courseId: "",
       },
 
-      impDialog: false,
-      uploadAction: QUESTION_API + "/courseProperty/import",
-      uploadHeaders: {},
-      uploadData: {},
+      uploadCourseUrl: `${QUESTION_API}/courseProperty/import`,
+      courseTemplateUrl: `${QUESTION_API}/courseProperty/importTemplate`,
       errMessages: [],
       errDialog: false,
-      fileLoading: false,
-      fileList: [],
 
       courseList: [],
       courseAddList: [],
@@ -326,7 +269,6 @@ export default {
         ],
       },
       downloading: false,
-      tempDownloading: false,
     };
   },
   computed: {
@@ -401,105 +343,21 @@ export default {
     },
     //导入
     impCourseProperty() {
-      this.impDialog = true;
-      this.initUpload();
-    },
-    initUpload() {
-      this.fileList = [];
-    },
-    beforeUpload(file) {
-      console.log(file);
-    },
-    uploadProgress() {
-      console.log("uploadProgress");
+      this.$refs.ImportCourseDialog.open();
     },
     uploadSuccess(response) {
-      if (!response.hasError) {
+      const { hasError, failRecords } = response.data;
+      if (!hasError) {
         this.$notify({
           message: "上传成功",
           type: "success",
         });
-        this.fileLoading = false;
-        this.impDialog = false;
         this.searchCourProperty();
       } else {
-        this.fileLoading = false;
-        this.impDialog = false;
-        this.errMessages = response.failRecords;
+        this.errMessages = failRecords;
         this.errDialog = true;
       }
     },
-    uploadError(response) {
-      var json = JSON.parse(response.message);
-      if (response.status == 500) {
-        this.$notify({
-          message: json.desc,
-          type: "error",
-        });
-      }
-      this.fileLoading = false;
-    },
-    //确定上传
-    submitUpload() {
-      if (!this.checkUpload()) {
-        return false;
-      }
-      this.$refs.upload.submit();
-      this.fileLoading = true;
-    },
-    checkUpload() {
-      var fileList = this.$refs.upload.uploadFiles;
-      if (fileList.length == 0) {
-        this.$notify({
-          message: "上传文件不能为空",
-          type: "error",
-        });
-        return false;
-      }
-      if (fileList.length > 1) {
-        this.$notify({
-          message: "每次只能上传一个文件",
-          type: "error",
-        });
-        return false;
-      }
-      for (let file of fileList) {
-        if (!file.name.endsWith(".xlsx")) {
-          this.$notify({
-            message: "上传文件必须为xlsx格式",
-            type: "error",
-          });
-          this.initUpload();
-          return false;
-        }
-      }
-      return true;
-    },
-    //清空文件
-    removeFile() {
-      // this.fileList = [];
-      this.$refs.upload.clearFiles();
-    },
-    //下载模板
-    async exportFile() {
-      if (this.tempDownloading) return;
-      this.tempDownloading = true;
-
-      const res = await downloadByApi(() => {
-        return this.$httpWithMsg.get(
-          `${QUESTION_API}/courseProperty/importTemplate`,
-          {
-            responseType: "blob",
-          }
-        );
-      }).catch((e) => {
-        this.$message.error(e || "下载失败,请重新尝试!");
-      });
-      this.tempDownloading = false;
-
-      if (!res) return;
-      this.$message.success("下载成功!");
-    },
     //查询所有课程属性
     searchFrom() {
       this.currentPage = 1;

+ 12 - 8
src/modules/questions/views/ImportPaperInfo.vue

@@ -52,14 +52,18 @@
             class="form_width"
           ></el-input>
         </el-form-item>
-        <!--
-              <el-row>
-                  <el-form-item label="相同大题名称" label-width="120px" class="pull-left">
-                      <el-radio  v-model="formSearch.sameName"  label="1">合并</el-radio>
-                      <el-radio  v-model="formSearch.sameName"  label="0">不合并</el-radio>
-                  </el-form-item>
-              </el-row>
-            -->
+
+        <!-- <el-row>
+          <el-form-item
+            label="相同大题名称"
+            label-width="120px"
+            class="pull-left"
+          >
+            <el-radio v-model="formSearch.sameName" label="1">合并</el-radio>
+            <el-radio v-model="formSearch.sameName" label="0">不合并</el-radio>
+          </el-form-item>
+        </el-row> -->
+
         <el-form-item class="padding-tb-20">
           <el-upload
             ref="upload"

+ 20 - 43
src/modules/questions/views/License.vue

@@ -58,28 +58,19 @@
           <el-input v-model="form.accessSecret" maxlength="255" />
         </el-form-item>
         <el-form-item v-show="form.type == 'OFFLINE'" label="导入授权文件">
-          <el-upload
-            ref="upload"
-            class="form_left"
+          <upload-button
+            ref="UploadButton"
             accept=".lic"
-            :action="uploadAction"
-            :headers="uploadHeaders"
-            :data="uploadData"
-            :on-success="uploadSuccess"
-            :on-error="uploadError"
-            :file-list="fileList"
+            :format="['lic']"
+            :upload-url="uploadAction"
             :auto-upload="false"
-            :multiple="false"
+            btn-content="选择文件"
+            btn-type="primary"
+            @upload-success="uploadSuccess"
+            @upload-error="uploadError"
+            @valid-error="validError"
           >
-            <el-button
-              slot="trigger"
-              size="small"
-              type="primary"
-              icon="icon icon-search-white"
-            >
-              选择文件
-            </el-button>
-          </el-upload>
+          </upload-button>
         </el-form-item>
         <el-form-item label=" ">
           <el-button
@@ -148,8 +139,10 @@
 import { QUESTION_API } from "@/constants/constants";
 import { mapState } from "vuex";
 import { downloadByApi } from "@/plugins/download";
+import UploadButton from "@/components/UploadButton.vue";
 
 export default {
+  components: { UploadButton },
   data() {
     return {
       uploadAction: QUESTION_API + "/system/auth/offline",
@@ -191,6 +184,7 @@ export default {
           LOGIN_UD_CHECK_ONECE: "",
         },
       },
+      tempDownloading: false,
     };
   },
   computed: {
@@ -293,25 +287,14 @@ export default {
       this.$message.success("下载成功!");
     },
     submitUpload() {
-      var fileList = this.$refs.upload.uploadFiles;
-      if (fileList.length == 0) {
-        this.$notify({
-          message: "请选择授权文件",
-          type: "error",
-        });
-        return false;
-      }
-      if (fileList.length > 1) {
-        this.$notify({
-          message: "每次只能上传一个文件",
-          type: "error",
-        });
-        return false;
-      }
-      this.$refs.upload.submit();
+      this.$refs.UploadButton.submit();
+    },
+    validError(error) {
+      console.log(error);
+      this.$message.error(error.message);
     },
     uploadSuccess(response) {
-      if (!response.hasError) {
+      if (!response.data.hasError) {
         this.$notify({
           message: "授权成功",
           type: "success",
@@ -320,13 +303,7 @@ export default {
       }
     },
     uploadError(response) {
-      var json = JSON.parse(response.message);
-      if (response.status == 500) {
-        this.$notify({
-          message: json.desc,
-          type: "error",
-        });
-      }
+      this.$message.error(response.data.message || "上传错误");
     },
   },
 };

+ 19 - 155
src/modules/questions/views/user.vue

@@ -115,7 +115,7 @@
             type="primary"
             plain
             icon="icon icon-import"
-            @click="impUser"
+            @click="importUser"
             >导入
           </el-button>
         </div>
@@ -466,70 +466,15 @@
         >
       </div>
     </el-dialog>
+
     <!-- 导入弹窗 -->
-    <el-dialog
-      title="导入用户"
-      width="520px"
-      :visible.sync="impDialog"
-      :modal="false"
-      :close-on-click-modal="false"
-      :close-on-press-escape="false"
-      append-to-body
-      custom-class="side-dialog"
-    >
-      <el-form>
-        <el-form-item style="margin-left: 20px">
-          <el-upload
-            ref="upload"
-            class="form_left"
-            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
-            :action="uploadAction"
-            :headers="uploadHeaders"
-            :data="uploadData"
-            :on-success="uploadSuccess"
-            :on-error="uploadError"
-            :file-list="fileList"
-            :auto-upload="false"
-            :multiple="false"
-          >
-            <el-button
-              slot="trigger"
-              size="small"
-              type="primary"
-              icon="icon icon-search-white"
-            >
-              选择文件
-            </el-button>
-            <el-button
-              size="small"
-              type="primary"
-              icon="icon icon-save-white"
-              @click="submitUpload"
-            >
-              确认上传
-            </el-button>
-            <el-button
-              size="small"
-              type="primary"
-              icon="icon icon-delete-white"
-              @click="removeFile"
-            >
-              清空文件
-            </el-button>
-            <el-button
-              size="small"
-              type="primary"
-              icon="icon icon-export-white"
-              :loading="tempDownloading"
-              @click="exportFile"
-            >
-              下载模板
-            </el-button>
-            <div slot="tip" class="el-upload__tip">只能上传xlsx文件</div>
-          </el-upload>
-        </el-form-item>
-      </el-form>
-    </el-dialog>
+    <import-file-dialog
+      ref="ImportUserDialog"
+      dialog-title="导入用户"
+      :template-url="userTemplateUrl"
+      :upload-url="uploadUserUrl"
+      @uploaded="uploadSuccess"
+    ></import-file-dialog>
 
     <!-- 导入错误信息列表 -->
     <el-dialog title="错误提示" :visible.sync="errDialog" append-to-body>
@@ -550,10 +495,11 @@
 <script>
 import { QUESTION_API } from "@/constants/constants.js";
 import { mapState } from "vuex";
-import { downloadByApi } from "@/plugins/download";
+import ImportFileDialog from "@/components/ImportFileDialog.vue";
 
 export default {
   name: "User",
+  components: { ImportFileDialog },
   data() {
     var validateRootOrg = (rule, value, callback) => {
       if (0 != value && !value) {
@@ -570,14 +516,10 @@ export default {
       }
     };
     return {
-      impDialog: false,
-      uploadAction: QUESTION_API + "/user/import",
-      uploadHeaders: {},
-      uploadData: {},
+      uploadUserUrl: `${QUESTION_API}/user/import`,
+      userTemplateUrl: `${QUESTION_API}/user/importTemplate`,
       errMessages: [],
       errDialog: false,
-      fileLoading: false,
-      fileList: [],
 
       courseList4Search: [],
       courseLoading4Search: false,
@@ -749,10 +691,6 @@ export default {
   created() {
     this.initPrivileges();
     this.init();
-    this.uploadHeaders = {
-      key: this.user.key,
-      token: this.user.token,
-    };
   },
   methods: {
     roleDis(code) {
@@ -762,97 +700,23 @@ export default {
         return false;
       }
     },
-    impUser() {
-      this.impDialog = true;
-      this.initUpload();
-    },
-    initUpload() {
-      this.fileList = [];
+    //导入
+    importUser() {
+      this.$refs.ImportUserDialog.open();
     },
     uploadSuccess(response) {
-      if (!response.hasError) {
+      const { hasError, failRecords } = response.data;
+      if (!hasError) {
         this.$notify({
           message: "上传成功",
           type: "success",
         });
-        this.fileLoading = false;
-        this.impDialog = false;
         this.searchForm();
       } else {
-        this.fileLoading = false;
-        this.impDialog = false;
-        this.errMessages = response.failRecords;
+        this.errMessages = failRecords;
         this.errDialog = true;
       }
     },
-    uploadError(response) {
-      var json = JSON.parse(response.message);
-      if (response.status == 500) {
-        this.$notify({
-          message: json.desc,
-          type: "error",
-        });
-      }
-      this.fileLoading = false;
-    },
-    //确定上传
-    submitUpload() {
-      if (!this.checkUpload()) {
-        return false;
-      }
-      this.$refs.upload.submit();
-      this.fileLoading = true;
-    },
-    checkUpload() {
-      var fileList = this.$refs.upload.uploadFiles;
-      if (fileList.length == 0) {
-        this.$notify({
-          message: "上传文件不能为空",
-          type: "error",
-        });
-        return false;
-      }
-      if (fileList.length > 1) {
-        this.$notify({
-          message: "每次只能上传一个文件",
-          type: "error",
-        });
-        return false;
-      }
-      for (let file of fileList) {
-        if (!file.name.endsWith(".xlsx")) {
-          this.$notify({
-            message: "上传文件必须为xlsx格式",
-            type: "error",
-          });
-          this.initUpload();
-          return false;
-        }
-      }
-      return true;
-    },
-    //清空文件
-    removeFile() {
-      // this.fileList = [];
-      this.$refs.upload.clearFiles();
-    },
-    //下载模板
-    async exportFile() {
-      if (this.tempDownloading) return;
-      this.tempDownloading = true;
-
-      const res = await downloadByApi(() => {
-        return this.$httpWithMsg.get(`${QUESTION_API}/user/importTemplate`, {
-          responseType: "blob",
-        });
-      }).catch((e) => {
-        this.$message.error(e || "下载失败,请重新尝试!");
-      });
-      this.tempDownloading = false;
-
-      if (!res) return;
-      this.$message.success("下载成功!");
-    },
     getCourses4Search(query) {
       this.courseLoading4Search = true;
       this.$httpWithMsg

+ 1 - 1
src/plugins/download.js

@@ -31,7 +31,7 @@ export async function downloadByApi(fetchFunc, fileName) {
       const res = await blobToText(errorInfo.response.data).catch(() => {});
       if (!res) return Promise.reject("下载失败!");
       const resJson = JSON.parse(res);
-      return Promise.reject(resJson.desc);
+      return Promise.reject(resJson.message);
     } else {
       return Promise.reject("下载失败!");
     }