优化
This commit is contained in:
parent
fe9229f044
commit
d075dabf2a
38
src/App.vue
38
src/App.vue
@ -58,7 +58,6 @@ import type { GlobalConfig, SourceItem, VideoInfo, DeviceItem } from './types'
|
||||
|
||||
declare const mqtt: any
|
||||
|
||||
const deviceId = 'fcfabf1d5dd036033419057075a56304' // 设备 ID,实际使用中可动态获取
|
||||
const selectedDeviceId = ref<string>('')
|
||||
const deviceList = ref<DeviceItem[]>([])
|
||||
|
||||
@ -150,7 +149,7 @@ function openVideo(id: string, index: number) {
|
||||
if (box && sourcesMap[id]) {
|
||||
box.subscribeLive(sourcesMap[id])
|
||||
// 订阅流
|
||||
apis.subscribeLive(deviceId,id).then((res) => {
|
||||
apis.subscribeLive(selectedDeviceId.value,id).then((res) => {
|
||||
if (res) {
|
||||
box.startPlay(res)
|
||||
} else {
|
||||
@ -170,6 +169,24 @@ function closeVideo(index: number) {
|
||||
boxRefs[index]?.destroy()
|
||||
}
|
||||
|
||||
// 清理所有资源(页面卸载时调用)
|
||||
function cleanupAll() {
|
||||
// 关闭所有视频
|
||||
for (let i = 0; i < 4; i++) {
|
||||
if (playingSources[i]) {
|
||||
closeVideo(i)
|
||||
}
|
||||
}
|
||||
// 清空引用和状态
|
||||
playingSources.fill(null)
|
||||
boxRefs.fill(null)
|
||||
sourceList.value = []
|
||||
// MQTT 断联
|
||||
mqttClient?.end(true)
|
||||
mqttClient = null
|
||||
drawerOpen.value = false
|
||||
}
|
||||
|
||||
// VideoBox ready 事件(videoInfo 初始化完成)
|
||||
function onVideoReady(_info: VideoInfo, _index: number) {
|
||||
// 可在此做额外处理
|
||||
@ -219,10 +236,14 @@ function onDeviceChange() {
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
window.cleanupAll = cleanupAll;
|
||||
window.handleInit = handleInit;
|
||||
await handleInit()
|
||||
})
|
||||
|
||||
|
||||
async function handleInit(){
|
||||
try {
|
||||
//const tokenRes = await apis.getToken()
|
||||
// if (tokenRes.error_code === 0) {
|
||||
//config.token = tokenRes.data
|
||||
apis = createApis()
|
||||
|
||||
// 加载设备列表
|
||||
@ -236,12 +257,12 @@ onMounted(async () => {
|
||||
if (selectedDeviceId.value) {
|
||||
await loadSources(selectedDeviceId.value)
|
||||
}
|
||||
//}
|
||||
} catch (e) {
|
||||
console.error('初始化失败', e)
|
||||
}
|
||||
|
||||
connectMqtt()
|
||||
})
|
||||
}
|
||||
|
||||
async function loadSources(deviceId: string) {
|
||||
try {
|
||||
@ -260,7 +281,8 @@ async function loadSources(deviceId: string) {
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
mqttClient?.end()
|
||||
window.cleanupAll = () => {};
|
||||
window.handleInit = () => {};
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
2
src/types/global.d.ts
vendored
2
src/types/global.d.ts
vendored
@ -1,5 +1,7 @@
|
||||
declare interface Window {
|
||||
HybridWebView: HybridWebView
|
||||
cleanupAll: () => void
|
||||
handleInit: () => void
|
||||
}
|
||||
declare interface HybridWebView {
|
||||
InvokeDotNet(methodName: string, ...args: any[]): Promise<T>;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user