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

    <el-form ref="form" :model="form" :rules="rules" :inline="false" size="mini" v-show="!showResult">
      <el-form-item label="选择导入设备名称" prop="deviceHardwareId">
        <device-select @onChange="(value)=>form.deviceHardwareId=value"></device-select>
      </el-form-item>
      <el-form-item label="" prop="filename">
        <el-upload ref="importUpload" class="upload-wrap" :file-list="importFileList" :action="IMPORTDEVICEMANAGE" :on-change="changeFile" :before-upload="beforeUpload" :multiple="false" :show-file-list="false" :on-success="successUpload" :on-error="errorUpload" :data="data" :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-item>
    </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" :disabled="disabled">确认</el-button>
        <el-button @click="$emit('onImportModalChange', false)" size="small">关闭</el-button>
      </div>

      <div v-show="showResult">
        <el-button v-show="errorNum" type="primary" @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 deviceSelect from "./DeviceSelect.vue";
import {BASEURL} from '@/api/config'
import {IMPORTDEVICEMANAGE} from '@/api/services/upload'

export default {
  props: ["importModalVisible", "sourceFrom"],
  data() {
    let checkId = (rule, vaule, cb) => {
      if (this.form.deviceHardwareId === "") {
        cb("");
      }
      cb();
    };
    let checkFilename = (rule, vaule, cb) => {
      if (this.filename === "") {
        cb("");
      }
      cb();
    };
    return {
      form: {
        deviceHardwareId: "",
      },
      showResult: false,
      successNum: "",
      errorNum: "",
      seqNo: "",
      importing: false,
      filename: "",
      data: {
        device2AddressStatus: this.sourceFrom,
      },
      rules: {
        deviceHardwareId: [{ validator: checkId, trigger: "blur" }],
        filename: [{ validator: checkFilename, trigger: "change" }],
      },
      BASEURL:BASEURL,
      IMPORTDEVICEMANAGE:IMPORTDEVICEMANAGE,
      importFileList:[]
    };
  },
  computed: {
    disabled() {
      return this.form.deviceHardwareId === "" || this.filename === "";
    },
  },
  methods: {
    doneResult() {
      this.$emit("onImportModalChange", false);
      this.$emit("onRefreshList");
      this.showResult = false;
      this.successNum = "";
      this.errorNum = "";
      this.seqNo = "";
      this.importing = false;
      this.filename = "";
      //this.form.deviceHardwareId = ''
    },
    exportErrorResult() {
      exportExcel("device/deviceManage/exportFailDeviceManage", {
        seqNo: this.seqNo,
      });
    },
    successUpload(res) {
      this.importing = false;
      if (typeof res === "object") {
        if (res.returnCode == 0) {
          const data = res.data;
          this.successNum = data.success;
          this.errorNum = parseInt(data.fail);
          this.seqNo = data.seqNo;

          this.$refs.importUpload.clearFiles();
          this.showResult = true;
        } else {
          this.$message({
            message: res.message,
            type: "error",
          });
        }
      } else {
        window.location.href = "/";
      }
    },
    errorUpload(err) {
      console.log(err);
      //window.location.href = "/";
    },
    submitUpload() {
      let _this = this;
      _this.$refs.form.validate((valid) => {
        if (valid) {
          _this.data.deviceHardwareId = _this.form.deviceHardwareId;
          _this.$refs.importUpload.submit();
        }
      });
    },
    changeFile(file, filelist) {
      this.filename = file.name;
      console.log(filelist)
    },
    beforeUpload(file) {
      if (file.name == this.filename) {
        this.importing = true;
        return true;
      }

      return false;
    },
    downloadTpl() {
      exportExcel("device/deviceManage/downLoadDeviceManage");
    },
    openDialog() {
      this.$emit("onImportModalChange", true);
    },
    closeDialog() {
      this.$emit("onImportModalChange", false);
    },
  },
  components: {
    deviceSelect,
  },
};
</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>