index.vue
4.71 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
<script setup lang="ts">
import { Spin } from 'ant-design-vue'
import { computed, nextTick, onMounted, onUnmounted, ref, toRaw, unref } from 'vue'
import 'video.js/dist/video-js.css'
import type { VideoJsPlayerOptions } from 'video.js'
import { BasicVideoPlay } from './component/index.ts'
import { VideoPlayerType, getVideoTypeByUrl, isRtspProtocol, useFingerprint } from './component/config'
import { getJwtToken, getShareJwtToken } from '@/utils/auth'
import { isLightboxMode, isShareMode } from '@/utils/env'
import type { CreateComponentType } from '@/core/Library/types'
import { closeFlvPlay, getFlvPlayUrl, getStreamingPlayUrl, getVideoControlStart } from '@/api/video'
import { useContentDataStore } from '@/store/modules/contentData'
import { VideoAccessModeEnum } from '@/enums/videoEnum'
const props = defineProps<{
config: CreateComponentType
}>()
// 获取节点数据
const contentDataStore = useContentDataStore()
const { getResult } = useFingerprint()
const loading = ref(false)
const getOptions = computed<VideoJsPlayerOptions>(() => {
const { config } = props || {}
const { cellBounds } = config || {}
const { height, width } = cellBounds as any
return { height, width }
})
// 存储视频数据
const videoConfig = computed(() => {
return contentDataStore?.contentData.filter((item) => {
return props.config.cellInfo?.id === item.id
})
})
// const loading = ref<boolean>(false)
const exampleVideoPlay = 'https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm'
const basicVideoPlayEl = ref<Nullable<InstanceType<typeof BasicVideoPlay>>>(null)
const withToken = ref(false)
const playSource = ref<Record<'src' | 'type', string>>(
{} as unknown as Record<'src' | 'type', string>,
)
// 数据绑定获取的url
const handleGetVideoPlay = async () => {
try {
loading.value = true
const { dataSourceJson } = unref(videoConfig)[0] || {}
const { videoOption } = dataSourceJson || {}
const { id, accessMode, videoUrl, deviceId, channelId } = videoOption || {}
let type = getVideoTypeByUrl(videoUrl!)
let playUrl = videoUrl
// 判断是否是流媒体播放
if (accessMode === VideoAccessModeEnum.Streaming && id) {
const { data: { url } = { url: '' } } = await getStreamingPlayUrl(id!)
playUrl = url
playUrl && (type = getVideoTypeByUrl(playUrl!))
}
else if (accessMode === VideoAccessModeEnum.GBT28181 && deviceId && channelId) {
const {
data: { flv },
} = await getVideoControlStart({ channelId, deviceId })
playUrl = flv
type = VideoPlayerType.flv
}
// 判断是否是rtsp播放
if (isRtspProtocol(videoUrl!)) {
const result = await getResult()
const { visitorId } = result
playUrl = getFlvPlayUrl(playUrl!, visitorId)
withToken.value = true
}
playSource.value = {
src: playUrl!,
type,
}
const instance = unref(basicVideoPlayEl)?.customInit((options) => {
if (unref(withToken)) {
(options as any).flvjs.config.headers = {
'X-Authorization': `Bearer ${isShareMode() ? getShareJwtToken() : getJwtToken()}`,
}
}
return {
...options,
sources: [toRaw(unref(playSource))],
} as VideoJsPlayerOptions
})
instance?.play()
}
finally {
loading.value = false
}
}
// 默认播放
const handleSelectPreview = () => {
// loading.value = false
const instance = unref(basicVideoPlayEl)?.customInit((options) => {
withToken.value = true
if (unref(withToken)) {
(options as any).flvjs.config.headers = {
'X-Authorization': `Bearer ${isShareMode() ? getShareJwtToken() : getJwtToken()}`,
}
}
return {
...options,
sources: [{ type: getVideoTypeByUrl(exampleVideoPlay), src: exampleVideoPlay }],
} as VideoJsPlayerOptions
})
instance?.play()
}
onMounted(async () => {
await nextTick()
isLightboxMode() || isShareMode()
? handleGetVideoPlay()
: handleSelectPreview()
})
onUnmounted(async () => {
const { visitorId } = await getResult()
const { dataSourceJson } = unref(videoConfig)[0] || {}
const { videoOption } = dataSourceJson || {}
const { videoUrl } = videoOption || {}
isRtspProtocol(videoUrl!) && closeFlvPlay(videoUrl!, visitorId)
await nextTick()
unref(basicVideoPlayEl)?.getInstance()?.dispose()
})
</script>
<template>
<div class="w-full h-full flex justify-center items-center">
<Spin :spinning="loading">
<BasicVideoPlay
ref="basicVideoPlayEl" :options="getOptions" :with-token="withToken"
:immediate-init-on-mounted="false"
/>
</Spin>
</div>
</template>
<style lang="less" scoped>
.video-spin {
@apply w-full h-full;
:deep(.ant-spin-container) {
@apply w-full h-full;
}
}
</style>