userListImport.vue 5.65 KB
<template>
  <!-- 批量导入弹框 -->
  <el-dialog class="import-dialog" title="批量导入" :visible="userImportModalVisible" @open="openDialog" @close="closeDialog" append-to-body width="600px" :close-on-click-modal="false">

    <el-form ref="form" :model="form" :inline="false" size="mini" v-show="!showResult">
      <el-upload ref="upload" class="upload-wrap" :action="IMPORT_ELECFENCE_LINKEDDEVICES" :data="uploadParams" :headers="uploadHeaders" :on-change="changeFile" :before-upload="beforeUpload" :multiple="false" :show-file-list="false" :on-success="successUpload" :on-error="errorUpload" :auto-upload="false">

        <el-input v-model="filename" placeholder="未选取任何文件" class="input-with-select" size="small">
          <el-button slot="append">浏览</el-button>
        </el-input>

        <div slot="tip" class="el-upload__tip">请选择需要导入的工单表格</div>
      </el-upload>
    </el-form>

    <el-row v-show="showResult" class="result-row">
      <el-col :span="12" class="text-center">
        <p><i class="el-icon-success el-alert--success"></i></p>
        <p><strong>{{successNum}}</strong><br>成功记录</p>
      </el-col>
      <el-col :span="12" class="text-center">
        <p><i class="el-icon-error el-alert--error"></i></p>
        <p><strong>{{errorNum}}</strong><br>失败记录</p>
      </el-col>
    </el-row>

    <div slot="footer" class="dialog-footer">
      <div v-show="!showResult" class="success">
        <el-button type="primary" @click="downloadTpl" size="small" class="pull-left">下载模板</el-button>
        <el-button type="primary" @click="submitUpload" size="small" :loading="importing">提交</el-button>
        <el-button @click="$emit('onUserImportModalChange', false)" size="small">关闭</el-button>
      </div>

      <div v-show="showResult">
        <el-button :type="errorNum!=0?'primary':'info'" @click="exportErrorResult" size="small" class="pull-left">导出失败结果</el-button>
        <el-button @click="doneResult" size="small" type="primary">完成</el-button>
      </div>
    </div>
  </el-dialog>

</template>
<script>
import { exportExcel } from "utils";
import {
  IMPORT_ELECFENCE_LINKEDDEVICES,
  DOWN_ELECFENCE_EXCELTEMPLATE,
} from "@/api/services/fence";
import { downFileByPath } from "@/utils/utils";
export default {
  props: ["userImportModalVisible"],
  data() {
    return {
      form: {},
      showResult: false,
      successNum: "",
      errorNum: "",
      seqNo: "",
      importing: false,
      filename: "",
      flag: [],
      IMPORT_ELECFENCE_LINKEDDEVICES,
      uploadHeaders: {},
      uploadParams: {},
    };
  },
  created() {
    let query = this.$route.query;
    let token = localStorage.getItem("token");
    this.uploadHeaders = { Authorization: token };
    if (query.id) {
      this.uploadParams = { fenceId: query.id };
    }
  },
  methods: {
    doneResult() {
      this.$emit("onUserImportModalChange", false);
      this.$emit("onRefreshList");
      this.showResult = false;
      this.successNum = "";
      this.errorNum = "";
      this.seqNo = "";
      this.importing = false;
      this.filename = "";
    },
    exportErrorResult() {
      if (this.errorNum != 0) {
        exportExcel("ecFence/exportFailSn", { seqNo: this.seqNo });
      } else {
        return false;
      }
    },
    successUpload(res) {
      this.importing = false;
      if (typeof res === "object") {
        if (res.returnCode == 0) {
          const data = res.data;
          let returnDate = [];
          if (this.flag.length != 0) {
            function noRepeat(obj1, obj2) {
              for (var key in obj2) {
                if (obj1[key]) {
                  if (obj1[key].sn != obj2[key].sn) {
                    returnDate.push(obj2[key]);
                  }
                } else {
                  returnDate.push(obj2[key]);
                }
              }
            }
            noRepeat(this.flag, res.data.returnDate);

            this.$emit("exportDate", returnDate);
            this.flag.push(...returnDate);

            //console.log(this.flag)
            this.successNum = returnDate.length;
            this.errorNum = parseInt(data.fail);
            this.seqNo = data.seqNo;
            this.$refs.upload.clearFiles();
            this.showResult = true;
          } else {
            this.flag = res.data.returnDate;
            this.$emit("exportDate", res.data.returnDate);
            this.successNum = data.success;
            this.errorNum = parseInt(data.fail);
            this.seqNo = data.seqNo;

            this.$refs.upload.clearFiles();
            this.showResult = true;
          }
        }
      } else {
        //window.location.href = '../ots/login';
      }
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    changeFile(file, filelist) {
      this.filename = file.name;
    },
    beforeUpload(file) {
      if (file.name == this.filename) {
        this.importing = true;
        return true;
      }

      return false;
    },
    // 批量导入模板下载
    downloadTpl() {
      DOWN_ELECFENCE_EXCELTEMPLATE().then((res) => {
        let obj = res.data;
        if (obj.dictValue) {
          downFileByPath(obj.dictValue);
        }
      });
    },
    openDialog() {
      this.$emit("onUserImportModalChange", true);
    },
    closeDialog() {
      this.$emit("onUserImportModalChange", false);
    },
    errorUpload(err) {
      //window.location.href = '../ots/login';
    },
  },
};
</script>

<style>
.import-dialog .upload-wrap .el-upload {
  width: 100%;
}
.import-dialog .el-icon-success,
.import-dialog .el-icon-error {
  font-size: 30px;
}
.import-dialog .result-row .el-col + .el-col {
  border-left: 1px solid #eee;
}
</style>