wangzhibo
7 天以前 1c3ae7bcad167c9a593f4634968a6e142d9b08f9
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
<template>
  <div class="monitor-page">
    <!-- 左侧树形 -->
    <div class="left-tree">
      <site-tree @select="handleTreeSelect" />
    </div>
 
    <!-- 右侧监控区域 -->
    <div class="right-player">
      <el-empty v-if="deviceList.length === 0" description="请左侧选择站点加载监控设备" />
      <div v-else class="player-grid" :style="gridStyle">
        <div v-for="dev in deviceList" :key="dev.id" class="player-card">
          <!-- <div class="title">{{ dev.iotName }}</div> -->
          <ezuikit-player v-if="ysToken" :key="'p_' + dev.id + '_' + ysToken" :container-id="'player_' + dev.id"
            :access-token="ysToken" :url="`ezopen://open.ys7.com/${dev.iotCode}/1.hd.live`"
            :pause-at-first-frame="true" />
        </div>
      </div>
    </div>
  </div>
</template>
 
<script lang="ts" setup>
defineOptions({
  name: 'monitor-device',
});
 
import { ref, computed, onMounted } from 'vue';
import { useCool } from '/@/cool';
import SiteTree from '../components/site-tree.vue';
import EzuikitPlayer from '../../ezuikit/components/ezuikit-player.vue';
 
const { service } = useCool();
 
const deviceList = ref<any[]>([]);
const ysToken = ref('');
 
// ✅ 根据设备数量自动计算网格列数
const gridCols = computed(() => {
  const n = deviceList.value.length;
  if (n <= 1) return 1;
  if (n <= 4) return 2;   // 2~4 个 → 2 列
  if (n <= 6) return 3;   // 5~6 个 → 3 列
  if (n <= 8) return 4;   // 7~8 个 → 4 列
  return 4;
});
 
const gridStyle = computed(() => ({
  gridTemplateColumns: `repeat(${gridCols.value}, 1fr)`,
}));
 
/** 拿萤石 token */
async function fetchYsToken() {
  if (ysToken.value) return; // 已有就不再重复拿
  try {
    const res = await service.base.sys.param.stringbykey({ key: 'ys7.accessToken' });
    ysToken.value = res as string; // ✅ 修正:赋值给 .value
  } catch (e: any) {
    console.error('萤石 Token 获取失败', e?.response?.data?.message || e.message);
  }
}
 
onMounted(fetchYsToken); // ✅ 修正:onMounted
 
/** 树节点点击 */
async function handleTreeSelect(businessId: string | number | null) {
  if (!businessId) {
    deviceList.value = [];
    return;
  }
 
  // 保险:token 万一慢于树点击
  if (!ysToken.value) {
    await fetchYsToken();
  }
 
  try {
    const res = await service.basicdata.iot.getYsDevices({ businessId });
    deviceList.value = (res || []) as any[];
  } catch {
    deviceList.value = [];
  }
}
</script>
 
<style lang="scss" scoped>
.monitor-page {
  height: 100%;
  display: flex;
  gap: 12px;
}
 
.left-tree {
  width: 280px;
  height: 100%;
  border: 1px solid var(--el-border-color);
  border-radius: 6px;
}
 
.right-player {
  flex: 1;
  height: 100%;
  border: 1px solid var(--el-border-color);
  border-radius: 6px;
  padding: 12px;
  overflow: auto;
}
 
.player-grid {
  display: grid;
  gap: 16px;
}
 
.player-card {
  .title {
    padding: 6px 0;
    font-weight: 500;
  }
}
</style>