优化
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
|
declare const mqtt: any
|
||||||
|
|
||||||
const deviceId = 'fcfabf1d5dd036033419057075a56304' // 设备 ID,实际使用中可动态获取
|
|
||||||
const selectedDeviceId = ref<string>('')
|
const selectedDeviceId = ref<string>('')
|
||||||
const deviceList = ref<DeviceItem[]>([])
|
const deviceList = ref<DeviceItem[]>([])
|
||||||
|
|
||||||
@ -150,7 +149,7 @@ function openVideo(id: string, index: number) {
|
|||||||
if (box && sourcesMap[id]) {
|
if (box && sourcesMap[id]) {
|
||||||
box.subscribeLive(sourcesMap[id])
|
box.subscribeLive(sourcesMap[id])
|
||||||
// 订阅流
|
// 订阅流
|
||||||
apis.subscribeLive(deviceId,id).then((res) => {
|
apis.subscribeLive(selectedDeviceId.value,id).then((res) => {
|
||||||
if (res) {
|
if (res) {
|
||||||
box.startPlay(res)
|
box.startPlay(res)
|
||||||
} else {
|
} else {
|
||||||
@ -170,6 +169,24 @@ function closeVideo(index: number) {
|
|||||||
boxRefs[index]?.destroy()
|
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 初始化完成)
|
// VideoBox ready 事件(videoInfo 初始化完成)
|
||||||
function onVideoReady(_info: VideoInfo, _index: number) {
|
function onVideoReady(_info: VideoInfo, _index: number) {
|
||||||
// 可在此做额外处理
|
// 可在此做额外处理
|
||||||
@ -219,10 +236,14 @@ function onDeviceChange() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
|
window.cleanupAll = cleanupAll;
|
||||||
|
window.handleInit = handleInit;
|
||||||
|
await handleInit()
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
async function handleInit(){
|
||||||
try {
|
try {
|
||||||
//const tokenRes = await apis.getToken()
|
|
||||||
// if (tokenRes.error_code === 0) {
|
|
||||||
//config.token = tokenRes.data
|
|
||||||
apis = createApis()
|
apis = createApis()
|
||||||
|
|
||||||
// 加载设备列表
|
// 加载设备列表
|
||||||
@ -236,12 +257,12 @@ onMounted(async () => {
|
|||||||
if (selectedDeviceId.value) {
|
if (selectedDeviceId.value) {
|
||||||
await loadSources(selectedDeviceId.value)
|
await loadSources(selectedDeviceId.value)
|
||||||
}
|
}
|
||||||
//}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('初始化失败', e)
|
console.error('初始化失败', e)
|
||||||
}
|
}
|
||||||
|
|
||||||
connectMqtt()
|
connectMqtt()
|
||||||
})
|
}
|
||||||
|
|
||||||
async function loadSources(deviceId: string) {
|
async function loadSources(deviceId: string) {
|
||||||
try {
|
try {
|
||||||
@ -260,7 +281,8 @@ async function loadSources(deviceId: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
onBeforeUnmount(() => {
|
||||||
mqttClient?.end()
|
window.cleanupAll = () => {};
|
||||||
|
window.handleInit = () => {};
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
2
src/types/global.d.ts
vendored
2
src/types/global.d.ts
vendored
@ -1,5 +1,7 @@
|
|||||||
declare interface Window {
|
declare interface Window {
|
||||||
HybridWebView: HybridWebView
|
HybridWebView: HybridWebView
|
||||||
|
cleanupAll: () => void
|
||||||
|
handleInit: () => void
|
||||||
}
|
}
|
||||||
declare interface HybridWebView {
|
declare interface HybridWebView {
|
||||||
InvokeDotNet(methodName: string, ...args: any[]): Promise<T>;
|
InvokeDotNet(methodName: string, ...args: any[]): Promise<T>;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user