step-second.vue 9.23 KB
<template>
  <div class="import-main">
    <div class="upload-process">
      <div class="upload-process-item" v-for="(item, index) in process" :key="item">
        <div class="process-item-label" :style="{ width: textNum * 20 + 'px' }">
          <label>*</label>
          {{ item.templateName }}:
        </div>
        <div class="process-item-info">
          <div class="process-item-size">
            <img src="@/assets/img/Excel.png" alt="" />
            文件大小:{{ item.size }}M
          </div>
          <div class="process-item-name">{{ item.fileName }}</div>
          <div class="process-item-num">
            <el-progress :show-text="false" :stroke-width="6" :percentage="item.percentage" :color="item.percentage != 100 ? '#5850FF' : '#2CCA74'"></el-progress>
            <div class="process-num-status" v-if="item.percentage > 0 && item.percentage < 100">上传中</div>
            <div class="process-num-status process-refresh" v-if="item.percentage == 0 && !dialogVisible" @click="refresh(index)">
              重试
              <i class="el-icon-refresh-right"></i>
            </div>
            <div class="process-num-status process-refresh" v-if="item.percentage == 0 && dialogVisible">
              等待上传
            </div>
            <div class="process-num-status" v-if="item.percentage == 100"><i class="el-icon-success"></i></div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose" top="30vh">
      <div class="dialog-body">
        <div class="dialog-tips-icon"><i class="el-icon-warning"></i></div>
        <div class="dialog-tips-text">点击继续将开始上传数据文件</div>
        <div class="dialog-tips-text">
          <span>期间请勿关闭页面</span>
          ,是否继续吗?
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleCancel">
          取 消
        </el-button>
        <el-button size="small" type="primary" @click="handleSure">确 定</el-button>
      </div>
    </el-dialog>
    <div class="loading-containner" v-if="processLoading"></div>
  </div>
</template>

<script>
import { utils } from 'glsx-common'
export default {
  name: 'step-second',
  props: {},
  data() {
    return {
      textNum: 0,
      processLoading: false,
      resultNum: 0,
      dialogVisible: false,
      setInter: [],
      process: [{ percentage: 0 }, { percentage: 0 }, { percentage: 0 }],
      uploadItem: ''
    }
  },
  mounted() {},
  beforeDestroy() {
    this.setInter.forEach(item => {
      item && clearInterval(item)
    })
  },
  methods: {
    handleSure: utils.debounce(function() {
      this.fileUpload()
      this.dialogVisible = false
    }, 300),
    handleCancel() {
      this.$emit('excelCancel')
      this.dialogVisible = false
    },
    handleClose() {
      this.handleCancel()
    },
    startUpload(info) {
      this.uploadItem = info
      const fileList = info.fileList
      this.process = fileList.map(p => {
        this.textNum = this.textNum > p.templateName.length ? this.textNum : p.templateName.length + 1
        return {
          percentage: 0,
          fileName: p.fileName,
          templateName: p.templateName,
          size: p.size.toFixed(2)
        }
      })
      console.log(info)
      this.dialogVisible = true
    },
    async refresh(index) {
      this.processLoading = true
      this.process[index].percentage = 0
      this.setInter[index] = setInterval(this.addProcess(index), 300)
      try {
        this.uploadItem.fileList[index].fileUrl = await this.$apis.common.pictureUpload({ file: this.uploadItem.fileList[index].fileInfo })
        this.resultNum++
        this.process[index].percentage = 100
        clearInterval(this.setInter[index])
        if (this.resultNum == this.process.length) {
          this.datacubeUpload()
        }
      } catch (e) {
        this.processLoading = false
        this.process[index].percentage = 0
        clearInterval(this.setInter[index])
      }
    },
    datacubeUpload() {
      this.processLoading = false
      const fileList = this.uploadItem.fileList.map(item => {
        return {
          templateName: item.templateName,
          fileName: item.fileName,
          fileUrl: item.fileUrl,
          templateUrl: item.templateUrl,
          BusinessTypeId: item.BusinessTypeId,
          businessTypeName: item.businessTypeName
        }
      })
      let params = { fileList, createdStaffId: this.$globalState.session.id, remarks: this.uploadItem.remarks, staffIdList: this.uploadItem.staffIdList }
      this.$apis.square.datacubeUpload(params).then(res => {
        if (res) {
          this.$emit('handleSuccess')
        }
      })
    },
    fileUpload() {
      this.process.forEach(p => {
        p.percentage = Math.random() * 30
      })
      this.resultNum = 0
      this.processLoading = true
      this.uploadItem.fileList.forEach(async (item, index) => {
        this.$set(this.setInter, index, setInterval(this.addProcess(index), 300))
        try {
          this.uploadItem.fileList[index].fileUrl = await this.$apis.common.pictureUpload({ file: item.fileInfo })
          this.process[index].percentage = 100
          this.resultNum++
          clearInterval(this.setInter[index])
          if (this.resultNum == this.process.length) {
            this.datacubeUpload()
          }
        } catch (e) {
          this.processLoading = false
          this.process[index].percentage = 0
          clearInterval(this.setInter[index])
        }

        //         setTimeout(() => {
        //           clearInterval(this.setInter)
        //           this.$emit('handleSuccess')
        //         }, 1000)
        //       }
        // this.$apis.common
        //   .pictureUpload({ file: item.fileInfo })
        //   .then(res => {
        //     resultNum++
        //     item.fileUrl = res
        //     this.process[index].percentage = 100
        //   })
        //   .catch(() => {
        //     this.process[index].percentage = 0
        //   })
      })

      // this.uploadItem.fileList = this.uploadItem.fileList.filter(item => item.fileUrl)
      // this.uploadItem.createdStaffId = this.$globalState.session.id
      // this.$apis.square
      //   .datacubeUpload(this.uploadItem)
      //   .then(res => {
      //     if (res) {
      //       const result = res.failFile
      //       let resultNum = 0
      //       result.forEach((item, index) => {
      //         if (item.uploadSuccess == 0) {
      //           this.process[index].percentage = 100
      //           resultNum++
      //         } else {
      //           this.process[index].percentage = 0
      //         }
      //       })
      //       if (resultNum == result.length) {
      //         setTimeout(() => {
      //           clearInterval(this.setInter)
      //           this.$emit('handleSuccess')
      //         }, 1000)
      //       }
      //     }
      //   })
      //   .catch(() => {
      //     this.process.forEach(p => {
      //       p.percentage = 0
      //     })
      //   })
    },
    addProcess(index) {
      this.process[index].percentage = this.process[index].percentage > 90 ? this.process[index].percentage : this.process[index].percentage + Math.random() * 5
    }
  }
}
</script>

<style lang="scss" scoped>
.loading-containner {
  top: 0px;
  left: 0px;
  position: fixed;
  width: 100%;
  background: rgba(255, 255, 255, 0.1);
  z-index: 5000;
  height: 100%;
}
.import-main {
  margin: 60px auto 0px;
  width: 92%;
  .upload-process {
    &-item {
      display: flex;
      margin-bottom: 20px;
      width: 40vw;
      .process-item-label {
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        min-width: 120px;
        line-height: 20px;
        text-align: right;
        label {
          color: #eb4e4e;
        }
      }
      .process-item-info {
        flex: 1;
      }
      .process-item-size {
        font-size: 14px;
        font-weight: 400;
        color: #999999;
        line-height: 22px;
        img {
          width: 24px;
        }
      }
      .process-item-name {
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        line-height: 22px;
        margin-top: 6px;
      }
      .process-item-num {
        margin-top: 8px;
        display: flex;
        align-items: center;
        font-size: 14px;
        font-weight: 400;
        color: rgba(0, 0, 0, 0.9);
        line-height: 22px;
        /deep/ .el-progress {
          flex: 1;
          margin-right: 6px;
        }
        .process-num-status {
          width: 60px;
          &.process-refresh {
            cursor: pointer;
          }
          i {
            color: #2cca74;
            font-size: 14px;
            &.el-icon-refresh-right {
              color: #8c8c8c;
            }
          }
        }
      }
    }
  }
  .dialog-body {
    text-align: center;
    margin: 10px 0px;
    .dialog-tips-icon {
      margin-bottom: 12px;
      i {
        color: #5850ff;
        font-size: 32px;
      }
    }
    .dialog-tips-text {
      font-size: 16px;
      font-weight: 400;
      color: #333333;
      line-height: 22px;
      span {
        color: #eb4e4e;
      }
    }
  }
  .dialog-footer {
    text-align: center;
  }
}
</style>