import.vue 15.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
<template>
  <div class="squre-main">
    <div class="squre-title">
      数据魔方
      <span>智能分析客户数据,自动人群分类,推荐策略。</span>
    </div>
    <div class="squre-safe-panel scrm-box-f" v-loading="loading" :style="{ 'min-height': boxHeight + 'px' }">
      <div class="dialog-step">
        <div class="el-steps el-steps--horizontal">
          <div class="el-step is-horizontal" style="flex-basis: 50%; margin-right: 0px">
            <div class="el-step__head is-finish">
              <div :class="['el-step__line', activeIndex > 1 ? 'is-finish' : '']" style="margin-right: 0px">
                <i class="el-step__line-inner" style="transition-delay: 0ms; border-width: 0px; width: 0%"></i>
              </div>
              <div :class="['el-step__icon', activeIndex == 1 ? 'is-text' : '']">
                <div class="el-step__icon-inner" v-if="activeIndex > 1"><i class="el-icon-check"></i></div>
                <div class="el-step__icon-inner" v-else>1</div>
              </div>
              <div class="el-step__main el-step__desc">
                <div :class="['el-step__title', activeIndex == 1 ? 'is-finish' : '']">第一步</div>
                <div :class="['el-step__sub', activeIndex == 1 ? 'is-finish' : '']">选择数据文件</div>
              </div>
            </div>
          </div>
          <div class="el-step is-horizontal" style="flex-basis: 50%; margin-right: 0px">
            <div :class="['el-step__head', activeIndex > 1 ? 'is-finish' : '']">
              <div :class="['el-step__line', activeIndex > 2 ? 'is-finish' : '']" style="margin-right: 0px">
                <i class="el-step__line-inner" style="transition-delay: -150ms; border-width: 0px; width: 0%"></i>
              </div>
              <div :class="['el-step__icon', activeIndex == 2 ? 'is-text' : '']">
                <!---->
                <div class="el-step__icon-inner" v-if="activeIndex > 2"><i class="el-icon-check"></i></div>
                <div class="el-step__icon-inner" v-else>2</div>
              </div>
              <div class="el-step__main el-step__desc">
                <div :class="['el-step__title', activeIndex == 2 ? 'is-finish' : '']">第二步</div>
                <div :class="['el-step__sub', activeIndex == 2 ? 'is-finish' : '']">上传数据文件</div>
              </div>
            </div>
          </div>
          <div class="el-step is-horizontal is-flex" style="flex-basis: 50%; max-width: 33.3333%">
            <div :class="['el-step__head', activeIndex > 2 ? 'is-finish' : '']">
              <div class="el-step__line"><i class="el-step__line-inner"></i></div>
              <div :class="['el-step__icon', activeIndex == 3 ? 'is-text' : '']">
                <!---->
                <div class="el-step__icon-inner">3</div>
              </div>
              <div class="el-step__main el-step__desc">
                <div :class="['el-step__title', activeIndex == 3 ? 'is-finish' : '']">第三步</div>
                <div :class="['el-step__sub', activeIndex == 3 ? 'is-finish' : '']">完成</div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="upload-list">
        <!-- <component :is="stepName"></component> -->
        <step-first v-show="stepName == 'stepFirst'" ref="firstForm"></step-first>
        <step-second v-show="stepName == 'stepSecond'" ref="secondForm" @handleSuccess="handleSuccess" @excelCancel="excelCancel"></step-second>
        <step-third v-show="stepName == 'stepThird'" ref="thirdForm"></step-third>
      </div>
    </div>
    <GLFooter>
      <el-button type="primary" @click="handleNext" v-show="activeIndex == 1">下一步</el-button>
      <el-button type="primary" @click="goBack" v-show="activeIndex == 3">返回魔方</el-button>
    </GLFooter>
  </div>
</template>

<script>
import * as XLSX from 'xlsx'
import stepFirst from './components/step-first.vue'
import stepSecond from './components/step-second.vue'
import stepThird from './components/step-third.vue'
export default {
  components: { stepFirst, stepSecond, stepThird },
  name: 'import',
  data() {
    return {
      boxHeight: 0,
      swiperList: ['加密传输', '脱敏传输', '去标识化', '限制访问'],
      autoplay: true,
      loading: false,
      importName: '',
      borderhover: false, //文件拖拖动到区域的hover效果
      imgArr: [],
      fileData: [],
      fileInfo: '',
      activeIndex: 1,
      staffIds: [],
      active: 1
    }
  },
  computed: {
    stepName() {
      let status = ''
      switch (this.activeIndex) {
        case 1:
          status = 'stepFirst'
          break
        case 2:
          status = 'stepSecond'
          break
        case 3:
          status = 'stepThird'
          break
      }
      return status
    }
  },
  methods: {
    goBack() {
      this.$router.push('/square/index')
    },
    // 从网络上读取某个excel文件,url必须同域,否则报错
    readWorkbookFromRemoteFile() {
      var xhr = new XMLHttpRequest()
      xhr.open('get', 'https://img01.glsx.com.cn/upload/biz/魔方导入模板v1_0_2_20210117_1.xlsx', true)
      xhr.responseType = 'arraybuffer'
      xhr.onload = function(e) {
        if (xhr.status == 200) {
          var data = new Uint8Array(xhr.response)
          var workbook = XLSX.read(data, { type: 'array' })
          var tabelHeader = ''
          for (var sheet in workbook.Sheets) {
            if (workbook.Sheets.hasOwnProperty(sheet)) {
              tabelHeader = XLSX.utils.sheet_to_json(workbook.Sheets[sheet])
              // break; // 若是只取第一张表,就取消注释这行
            }
          }
          if (tabelHeader && tabelHeader.length > 0) {
            this.tabelHeaderTwo = Object.keys(tabelHeader[0])
          }
          console.log(this.tabelHeaderTwo)
        }
      }
      xhr.send()
    },
    handleSuccess() {
      this.activeIndex = 3
    },
    excelCancel() {
      this.activeIndex = 1
    },
    changeItem(e) {
      this.active = e + 1
    },
    setActiveItem(index) {
      this.autoplay = false
      this.active = index + 1
      this.$refs.carousel.setActiveItem(index)
    },
    delHandleMouseEnter() {
      this.$refs.carousel.handleMouseEnter = () => {}
    },
    handleNext() {
      let uploadInfo = this.$refs.firstForm.getUploadInfo()
      uploadInfo.fileList = uploadInfo.fileList.filter(item => item.fileInfo)
      if (uploadInfo) {
        this.activeIndex++
        this.$refs.secondForm.startUpload(uploadInfo)
      }
    },
    changeInput(e) {
      this.uploadFile(e.target.files[0])
    },
    readExcel(file) {
      // 解析Excel
      let _this = this
      return new Promise(function(resolve, reject) {
        // 返回Promise对象
        const reader = new FileReader()
        reader.onload = e => {
          // 异步执行
          try {
            // 以二进制流方式读取获得整份excel表格对象
            var data = e.target.result,
              workbook = XLSX.read(data, { type: 'binary' })
          } catch (e) {
            reject(e.message)
          }

          // 表格的表格范围,可用于判断表头是否数量是否正确
          var fromTo = ''
          // 遍历每张表读取
          for (var sheet in workbook.Sheets) {
            let sheetInfos = workbook.Sheets[sheet]
            let locations = [] // A1,B1,C1...
            if (workbook.Sheets.hasOwnProperty(sheet)) {
              fromTo = sheetInfos['!ref'] // A1:B5
              locations = _this.getLocationsKeys(fromTo)
            }

            for (let i = 0; i < locations.length; i++) {
              let value = sheetInfos[locations[i]].v
              if (value != i) {
                // 自定的校验规则,自由实现便可
                // 校验失败reject
                reject(locations[i] + "'s parameter isn't " + i)
              }
            }
            // 校验成功resolve
            resolve(true)
          }
        }
        reader.readAsBinaryString(file)
      })
    },
    getHeaderRow(sheet) {
      const headers = []
      /* sheet['!ref']表示所有单元格的范围,例如从A1到F8则记录为 A1:F8*/
      const range = XLSX.utils.decode_range(sheet['!ref'])
      let C,
        R = range.s.r /* 从第一行开始 */
      /* 按列进行数据遍历 */
      for (C = range.s.c; C <= range.e.c; ++C) {
        /* 查找第一行中的单元格 */
        const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]

        let hdr = 'UNKNOWN ' + C // <-- 进行默认值设置
        if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)

        headers.push(hdr)
      }
      return headers
    },
    uploadFile(file) {
      const reader = new FileReader()
      reader.onload = e => {
        /* 解析数据 */
        const bstr = e.target.result
        const wb = XLSX.read(bstr, { type: 'binary' })
        /* 获取文件的第一个工作表(WorkSheet) */
        const wsname = wb.SheetNames[0]
        const ws = wb.Sheets[wsname]
        /* 数组转换 */
        const data = XLSX.utils.sheet_to_json(ws, { header: 1 })
        /* 进行表格数据更新 */
        if (data && data.length > 0) {
          this.tabelHeader = data[0]
        }
        /* 进行表格表头数据更新 */
        this.getHeaderRow(ws)
      }
      reader.readAsBinaryString(file)
      // let AllImgExt = '.xls|.xlsx'
      // let extName = e.name.substring(e.name.lastIndexOf('.')).toLowerCase()
      // console.log(extName)
      // if (AllImgExt.indexOf(extName) === -1) {
      //   this.$message.error('上传格式不正确')
      //   return false
      // }
      // const size = e.size / 1024 / 1024
      // if (size < 9) {
      //   // // 转码base64
      //   const reader = new FileReader()
      //   reader.readAsDataURL(e)
      //   reader.onload = event => {
      //     const imgFile = event.target.result
      //     const arrBase64 = imgFile.split(',')
      //     const fileInfo = e.name.split('.')[1] + '@' + arrBase64[1]
      //     this.$apis.common.pictureUpload({ file: fileInfo }).then(res => {
      //       this.fileInfo = res
      //       this.importName = e.name
      //       this.$refs.file.value = ''
      //     })
      //   }
      // } else {
      //   this.$message.error('上传文件不可超过9M')
      // }
    },
    handleSubmit() {
      if (!this.fileInfo) {
        this.$message.error('请上传文件')
        return
      }
      this.loading = true
      this.$apis.square.importsUpload({ fileUrl: this.fileInfo }).then(res => {
        this.loading = false
        if (res) {
          this.$store.dispatch('square/setImport', res)
          this.$router.push('/square/importResult/' + res.id)
        }
      })
    }
  },
  mounted() {
    let clientHeight = document.documentElement.clientHeight
    this.boxHeight = clientHeight - 280
    // this.readWorkbookFromRemoteFile()
  }
}
</script>

<style lang="scss" scoped>
.squre-title {
  font-size: 20px;
  font-weight: 600;
  color: #333333;
  line-height: 20px;
  letter-spacing: 1px;
  span {
    margin-left: 4px;
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    line-height: 14px;
  }
}
.dialog-step {
  width: 80%;
  margin: 0 auto 80px;
}

.el-steps {
  transform: translateX(-30px);
}
.el-step.is-vertical .el-step__head {
  width: 32px;
}
.el-step__line {
  background: rgba(0, 0, 0, 0.15);
}
.el-step__icon {
  width: 32px;
  height: 32px;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 50%;
}
.el-step.is-horizontal .el-step__line {
  top: 14px;
  left: 120px;
  right: 4%;
}
.el-step__icon-inner {
  color: #c6c6c6;
}
.el-step__head.is-finish {
  color: #ffffff;
  border-color: #5850ff;
}
.el-step__desc {
  position: absolute;
  display: inline-block;
  top: -5px;
  left: 42px;
  width: 134px;
  font-size: 16px;
  font-weight: 500;
  color: rgba(0, 0, 0, 0.85);
  line-height: 24px;
}
.el-step__head.is-finish .el-step__icon-inner {
  color: #5850ff;
}
.el-step__head.is-finish .el-step__line.is-finish {
  background: #5850ff;
}
.el-step__head.is-finish .el-step__icon {
  border-color: #5850ff;
  border-radius: 50%;
  // background: #5850ff;
}
.el-step__head.is-finish .el-step__icon.is-text {
  border-color: #5850ff;
  background: #5850ff;
}
.el-step__icon.is-text .el-step__icon-inner {
  color: #ffffff;
}
.el-step__title {
  font-size: 16px;
  line-height: 24px;
  font-weight: 500;
  color: rgba(0, 0, 0, 0.65);
}
.el-step__sub {
  font-size: 14px;
  line-height: 24px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.45);
}
.el-step__description {
  font-size: 13px;
}
.el-step__title.is-wait {
  font-weight: 400;
}
.el-step__title.is-finish {
  font-weight: 600;
  color: rgba(0, 0, 0, 0.85);
}
.el-step__sub.is-finish {
  color: rgba(0, 0, 0, 0.65);
  line-height: 22px;
}
.image-uploader {
  position: relative;
  height: 40px;
  display: flex;
  margin: 20px 0px;
  justify-content: center;
  input {
    cursor: pointer;
    position: absolute;
    top: 0px;
    width: 35px;
    height: 35px;
    font-size: 0px;
  }
  /deep/ {
    .el-upload-list {
      display: none;
    }
  }
}
.squre-safe-panel {
  padding: 40px;
  margin-top: 10px;
  margin-bottom: 16px;
  /deep/ {
    .el-carousel__indicators,
    .el-carousel__arrow {
      display: none;
    }
  }
  .safe-upload-panel {
    margin: 20px auto;
    padding: 20px;
    box-sizing: border-box;
    width: 384px;
    height: 214px;
    background: rgba(0, 0, 0, 0.02);
    border-radius: 4px;
    border: 1px solid rgba(0, 0, 0, 0.15);
    text-align: center;
    .safe-upload-icon {
      i {
        font-size: 35px;
        color: #5850ff;
      }
    }
    .safe-upload-tips {
      font-size: 14px;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.65);
      line-height: 22px;
    }
    .safe-upload-xl {
      font-size: 12px;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.45);
      line-height: 20px;
    }
    .safe-upload-link {
      font-size: 14px;
      font-weight: 400;
      color: #5850ff;
      line-height: 22px;
      margin-top: 10px;
      cursor: pointer;
    }
  }
  .safe-upload-footer {
    width: 300px;
    display: flex;
    justify-content: center;
    margin: 0 auto;
  }
  .squre-safe-title {
    text-align: center;
    font-size: 32px;
    font-weight: 600;
    color: rgba(0, 0, 0, 0.85);
    line-height: 40px;
  }
  .swiper-container {
    width: 70%;
    margin: 40px auto;
    .swiper-wrap {
      display: flex;
      justify-content: center;
      text-align: center;
      .swiper-wrap-item {
        width: 20%;
        font-size: 24px;
        font-weight: 300;
        color: #464646;
        line-height: 32px;
        letter-spacing: 2px;
        cursor: pointer;
        &.checked {
          color: #1d39c4;
          font-weight: 600;
        }
      }
    }
    .swiper-container-content {
      font-size: 16px;
      font-weight: 500;
      color: rgba(0, 0, 0, 0.85);
      line-height: 24px;
      margin-right: 80px;
    }
    .swiper-container-panel {
      padding: 40px;
      box-sizing: border-box;
      margin: 40px 0px;
      height: 328px;
      background: #ffffff;
      border-radius: 2px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .swiper-container-image {
        img {
          height: 248px;
          width: auto;
        }
      }
    }
  }
}
</style>