Commit 2cf8f88e27797814a2a9ddc743c8ca11923124a1

Authored by xp.Huang
2 parents ebd6229a 31274845

Merge branch 'fix/data-board-video-component' into 'main_dev'

fix: 修复看板管理通过平台获取播放视频

See merge request yunteng/thingskit-front!840
@@ -2,17 +2,23 @@ @@ -2,17 +2,23 @@
2 import { isNumber } from 'lodash'; 2 import { isNumber } from 'lodash';
3 import videoJs, { VideoJsPlayer, VideoJsPlayerOptions } from 'video.js'; 3 import videoJs, { VideoJsPlayer, VideoJsPlayerOptions } from 'video.js';
4 import 'video.js/dist/video-js.css'; 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 import { useDesign } from '/@/hooks/web/useDesign'; 6 import { useDesign } from '/@/hooks/web/useDesign';
7 import { getJwtToken, getShareJwtToken } from '/@/utils/auth'; 7 import { getJwtToken, getShareJwtToken } from '/@/utils/auth';
8 import { isShareMode } from '/@/views/sys/share/hook'; 8 import { isShareMode } from '/@/views/sys/share/hook';
9 import 'videojs-flvjs-es6'; 9 import 'videojs-flvjs-es6';
10 const { prefixCls } = useDesign('basic-video-play'); 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 const emit = defineEmits<{ 23 const emit = defineEmits<{
18 (event: 'ready', instance?: Nullable<VideoJsPlayer>): void; 24 (event: 'ready', instance?: Nullable<VideoJsPlayer>): void;
@@ -68,8 +74,18 @@ @@ -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 onMounted(() => { 87 onMounted(() => {
72 - init(); 88 + props.immediateInitOnMounted && init();
73 }); 89 });
74 90
75 onUnmounted(() => { 91 onUnmounted(() => {
@@ -79,6 +95,7 @@ @@ -79,6 +95,7 @@
79 }); 95 });
80 96
81 defineExpose({ 97 defineExpose({
  98 + customInit,
82 reloadPlayer: init, 99 reloadPlayer: init,
83 getInstance: () => unref(videoPlayInstance), 100 getInstance: () => unref(videoPlayInstance),
84 }); 101 });
@@ -42,17 +42,17 @@ @@ -42,17 +42,17 @@
42 42
43 const handleGetVideoPlayUrl = async () => { 43 const handleGetVideoPlayUrl = async () => {
44 try { 44 try {
45 - const instance = unref(basicVideoPlayEl)?.getInstance();  
46 const { config } = props; 45 const { config } = props;
47 const { option } = config; 46 const { option } = config;
48 const { videoConfig, uuid } = option || {}; 47 const { videoConfig, uuid } = option || {};
49 if (!uuid) return; 48 if (!uuid) return;
50 const { url, id, accessMode } = videoConfig || {}; 49 const { url, id, accessMode } = videoConfig || {};
51 - const type = getVideoTypeByUrl(url!); 50 + let type = getVideoTypeByUrl(url!);
52 let playUrl = url; 51 let playUrl = url;
53 if (accessMode === AccessMode.Streaming && id) { 52 if (accessMode === AccessMode.Streaming && id) {
54 const { data: { url } = { url: '' } } = await getStreamingPlayUrl(id!); 53 const { data: { url } = { url: '' } } = await getStreamingPlayUrl(id!);
55 playUrl = url; 54 playUrl = url;
  55 + playUrl && (type = getVideoTypeByUrl(playUrl!));
56 } 56 }
57 57
58 if (isRtspProtocol(url!)) { 58 if (isRtspProtocol(url!)) {
@@ -60,10 +60,6 @@ @@ -60,10 +60,6 @@
60 const { visitorId } = result; 60 const { visitorId } = result;
61 playUrl = getFlvPlayUrl(playUrl!, visitorId); 61 playUrl = getFlvPlayUrl(playUrl!, visitorId);
62 withToken.value = true; 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 playSource.value = { 65 playSource.value = {
@@ -71,7 +67,17 @@ @@ -71,7 +67,17 @@
71 type, 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 instance?.play(); 81 instance?.play();
76 } finally { 82 } finally {
77 loading.value = false; 83 loading.value = false;
@@ -80,8 +86,19 @@ @@ -80,8 +86,19 @@
80 86
81 const handleSelectPreview = () => { 87 const handleSelectPreview = () => {
82 loading.value = false; 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 instance?.play(); 102 instance?.play();
86 }; 103 };
87 104
@@ -103,7 +120,12 @@ @@ -103,7 +120,12 @@
103 <template> 120 <template>
104 <main class="w-full h-full flex flex-col justify-center items-center p-2"> 121 <main class="w-full h-full flex flex-col justify-center items-center p-2">
105 <Spin :spinning="loading" wrapper-class-name="video-spin"> 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 </Spin> 129 </Spin>
108 </main> 130 </main>
109 </template> 131 </template>