HistoricalRecord.vue 18.4 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 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
<template>
  <el-container class="history-container">
    <el-main class="layout-main">
      <splitpanes class="default-theme splitpanes-container">

        <pane :size="20" :min-size="15" :max-size="40" class="aside-pane">
          <div class="tree-wrapper">
            <vehicle-list
              ref="vehicleList"
              @tree-loaded="handleTreeLoaded"
              @node-click="nodeClick"
            />
          </div>
        </pane>

        <pane :size="80" class="main-pane">
          <div
            class="content-main"
            v-loading="loading"
            element-loading-text="拼命加载中"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(255, 255, 255, 0.7)"
          >
            <div class="header-section">
              <historical-record-form
                style="text-align:left"
                :inline="true"
                v-show="showSearch"
                :query-params="queryParams"
                @handleQuery="handleQuery"
              />
              <el-row v-if="showSelectionTag" :gutter="10" class="mb8" style="margin-bottom: 10px;">
                <el-col :span="24">
                  <el-tag effect="dark" type="success">
                    <i class="el-icon-video-camera"></i> {{ deviceTitle }}
                  </el-tag>
                </el-col>
              </el-row>

              <div v-if="allVehicleChannels.length" class="channel-pick">
                <div class="channel-pick-main">
                  <span class="channel-pick-label">查询通道(可多选):</span>
                  <el-checkbox-group v-model="checkedChannelKeys" class="channel-pick-group">
                    <el-checkbox
                      v-for="c in allVehicleChannels"
                      :key="c.key"
                      :label="c.key"
                    >
                      {{ c.channelName }}
                    </el-checkbox>
                  </el-checkbox-group>
                </div>
                <div class="channel-pick-actions">
                  <el-button type="text" size="mini" @click="selectAllChannels">全选</el-button>
                  <el-button type="text" size="mini" @click="clearChannelSelection">清空</el-button>
                </div>
              </div>
            </div>

            <div class="table-section">
              <history-search-table
                ref="historySearchTable"
                class="history-search-table-host"
                :table-data="historyData"
                :table-height="historyTableHeight"
                :busy-channel-keys="busyChannelKeys"
                @playHistoryVideo="clickHistoricalPlay"
                @uploadHistoryVideo="onUploadHistoryVideo"
                @channel-operation="onChannelOperation"
              />
            </div>

            <history-play-dialog ref="historyPlayDialog" />
          </div>
        </pane>
      </splitpanes>
    </el-main>
  </el-container>
</template>

<script>
import VehicleList from "./JT1078Components/deviceList/VehicleList.vue";
import HistoryPlayDialog from "./JT1078Components/HistoryPlayDialog.vue";
import HistoricalRecordForm from "./JT1078Components/HistoryRecordFrom.vue";
import HistorySearchTable from "./JT1078Components/HistorySearchTable.vue";
import { Splitpanes, Pane } from 'splitpanes'
import 'splitpanes/dist/splitpanes.css'
import { parseTime } from "../../utils/ruoyi";

export default {
  name: "HistoricalRecord",
  components: {
    VehicleList,
    HistoryPlayDialog,
    HistorySearchTable,
    HistoricalRecordForm,
    Splitpanes,
    Pane
  },
  data() {
    return {
      timer: null,
      historyTimer: null,
      historyData: [],
      sourceValue: [],
      loading: false,
      sim_channel: null,
      selectedChannels: [],
      /** 当前车辆下可选通道(用于复选框) */
      allVehicleChannels: [],
      /** 选中的通道 key:sim_channel */
      checkedChannelKeys: [],
      channelData: null,
      nodeChannelData: null,
      deviceData: null,
      deviceTitle: '',
      showSearch: true,
      queryParams: {
        time: this.getTodayRange(),
        pageNum: 1,
        pageSize: 10
      },
      videoUrl: [],
      deviceNode: null,
      busyChannelKeys: [],
      /** 用于计算表格固定高度,避免 el-table height=100% 无像素高度导致无法滚动 */
      viewportHeight: typeof window !== 'undefined' ? window.innerHeight : 600
    };
  },
  computed: {
    showSelectionTag() {
      return this.allVehicleChannels.length > 0 || this.deviceData || this.channelData
    },
    historyTableHeight() {
      const v = this.viewportHeight || 600
      /* 预留:顶栏/标签/表单/通道区/内边距(略多留一点以免裁切) */
      const reserved = 340
      return Math.max(260, Math.floor(v - reserved))
    }
  },
  mounted() {
    this._onViewportResize = () => {
      this.viewportHeight = window.innerHeight
    }
    window.addEventListener('resize', this._onViewportResize)
  },
  watch: {
    deviceNode(val) {
      this.deviceNode = val
      if (this.$refs.historySearchTable) {
        this.$refs.historySearchTable.deviceNode = val
      }
    }
  },
  destroyed() {
    clearInterval(this.timer)
    clearInterval(this.historyTimer)
    if (this._onViewportResize) {
      window.removeEventListener('resize', this._onViewportResize)
    }
  },
  methods: {
    getTodayRange() {
      const startOfToday = new Date()
      startOfToday.setHours(0, 0, 0, 0)
      const endOfToday = new Date()
      endOfToday.setHours(23, 59, 59, 999)
      return [startOfToday, endOfToday]
    },

    handleTreeLoaded(data) {
      this.sourceValue = data;
    },

    channelKey(sim, channel) {
      return String(sim) + '_' + String(channel)
    },

    parseChannelFromId(nodeData) {
      const parts = String(nodeData.id).split('_')
      if (parts.length !== 3) return null
      const sim = parts[1]
      const channel = parts[2]
      return {
        sim,
        channel,
        channelName: nodeData.name,
        vehicleName: nodeData.pid,
        key: this.channelKey(sim, channel)
      }
    },

    buildNodeChannelMap(children) {
      const map = {}
      if (!children) return map
      for (let i = 0; i < children.length; i++) {
        const c = children[i]
        const ids = c.id.split('_')
        if (ids.length === 3) {
          c.deviceId = ids[0]
          c.channelId = ids[2]
          map[ids[2]] = c
        }
      }
      return map
    },

    /**
     * 点击通道:单通道并默认勾选;点击车辆:列出通道,需手动勾选再搜索
     */
    nodeClick(data, node) {
      if (!data) return
      const split = data.id.split('_')
      this.deviceNode = node

      if (split.length === 3) {
        const ch = this.parseChannelFromId(data)
        this.channelData = data
        this.deviceData = node.parent && node.parent.data ? node.parent.data : null
        this.deviceTitle = `车辆:${data.pid} 通道:${data.name}`

        const children = node.parent && node.parent.data && node.parent.data.children
        this.nodeChannelData = this.buildNodeChannelMap(children)

        this.allVehicleChannels = []
        if (children && children.length) {
          for (let i = 0; i < children.length; i++) {
            const p = this.parseChannelFromId(children[i])
            if (p) {
              p.vehicleName = data.pid
              this.allVehicleChannels.push(p)
            }
          }
        } else if (ch) {
          this.allVehicleChannels = [ch]
        }
        this.checkedChannelKeys = ch ? [ch.key] : []
        this.selectedChannels = ch ? [ch] : []
        this.sim_channel = ch ? ch.sim + '_' + ch.channel : null
        return
      }

      this.deviceData = data
      this.channelData = null
      const kids = data.children
      if (kids && kids.length > 0) {
        const fp = kids[0] && kids[0].id ? String(kids[0].id).split('_') : []
        if (fp.length === 3) {
          this.allVehicleChannels = []
          for (let i = 0; i < kids.length; i++) {
            const p = this.parseChannelFromId(kids[i])
            if (p) {
              p.vehicleName = data.name
              this.allVehicleChannels.push(p)
            }
          }
          this.nodeChannelData = this.buildNodeChannelMap(kids)
          this.checkedChannelKeys = []
          this.selectedChannels = []
          this.sim_channel = null
          this.deviceTitle = `车辆:${data.name} · 请勾选要查询的通道后点击搜索`
          return
        }
      }
      this.allVehicleChannels = []
      this.checkedChannelKeys = []
      this.selectedChannels = []
      this.sim_channel = null
      this.nodeChannelData = {}
      this.deviceTitle = `车辆/分组:${data.name}(请展开后选择车辆或通道)`
    },

    selectAllChannels() {
      this.checkedChannelKeys = this.allVehicleChannels.map(c => c.key)
    },

    clearChannelSelection() {
      this.checkedChannelKeys = []
    },

    handleQuery(queryParams) {
      this.queryParams = { ...this.queryParams, ...queryParams };
      this.searchHistoryList()
    },

    clickHistoricalPlay(data) {
      this.playHistoryItem(data)
    },

    channelBusyKey(row) {
      return String(row.sim) + '_' + String(row.channel)
    },

    onChannelOperation({ key, busy }) {
      if (busy) {
        if (this.busyChannelKeys.indexOf(key) < 0) {
          this.busyChannelKeys.push(key)
        }
      } else {
        this.busyChannelKeys = this.busyChannelKeys.filter(k => k !== key)
      }
    },

    onUploadHistoryVideo(row) {
      const key = this.channelBusyKey(row)
      if (this.busyChannelKeys.indexOf(key) >= 0) {
        this.$message.warning('该通道正在上传或下载,请稍候')
        return
      }
      this.busyChannelKeys.push(key)
      this.loading = true
      this.$axios({
        method: 'get',
        url: '/api/jt1078/query/history/uploading/' + row.name
      }).then(() => {
        this.$message.success("视频开始上传,请等待")
        this.searchHistoryList()
      }).catch(() => {
        this.$message.error("视频上传失败")
      }).finally(() => {
        this.busyChannelKeys = this.busyChannelKeys.filter(k => k !== key)
        this.loading = false
      })
    },

    extractHistoryItems(res) {
      try {
        const items = res.data && res.data.data && res.data.data.obj && res.data.data.obj.data && res.data.data.obj.data.items
        return Array.isArray(items) ? items : null
      } catch (e) {
        return null
      }
    },

    listResponseErrorMsg(res) {
      const top = res && res.data
      const body = top && top.data
      if (body && (body.msg || body.message)) return body.msg || body.message
      if (top && (top.msg || top.message)) return top.msg || top.message
      return ''
    },

    channelDisplayName(sim, channelId) {
      const k = String(channelId)
      if (this.nodeChannelData && this.nodeChannelData[k]) {
        return this.nodeChannelData[k].name
      }
      const found = this.allVehicleChannels.find(
        c => String(c.sim) === String(sim) && String(c.channel) === k
      )
      if (found && found.channelName) return found.channelName
      return `通道${Number(channelId)}`
    },

    defaultDeviceIdForRow(sim) {
      if (this.deviceData && this.deviceData.name) return this.deviceData.name
      if (this.channelData && this.channelData.pid) return this.channelData.pid
      const hit = this.allVehicleChannels.find(c => String(c.sim) === String(sim))
      return (hit && hit.vehicleName) ? hit.vehicleName : sim
    },

    /** 串行请求历史列表,避免多路并发压垮 JT1078 网关 9205 */
    async searchHistoryList() {
      if (!this.allVehicleChannels.length) {
        return this.$message.error('请先点击左侧选择车辆或通道')
      }
      if (!this.checkedChannelKeys.length) {
        return this.$message.warning('请至少勾选一个要查询的通道')
      }
      if (!this.queryParams.time) {
        return this.$message.error('请选择开始和结束时间');
      }

      const toQuery = this.allVehicleChannels.filter(c => this.checkedChannelKeys.indexOf(c.key) >= 0)
      if (!toQuery.length) {
        return this.$message.warning('所选通道无效,请重新勾选')
      }

      const t0 = parseTime(this.queryParams.time[0], '{y}-{m}-{d} {h}:{i}:{s}')
      const t1 = parseTime(this.queryParams.time[1], '{y}-{m}-{d} {h}:{i}:{s}')
      this.loading = true
      const merged = []
      let failCount = 0

      try {
        for (let i = 0; i < toQuery.length; i++) {
          const ch = toQuery[i]
          try {
            const res = await this.$axios({
              method: 'get',
              url: '/api/jt1078/query/history/list/' + ch.sim + '/' + ch.channel + '/' + t0 + '/' + t1
            })
            const inner = res.data && res.data.data
            const code = inner && inner.code
            if (code === '-1' || code === -1) {
              const msg = this.listResponseErrorMsg(res) || '该通道查询失败'
              this.$message.warning(`通道 ${ch.channelName}: ${msg}`)
              failCount++
              continue
            }
            const items = this.extractHistoryItems(res)
            if (!items || !items.length) continue
            for (let j = 0; j < items.length; j++) {
              const it = { ...items[j] }
              it.disabled = false
              it.countdown = 10
              it.channelName = this.channelDisplayName(it.sim || ch.sim, it.channel)
              it.deviceId = this.defaultDeviceIdForRow(it.sim || ch.sim)
              merged.push(it)
            }
          } catch (e) {
            failCount++
            console.warn(e)
            this.$message.warning(`通道 ${ch.channelName} 请求异常`)
          }
        }
        merged.sort((a, b) => {
          const ta = new Date(a.startTime || 0).getTime()
          const tb = new Date(b.startTime || 0).getTime()
          return ta - tb
        })
        this.historyData = merged
        this.selectedChannels = toQuery
        if (merged.length === 0) {
          this.$message.warning(failCount > 0 ? '全部通道未返回列表,请查看网关或超时配置' : '搜索历史列表为空')
        }
      } finally {
        this.loading = false
      }
    },

    playHistoryItem(e) {
      this.loading = true
      this.$axios({
        method: 'get',
        url: '/api/jt1078/query/send/request/io/history/' + e.sim + '/' + e.channel + "/" + e.startTime + "/" + e.endTime + "/" + e.channelMapping
      }).then(res => {
        if (res.data && res.data.data && res.data.data.data) {
          const d = res.data.data.data
          const videoUrl1 = location.protocol === 'https:'
            ? (d.wss_flv || d.ws_flv || d.flv)
            : (d.ws_flv || d.wss_flv || d.flv)
          if (!videoUrl1) {
            this.$message.error('未获取到播放地址')
            this.loading = false
            return
          }
          const dlg = this.$refs.historyPlayDialog
          dlg.updateOpen(true)
          this.$nextTick(() => {
            dlg.data = {
              videoUrl: videoUrl1,
              startTime: e.startTime,
              endTime: e.endTime,
              deviceId: e.deviceId,
              channelName: e.channelName,
              channel: e.channel,
              sim: e.sim,
              channelMapping: e.channelMapping
            }
            this.loading = false
          })
        } else {
          this.$message.error(res.data.msg || "获取播放地址失败");
          this.loading = false
        }
      }).catch(() => {
        this.$message.error("请求播放地址异常")
        this.loading = false
      })
    },

    setPlayUrl(url, idx) {
      this.$set(this.videoUrl, idx, url)
    },

    isEmpty(val) {
      return null == val || undefined == val || "" == val;
    }
  }
};
</script>

<style scoped>
.history-container {
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.layout-main {
  padding: 0;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

.splitpanes-container {
  height: 100%;
  min-height: 0;
  background-color: #ffffff;
}

.splitpanes-container ::v-deep .splitpanes__pane {
  min-height: 0;
  overflow: hidden;
}

.aside-pane {
  background-color: #ffffff;
  border-right: 1px solid #dcdfe6;
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.tree-wrapper {
  height: 100%;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  background-color: #ffffff;
  padding: 10px 5px;
  box-sizing: border-box;
}

.tree-wrapper ::v-deep .head-container { background-color: transparent !important; padding: 0 !important; margin: 0 !important; }
.tree-wrapper ::v-deep .head-container .el-row { margin: 0 !important; display: block; }
.tree-wrapper ::v-deep .head-container .el-col { width: 100% !important; padding: 0 !important; float: none !important; }
.tree-wrapper ::v-deep .el-input__inner { border-radius: 4px; }
.tree-wrapper ::v-deep .vue-easy-tree, .tree-wrapper ::v-deep .filter-tree { margin-top: 10px !important; padding-left: 5px !important; background-color: #ffffff !important; }

.main-pane {
  background-color: #ffffff;
  padding: 0;
  overflow: hidden;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.content-main {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  background-color: #ffffff;
  margin: 0;
  border-radius: 0;
  padding: 20px;
  box-sizing: border-box;
}

.header-section {
  flex-shrink: 0;
  margin-bottom: 15px;
  border-bottom: 1px solid #EBEEF5;
  padding-bottom: 10px;
}

.channel-pick {
  margin-top: 10px;
  padding: 10px 12px;
  background: #f5f7fa;
  border-radius: 4px;
  border: 1px solid #ebeef5;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.channel-pick-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 8px 12px;
}

.channel-pick-label {
  flex-shrink: 0;
  font-size: 13px;
  color: #606266;
  line-height: 28px;
}

.channel-pick-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 12px;
  flex: 1;
  min-width: 0;
}

.channel-pick-actions {
  flex-shrink: 0;
  margin-left: auto;
  padding-left: 12px;
  white-space: nowrap;
  line-height: 28px;
}

.table-section {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: #ffffff;
  position: relative;
}

.history-search-table-host {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: block;
}

::v-deep .el-form-item__label {
  color: #606266;
  font-weight: 500;
}
</style>