zoukankan      html  css  js  c++  java
  • 分片分N次请求记录,包含重试控制,前端拼接总数据数组后导出

    question:

    1、如何控制每次并发的数量

    2、promise 改写并发

    一、同步版本

        
        async loopReqFile_sync (params) {
          //  199/30   0~6   7次,已默认请求第一次,1~6
          for (let i = 1; i < this.reqCount; i++) {
            params.start = i * this.downSize
            const { data } = await this.$api.audit.getNewRecordExcel_batch(params)
            this.fileData[i] = data.list
          }
          this.end = Date.now()
          console.log('cost time is', this.end - this.start)
          this.exportFileFunc()
          this.loading = false
        },

    二、现在做到了并发请求,非promise版本

         

      handleExportExcel () {
          this.loading = true
          const formData = this.search.getFieldsValue()
          const _formData = omit(formData, ['start_end_time'])
          const params = {
            ..._formData,
            start_time: formData.start_end_time[0].format('YYYY-MM-DD HH:mm:ss'),
            end_time: formData.start_end_time[1].format('YYYY-MM-DD HH:mm:ss')
          }
    
          this.fileData = []
          this.retryObj = {}
          params.start = 0
          params.size = this.downSize
    // 第一次请求
    this.$api.audit .getNewRecordExcel_batch(params) // { responseType: 'blob' } .then(res => { const totalNum = res.data.count if (totalNum > 10000) { this.$message.info('请减少导出数量') this.loading = false } else { if (res.data.list.length > 0) { this.fileData[0] = res.data.list this.retryObj['file_0'] = 0 // 重试控制 if (res.data.list.length < res.data.count) { // 第一次没有请求完数据 this.reqCount = Math.ceil((res.data.count) / (this.downSize)) this.start = Date.now()
    // 剩下的请求
    this.loopReqFile_async(params) } else { this.loading = false this.exportFileFunc() } } else { this.$message.info('暂无数据') this.loading = false } } }) .catch(() => {}) .finally(() => { }) },


        // 异步依次请求
        loopReqFile_async (params) {
          //  199/30   0~6   7次,已默认请求第一次,1~6
          this.realReqCount = 0
          for (let i = 1; i < this.reqCount; i++) {
            const j = i
            const reqObj = { ...params }
            reqObj.start = j * this.downSize
            this.retryObj['file_' + j] = 0

            setTimeout(() => {
              this.reqEveryFile(reqObj, j)
            }, j * 200)
          }
        },


       reqEveryFile (params, index) {
          // 异步并发
          this.$api.audit.getNewRecordExcel_batch(params).then(res => {
            this.fileData[index] = res.data.list
            this.realReqCount++
            if (this.realReqCount === (this.reqCount - 1)) {
              this.exportFileFunc()
              this.loading = false
              this.end = Date.now()
            }
          }).catch((e) => {
            if (this.retryObj['file_' + index] === 0) {
              this.reqEveryFile(params, index)
              this.retryObj['file_' + index] = this.retryObj['file_' + index] + 1
            }
          }).finally(() => {})
        },
     
     
       // 导出
        exportFileFunc () {
          let records = []
          for (let i = 0; i < this.fileData.length; i++) {
            records = records.concat(this.fileData[i])
          }

          const exportData = []
          const excludeEmt = ['nickname', 'mtime', 'action']
          const newColumns = this.columns.filter(item => excludeEmt.indexOf(item.dataIndex) === -1)
          console.log('newColumns', newColumns)
          exportData[0] = newColumns.map((item) => item.title)
          for (let j = 0; j < records.length; j++) {
            const tmpArr = newColumns.map((item) => {
              const crtKey = item.dataIndex
              let crtValue = records[j][crtKey]
              switch (crtKey) {
                case 'updated': crtValue = moment.unix(crtValue).format('YYYY-MM-DD HH:mm:ss')
              }
              return crtValue
            })
            exportData.push(tmpArr)
          }
     
          var workbook = new Excel.Workbook()
          var sheet = workbook.addWorksheet('Worksheet')
          sheet.addRows(exportData)
          workbook.xlsx.writeBuffer({ base64: true })
            .then(buffer => {
              var blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
              var a = document.createElement('a')
              a.href = URL.createObjectURL(blob)
              a.download = `记录.xlsx`
              a.click()
              var dispose = () => URL.revokeObjectURL(blob)
              setTimeout(dispose, 100)
            })
        },
     

          

    三、promise版本

  • 相关阅读:
    以太坊编程简单介绍 ,Part-1
    以太坊可更新智能合约研究与开发综述
    为你的以太坊应用程序设计架构
    货币的未来取决于打破关于货币历史的虚构谎言
    JVM调优:GC 参数
    JVM调优:GC 参数
    Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法
    Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法
    Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法
    Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法
  • 原文地址:https://www.cnblogs.com/dhjy123/p/15475941.html
Copyright © 2011-2022 走看看