traffic-audit-web/src/views/ReportExport.vue
@@ -162,7 +162,7 @@
        </el-dialog>
        <!-- 汇总大表异步生成进度(生成一次约 10~20 秒,避免点击后长时间无响应) -->
        <el-dialog title="正在生成汇总大表" :visible.sync="taskVisible" width="460px"
        <el-dialog :title="taskDialogTitle" :visible.sync="taskVisible" width="460px"
                   :close-on-click-modal="false" :show-close="taskFinished">
          <div class="task-body">
            <el-progress :percentage="taskInfo.percent || 0" :status="taskInfo.status === 'FAILED' ? 'exception' : (taskDone ? 'success' : null)"
@@ -170,13 +170,17 @@
            <p class="task-step">{{ taskInfo.step || '排队中' }}</p>
            <p class="task-meta" v-if="taskInfo.period">报表期 {{ taskInfo.period }} {{ taskInfo.fromCache ? '上次生成用时' : '已用' }} {{ (taskInfo.elapsedMs / 1000).toFixed(1) }} 秒</p>
            <p class="task-meta" v-if="taskInfo.fromCache">本次直接使用 {{ taskCacheTime }} 生成的结果(10 分钟内且期间没有新导入数据)。如需按最新数据重算,请点「重新生成」。</p>
            <p class="task-done" v-if="taskDownloaded">
              已自动下载:<b>{{ taskFileName }}</b><span v-if="taskFileSize">({{ taskFileSizeText }})</span><br>
              浏览器没弹出下载时可点「再次下载」——用本地缓存保存,不会重新请求服务器。
            </p>
            <p class="task-error" v-if="taskInfo.status === 'FAILED'">{{ taskInfo.error }}</p>
          </div>
          <span slot="footer">
            <el-button size="small" type="primary" :disabled="!taskDone" @click="downloadTaskFile">{{ taskInfo.fromCache ? '下载' : '保存文件' }}</el-button>
            <el-button size="small" :disabled="!taskDone" @click="downloadTaskFile()">{{ taskDownloaded ? '再次下载' : (taskInfo.fromCache ? '下载' : '保存文件') }}</el-button>
            <el-button size="small" :disabled="taskRunning" @click="restartTask">重新生成</el-button>
            <el-button size="small" v-if="taskRunning" @click="minimizeTask">后台运行</el-button>
            <el-button size="small" v-else @click="closeTask">关闭</el-button>
            <el-button size="small" v-else type="primary" @click="closeTask">关闭</el-button>
          </span>
        </el-dialog>
      </el-main>
@@ -212,6 +216,11 @@
      taskVisible: false,
      taskInfo: {},
      taskTimer: null,
      // 进度弹窗:完成后会自动下载一次,这里记录「已下载」与文件信息(重复点击走本地缓存,不再请求后端)
      taskDownloaded: false,
      taskFileName: '',
      taskFileSize: 0,
      taskBlob: null,
      taskKey: 'summaryWorkbook',
      modules: [
        {
@@ -320,6 +329,18 @@
      const d = new Date(t)
      const p = (n) => String(n).padStart(2, '0')
      return d.getFullYear() + '-' + p(d.getMonth() + 1) + '-' + p(d.getDate()) + ' ' + p(d.getHours()) + ':' + p(d.getMinutes()) + ':' + p(d.getSeconds())
    },
    // 弹窗标题随任务状态变化,避免「已完成」还写着「正在生成」
    taskDialogTitle() {
      const s = this.taskInfo && this.taskInfo.status
      if (s === 'FAILED') return '汇总大表生成失败'
      if (s === 'DONE') return '汇总大表已生成'
      return '正在生成汇总大表'
    },
    taskFileSizeText() {
      const n = this.taskFileSize || 0
      if (!n) return ''
      return n >= 1048576 ? (n / 1048576).toFixed(2) + ' MB' : Math.round(n / 1024) + ' KB'
    }
  },
  watch: {
@@ -524,6 +545,10 @@
      this.stopTaskPolling()
      this.taskInfo = { status: 'PENDING', percent: 0, step: '提交任务', period: this.period, elapsedMs: 0 }
      this.taskVisible = true
      this.taskDownloaded = false
      this.taskFileName = ''
      this.taskFileSize = 0
      this.taskBlob = null
      this.loading = 'gen-' + this.curModule.key
      const params = Object.assign({}, this.buildParams(), { refresh: refresh ? 'true' : 'false' })
      api.get('/report/exportTask/summaryWorkbook', { params, timeout: 60000 })
@@ -536,7 +561,7 @@
            return
          }
          this.taskInfo = res.data
          if (this.taskInfo.status === 'DONE') { this.loading = ''; this.downloadTaskFile(); return }
          if (this.taskInfo.status === 'DONE') { this.loading = ''; this.downloadTaskFile(true); return }
          if (this.taskInfo.status === 'FAILED') { this.loading = ''; return }
          this.pollTask()
        })
@@ -568,7 +593,7 @@
            if (this.taskInfo.status === 'DONE') {
              this.stopTaskPolling()
              this.loading = ''
              this.downloadTaskFile()
              this.downloadTaskFile(true)
            } else if (this.taskInfo.status === 'FAILED') {
              this.stopTaskPolling()
              this.loading = ''
@@ -581,12 +606,26 @@
    stopTaskPolling() {
      if (this.taskTimer) { clearInterval(this.taskTimer); this.taskTimer = null }
    },
    downloadTaskFile() {
    // auto=true 表示「任务完成后的自动下载」;用户点按钮时为 false
    downloadTaskFile(auto) {
      const id = this.taskInfo && this.taskInfo.taskId
      if (!id) return
      const name = this.taskInfo.fileName || ('生成_道路运输量汇总表_' + this.period + '.xlsx')
      this.taskFileName = name
      // 已经下载过:直接用内存里的 Blob 再存一次,不再请求后端(避免同一份文件重复拉取 1MB+)
      if (this.taskBlob) {
        this.downloadBlob(this.taskBlob, name)
        this.$message.success('已从本地缓存保存:' + name)
        return
      }
      api.get('/report/exportTask/' + id + '/file', { responseType: 'blob', timeout: 120000 })
        .then(res => { this.downloadBlob(res, name); this.$message.success('报表导出成功') })
        .then(res => {
          this.taskBlob = res
          this.taskFileSize = (res && res.size) || 0
          this.taskDownloaded = true
          this.downloadBlob(res, name)
          this.$message.success(auto ? '生成完成,已自动下载' : '报表导出成功')
        })
        .catch(err => this.failWith(err))
    },
    restartTask() { this.startSummaryTask(true) },
@@ -756,6 +795,7 @@
.task-step { margin: 12px 0 4px; font-size: 14px; color:#303133; }
.task-meta { margin: 2px 0; font-size: 12px; color:#909399; line-height: 1.6; }
.task-error { margin: 8px 0 0; font-size: 13px; color:#F56C6C; word-break: break-all; }
.task-done { margin: 8px 0 0; font-size: 13px; color:#67C23A; line-height: 1.6; }
</style>
<style>