Commit 2cf8f88e27797814a2a9ddc743c8ca11923124a1
Merge branch 'fix/data-board-video-component' into 'main_dev'
fix: 修复看板管理通过平台获取播放视频 See merge request yunteng/thingskit-front!840
Showing
2 changed files
with
55 additions
and
16 deletions
| ... | ... | @@ -2,17 +2,23 @@ |
| 2 | 2 | import { isNumber } from 'lodash'; |
| 3 | 3 | import videoJs, { VideoJsPlayer, VideoJsPlayerOptions } from 'video.js'; |
| 4 | 4 | import 'video.js/dist/video-js.css'; |
| 5 | - import { computed, CSSProperties, onMounted, onUnmounted, ref, unref } from 'vue'; | |
| 5 | + import { computed, CSSProperties, onMounted, onUnmounted, ref, toRaw, unref } from 'vue'; | |
| 6 | 6 | import { useDesign } from '/@/hooks/web/useDesign'; |
| 7 | 7 | import { getJwtToken, getShareJwtToken } from '/@/utils/auth'; |
| 8 | 8 | import { isShareMode } from '/@/views/sys/share/hook'; |
| 9 | 9 | import 'videojs-flvjs-es6'; |
| 10 | 10 | const { prefixCls } = useDesign('basic-video-play'); |
| 11 | 11 | |
| 12 | - const props = defineProps<{ | |
| 13 | - options?: VideoJsPlayerOptions; | |
| 14 | - withToken?: boolean; | |
| 15 | - }>(); | |
| 12 | + const props = withDefaults( | |
| 13 | + defineProps<{ | |
| 14 | + options?: VideoJsPlayerOptions; | |
| 15 | + withToken?: boolean; | |
| 16 | + immediateInitOnMounted?: boolean; | |
| 17 | + }>(), | |
| 18 | + { | |
| 19 | + immediateInitOnMounted: true, | |
| 20 | + } | |
| 21 | + ); | |
| 16 | 22 | |
| 17 | 23 | const emit = defineEmits<{ |
| 18 | 24 | (event: 'ready', instance?: Nullable<VideoJsPlayer>): void; |
| ... | ... | @@ -68,8 +74,18 @@ |
| 68 | 74 | }); |
| 69 | 75 | }; |
| 70 | 76 | |
| 77 | + const customInit = (getOptionsFn: (optios: VideoJsPlayerOptions) => VideoJsPlayerOptions) => { | |
| 78 | + return (videoPlayInstance.value = videoJs( | |
| 79 | + unref(videoPlayEl)!, | |
| 80 | + getOptionsFn(toRaw(unref(getOptions))), | |
| 81 | + () => { | |
| 82 | + emit('ready', unref(videoPlayInstance)); | |
| 83 | + } | |
| 84 | + )); | |
| 85 | + }; | |
| 86 | + | |
| 71 | 87 | onMounted(() => { |
| 72 | - init(); | |
| 88 | + props.immediateInitOnMounted && init(); | |
| 73 | 89 | }); |
| 74 | 90 | |
| 75 | 91 | onUnmounted(() => { |
| ... | ... | @@ -79,6 +95,7 @@ |
| 79 | 95 | }); |
| 80 | 96 | |
| 81 | 97 | defineExpose({ |
| 98 | + customInit, | |
| 82 | 99 | reloadPlayer: init, |
| 83 | 100 | getInstance: () => unref(videoPlayInstance), |
| 84 | 101 | }); | ... | ... |
| ... | ... | @@ -42,17 +42,17 @@ |
| 42 | 42 | |
| 43 | 43 | const handleGetVideoPlayUrl = async () => { |
| 44 | 44 | try { |
| 45 | - const instance = unref(basicVideoPlayEl)?.getInstance(); | |
| 46 | 45 | const { config } = props; |
| 47 | 46 | const { option } = config; |
| 48 | 47 | const { videoConfig, uuid } = option || {}; |
| 49 | 48 | if (!uuid) return; |
| 50 | 49 | const { url, id, accessMode } = videoConfig || {}; |
| 51 | - const type = getVideoTypeByUrl(url!); | |
| 50 | + let type = getVideoTypeByUrl(url!); | |
| 52 | 51 | let playUrl = url; |
| 53 | 52 | if (accessMode === AccessMode.Streaming && id) { |
| 54 | 53 | const { data: { url } = { url: '' } } = await getStreamingPlayUrl(id!); |
| 55 | 54 | playUrl = url; |
| 55 | + playUrl && (type = getVideoTypeByUrl(playUrl!)); | |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | if (isRtspProtocol(url!)) { |
| ... | ... | @@ -60,10 +60,6 @@ |
| 60 | 60 | const { visitorId } = result; |
| 61 | 61 | playUrl = getFlvPlayUrl(playUrl!, visitorId); |
| 62 | 62 | withToken.value = true; |
| 63 | - | |
| 64 | - (instance!.options_ as any).flvjs.config.headers = { | |
| 65 | - 'X-Authorization': `Bearer ${isShareMode() ? getShareJwtToken() : getJwtToken()}`, | |
| 66 | - }; | |
| 67 | 63 | } |
| 68 | 64 | |
| 69 | 65 | playSource.value = { |
| ... | ... | @@ -71,7 +67,17 @@ |
| 71 | 67 | type, |
| 72 | 68 | }; |
| 73 | 69 | |
| 74 | - instance?.src(toRaw(unref(playSource))); | |
| 70 | + const instance = unref(basicVideoPlayEl)?.customInit((options) => { | |
| 71 | + withToken.value = true; | |
| 72 | + | |
| 73 | + if (unref(withToken)) { | |
| 74 | + (options as any).flvjs.config.headers = { | |
| 75 | + 'X-Authorization': `Bearer ${isShareMode() ? getShareJwtToken() : getJwtToken()}`, | |
| 76 | + }; | |
| 77 | + } | |
| 78 | + return { ...options, sources: [toRaw(unref(playSource))] } as VideoJsPlayerOptions; | |
| 79 | + }); | |
| 80 | + | |
| 75 | 81 | instance?.play(); |
| 76 | 82 | } finally { |
| 77 | 83 | loading.value = false; |
| ... | ... | @@ -80,8 +86,19 @@ |
| 80 | 86 | |
| 81 | 87 | const handleSelectPreview = () => { |
| 82 | 88 | loading.value = false; |
| 83 | - const instance = unref(basicVideoPlayEl)?.getInstance(); | |
| 84 | - instance?.src({ type: getVideoTypeByUrl(exampleVideoPlay), src: exampleVideoPlay }); | |
| 89 | + const instance = unref(basicVideoPlayEl)?.customInit((options) => { | |
| 90 | + withToken.value = true; | |
| 91 | + | |
| 92 | + if (unref(withToken)) { | |
| 93 | + (options as any).flvjs.config.headers = { | |
| 94 | + 'X-Authorization': `Bearer ${isShareMode() ? getShareJwtToken() : getJwtToken()}`, | |
| 95 | + }; | |
| 96 | + } | |
| 97 | + return { | |
| 98 | + ...options, | |
| 99 | + sources: [{ type: getVideoTypeByUrl(exampleVideoPlay), src: exampleVideoPlay }], | |
| 100 | + } as VideoJsPlayerOptions; | |
| 101 | + }); | |
| 85 | 102 | instance?.play(); |
| 86 | 103 | }; |
| 87 | 104 | |
| ... | ... | @@ -103,7 +120,12 @@ |
| 103 | 120 | <template> |
| 104 | 121 | <main class="w-full h-full flex flex-col justify-center items-center p-2"> |
| 105 | 122 | <Spin :spinning="loading" wrapper-class-name="video-spin"> |
| 106 | - <BasicVideoPlay ref="basicVideoPlayEl" :options="getOptions" :with-token="withToken" /> | |
| 123 | + <BasicVideoPlay | |
| 124 | + ref="basicVideoPlayEl" | |
| 125 | + :options="getOptions" | |
| 126 | + :with-token="withToken" | |
| 127 | + :immediateInitOnMounted="false" | |
| 128 | + /> | |
| 107 | 129 | </Spin> |
| 108 | 130 | </main> |
| 109 | 131 | </template> | ... | ... |