wangrong
2026-09-24 bf9400d4198094194439214b5e90036b2f040052
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
<template>
  <div class="camera-page">
    <div class="scroll-wrapper">
      <el-button class="arrow left" @click="scrollLeftManual" :disabled="canScrollLeft" circle>
        <el-icon><ArrowLeft /></el-icon>
      </el-button>
 
      <div class="scroll-viewport" ref="viewportRef" @mouseenter="pauseAuto" @mouseleave="resumeAuto">
        <div class="scroll-track" :style="{ transform: `translateX(${-scrollPos}px)` }">
          <div v-for="dev in deviceList" :key="dev.id" class="player-card">
            <ezuikit-player
              v-if="ysToken"
              :key="'p_' + dev.id + '_' + ysToken + '_' + playerRemountKey"
              :container-id="'player_' + dev.id + '_' + playerRemountKey"
              :access-token="ysToken"
              :url="`ezopen://open.ys7.com/${dev.iotCode || dev.code}/1.hd.live`"
              :pause-after-first-frame="true"
            />
          </div>
        </div>
      </div>
 
      <el-button class="arrow right" @click="scrollRightManual" :disabled="canScrollRight" circle>
        <el-icon><ArrowRight /></el-icon>
      </el-button>
    </div>
  </div>
</template>
 
<script lang="ts" setup>
defineOptions({
  name: 'camera-monitor',
});
 
import { ref, computed, onMounted, onUnmounted } from 'vue';
import { useCool } from '/@/cool';
import { ArrowLeft, ArrowRight } from '@element-plus/icons-vue';
import EzuikitPlayer from '../../ezuikit/components/ezuikit-player.vue';
 
const { service } = useCool();
 
const deviceList = ref<any[]>([]);
const ysToken = ref('');
const loading = ref(false);
const scrollPos = ref(0);
const viewportRef = ref<HTMLElement | null>(null);
/** 全屏切换后递增,强制重建萤石播放器 */
const playerRemountKey = ref(0);
 
function onScreenFullscreenChange() {
  playerRemountKey.value += 1;
}
 
// 自动滚动控制
const AUTO_INTERVAL = 3000; // 每次滚动间隔(ms)
const autoPlay = ref(true); // 是否自动滚动
let autoTimer: ReturnType<typeof setInterval> | null = null;
 
// 每次滚动的距离
const scrollStep = computed(() => {
  const cardWidth = 300; // 与 player-card 宽度一致
  const gap = 12;
  return cardWidth + gap;
});
 
// 可滚动的最大距离
const maxScroll = computed(() => {
  if (!viewportRef.value) return 0;
  const viewportWidth = viewportRef.value.clientWidth;
  const trackWidth = deviceList.value.length * scrollStep.value;
  return Math.max(0, trackWidth - viewportWidth);
});
 
// 是否可以向左/向右滚动(手动箭头用)
const canScrollLeft = computed(() => scrollPos.value <= 0);
const canScrollRight = computed(() => scrollPos.value >= maxScroll.value - 2);
 
// 自动滚动方向: 1 向右, -1 向左
const direction = ref(1);
 
function scrollLeft() {
  scrollPos.value = Math.max(0, scrollPos.value - scrollStep.value);
}
 
function scrollRight() {
  const max = maxScroll.value;
  if (scrollPos.value + scrollStep.value >= max) {
    scrollPos.value = max;
  } else {
    scrollPos.value += scrollStep.value;
  }
}
 
// 执行一步自动滚动(到边界自动换向,回到起点循环)
function autoTick() {
  if (deviceList.value.length === 0) return;
  if (maxScroll.value <= 0) return; // 一屏装下全部,不滚动
 
  scrollPos.value += scrollStep.value * direction.value;
 
  if (scrollPos.value >= maxScroll.value) {
    scrollPos.value = maxScroll.value;
    direction.value = -1; // 到最右,回头向左
  } else if (scrollPos.value <= 0) {
    scrollPos.value = 0;
    direction.value = 1; // 回到起点,循环向右
    pauseAuto();          // 回到起点停一下(可选,见下方 startAuto)
    startAuto();          // 立即开始新一轮
  }
}
 
function startAuto() {
  stopAuto();
  if (!autoPlay.value) return;
  autoTimer = setInterval(autoTick, AUTO_INTERVAL);
}
 
function stopAuto() {
  if (autoTimer) {
    clearInterval(autoTimer);
    autoTimer = null;
  }
}
 
// 鼠标悬停暂停,移开继续
function pauseAuto() {
  stopAuto();
}
function resumeAuto() {
  startAuto();
}
 
// 手动点击箭头:重置自动滚动定时器,避免与自动节奏冲突
function scrollLeftManual() {
  scrollLeft();
  startAuto();
}
function scrollRightManual() {
  scrollRight();
  startAuto();
}
 
async function fetchYsToken() {
  if (ysToken.value) return;
  try {
    const res = await service.base.sys.param.stringbykey({ key: 'ys7.accessToken' });
    ysToken.value = res as string;
  } catch (e: any) {
    console.error('萤石 Token 获取失败', e?.response?.data?.message || e.message);
  }
}
 
async function fetchDevices() {
  loading.value = true;
  try {
    const res = await service.basicdata.iot.getCameras();
    deviceList.value = (res || []) as any[];
    scrollPos.value = 0;
    direction.value = 1;
    // 数据加载完再启动自动滚动
    startAuto();
  } catch (e: any) {
    console.error('获取摄像头列表失败', e?.response?.data?.message || e.message);
    deviceList.value = [];
  } finally {
    loading.value = false;
  }
}
 
async function refreshDevices() {
  stopAuto();
  await fetchDevices();
}
 
function handleKeydown(e: KeyboardEvent) {
  if (e.key === 'ArrowLeft') { scrollLeftManual(); }
  if (e.key === 'ArrowRight') { scrollRightManual(); }
}
 
onMounted(async () => {
  window.addEventListener('keydown', handleKeydown);
  window.addEventListener('report-screen-fullscreen-change', onScreenFullscreenChange);
  await fetchYsToken();
  await fetchDevices(); // 内部会在数据就绪后 startAuto
});
 
onUnmounted(() => {
  stopAuto();
  window.removeEventListener('keydown', handleKeydown);
  window.removeEventListener('report-screen-fullscreen-change', onScreenFullscreenChange);
});
</script>
 
<style lang="scss" scoped>
.camera-page {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
 
.scroll-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
  /* 移除 max-width 限制,让容器占满可用宽度 */
}
 
.scroll-viewport {
  overflow: hidden;
  flex: 1;
  margin: 0 42px;
}
 
.scroll-track {
  display: flex;
  gap: 12px;
  transition: transform 0.35s ease;
}
 
.player-card {
  flex-shrink: 0;
  width: 300px;
  height: 360px;
  border-radius: 8px;
  overflow: hidden;
  background: #000;
}
 
.arrow {
  flex-shrink: 0;
  z-index: 10;
  background: #00ff00 !important;
  border-color: #00ff00 !important;
  color: #000 !important;
  font-size: 18px;
  width: 32px;
  height: 96px; /* 加高箭头,更易点击 */
  border-radius: 4px !important;
}
 
.arrow:hover {
  background: #33ff33 !important;
  border-color: #33ff33 !important;
}
 
.arrow:active {
  background: #00cc00 !important;
  border-color: #00cc00 !important;
}
 
.el-button.is-disabled {
  opacity: 0.25;
  background: #555 !important;
  border-color: #555 !important;
  color: #888 !important;
}
 
@media (max-width: 768px) {
  .camera-page {
    padding: 8px;
  }
 
  .scroll-viewport {
    margin: 0 6px;
  }
 
  .player-card {
    width: min(280px, calc(100vw - 96px));
    height: 240px;
  }
 
  .arrow {
    width: 28px;
    height: 64px;
  }
}
</style>