import.vue 6.22 KB
<template>
  <div class="imort_page">

    <div class="choose_box">
      <div class="upload_wrap">
        <el-input placeholder="请选择需要导入的车辆表格" v-model="uploadFile.name" readonly>
          <el-button slot="append">选择文件</el-button>
        </el-input>
        <!-- 隐藏上传文件按钮 开始 -->
        <el-upload ref="uploadBox" class="upload_box" :auto-upload="false" :headers="uploadHead" :accept="acceptFile" :data="uploadParams" :action="uploadPath" multiple :show-file-list="false" :on-change="onChange" :on-success="onSuccess" :before-upload="beforeUpload" :on-error="onError" :file-list="fileList">
          <el-button type="primary">点击上传</el-button>
        </el-upload>
        <!-- 隐藏上传文件按钮 end -->
      </div>
      <el-link type="primary" class="el_link" @click="downTemplate">下载模版</el-link>
    </div>

    <!-- <div class="btn_group">
      <el-button type="default" size="small">取消</el-button>
      <el-button type="primary" size="small">确定</el-button>
    </div> -->

    <!-- 导入结果 开始 -->
    <div class="upload_result" v-if="uploadResult && (uploadResult.total || uploadResult.total == 0)">
      <h2 class="title">导入结果</h2>
      <div class="result_box">
        <div class="cell">
          <div class="name">
            <span>成功记录</span>
          </div>
          <span class="txt">{{uploadResult.succNum||0}}</span>
        </div>
        <div class="cell">
          <div class="name">
            <span>失败记录</span>
            <span class="btn" v-if="uploadResult.failFile" @click="downloadFail()">导出记录</span>
          </div>
          <span class="txt">{{uploadResult.failNum||0}}</span>
        </div>
      </div>
    </div>
    <!-- 导入结果 end -->

  </div>
</template>
<script>
import {
  IMPORT_VEHICLE_TEMPLATE,
  DOWNLOAD_ASSIGNPERMIT_TEMPLATE_FILE,
} from "@/services/user";
import { showToast, broadcast } from "@/utils/util";
export default {
  name: "ImportVehicle",
  props: {
    id: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      fileInfo: "",
      uploadPath: "",
      uploadHead: {},
      uploadFile: {},
      uploadParams: {},
      uploadResult: {},
      acceptFile: ".xlsx,.xls",
      fileList: [],
    };
  },
  mounted() {
    let id = this.id;
    let token = localStorage.getItem("token");
    this.uploadPath =
      this.$root.parentVuex.getters.baseURL + IMPORT_VEHICLE_TEMPLATE;
    this.uploadHead = { Authorization: token };
    this.uploadParams = { receiverId: id };
  },
  methods: {
    // 监听文件选择
    onChange(file, fileList) {
      console.log(file);
      console.log(fileList);
      if (!file.response) {
        this.uploadFile = file;
        this.fileList = [file];
      }
    },

    // 下载失败文件
    downloadFail() {
      let o = this.uploadResult;
      window.open(o.failFile);
    },

    // 文件上传前
    beforeUpload() {
      this.uploadResult = {};
    },

    // 文件上传失败
    onError(err, file, fileList) {
      console.log(err);
    },

    // 监听文件上传成功
    onSuccess(response, file, fileList) {
      console.log(response);
      let _this = this;
      broadcast("cancel-loading");
      _this.$refs.uploadBox.clearFiles();
      if (response.code != 200) {
        showToast({
          message: response.message,
        });
        _this.uploadFile = {};
        _this.fileList = [];
        return false;
      }
      this.uploadResult = response.data;
      broadcast("import-success");
    },

    // 上传文档
    saveInfo() {
      let o = this.uploadFile;
      console.log(this.fileList);
      console.log(o);
      if (!o || !o.name) {
        showToast({
          message: "请选择文件",
        });
        console.log("7777777777777");
        broadcast("cancel-loading");
        return false;
      }

      let list = o.name.split('.')
      let acceptFile = this.acceptFile;
      let _type = list[list.length-1];
      let idx = acceptFile.includes(_type)
      if(!idx){
        showToast({
          message: `导入文件只支持${acceptFile}`,
        });
        broadcast("cancel-loading");
        return false;
      }
      this.$refs.uploadBox.submit();
    },

    // 下载模板
    downTemplate() {
      DOWNLOAD_ASSIGNPERMIT_TEMPLATE_FILE().then((res) => {
        let { data } = res;
        window.open(data.dictValue);
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.choose_box {
  display: flex;
  align-items: center;
  padding: 10px 72px;
  .el_link {
    flex-shrink: 0;
    text-align: left;
    margin: 0 10px;
  }
  .upload_wrap {
    flex: 1;
    position: relative;
    .upload_box {
      position: absolute;
      right: 0;
      bottom: 0;
      top: 0;
      opacity: 0;
    }
  }
}
.btn_group {
  padding: 20px 0 10px 0;
  text-align: center;
}
.upload_result {
  border-top: 1px solid #eee;
  margin-top: 30px;
  .title {
    font-family: PingFangSC-Medium;
    font-size: 20px;
    color: #333333;
    font-weight: 500;
  }
  .result_box {
    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 30px;
    .cell {
      width: 248px;
      height: 108px;
      background: #fbfbfb;
      border: 1px solid rgba(215, 215, 215, 1);
      border-radius: 4px;
      margin: 0 20px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding: 20px;
      box-sizing: border-box;
      .name {
        font-family: PingFangSC-Medium;
        font-size: 14px;
        color: #666666;
        font-weight: 500;
        display: flex;
        align-items: center;
        .btn {
          width: 76px;
          height: 30px;
          border: 1px solid rgba(33, 150, 243, 1);
          border-radius: 4px;
          text-align: center;
          line-height: 28px;
          font-family: PingFangSC-Regular;
          font-size: 14px;
          color: #409eff;
          font-weight: 400;
          cursor: pointer;
          margin-left: 10px;
        }
        .btn:hover {
          opacity: 0.8;
        }
      }
      .txt {
        font-family: PingFangSC-Medium;
        font-size: 30px;
        color: #333333;
        font-weight: 500;
        line-height: 1.5;
      }
    }
  }
}
</style>