汪安军

视频详情页 - 修复异步时机导致报错视频地址为空的问题

...@@ -60,13 +60,33 @@ export default { ...@@ -60,13 +60,33 @@ export default {
60 return { 60 return {
61 detail: { 61 detail: {
62 mp: '' 62 mp: ''
63 - } 63 + },
64 + initTimer: null,
65 + mp: null
64 } 66 }
65 }, 67 },
66 created() { 68 created() {
67 }, 69 },
68 mounted() { 70 mounted() {
69 - setTimeout(() => { 71 + this.initPlayer();
72 + // 配置16:9高度比
73 + const width = document.getElementById('mui-player-' + this.item.id).clientWidth;
74 + const height = (width * 9) / 16;
75 + document.getElementById('mui-player-' + this.item.id).height = height;
76 + },
77 + beforeUnmount() {
78 + if (this.initTimer) {
79 + clearTimeout(this.initTimer);
80 + this.initTimer = null;
81 + }
82 + if (this.mp && typeof this.mp.destroy === 'function') {
83 + this.mp.destroy();
84 + }
85 + },
86 + methods: {
87 + initPlayer() {
88 + if (!this.item || !this.item.id || !this.item.video) return;
89 + this.initTimer = setTimeout(() => {
70 var mp = new MuiPlayer({ 90 var mp = new MuiPlayer({
71 container: '#mui-player-' + this.item.id, 91 container: '#mui-player-' + this.item.id,
72 title: this.item.title, 92 title: this.item.title,
...@@ -87,16 +107,11 @@ export default { ...@@ -87,16 +107,11 @@ export default {
87 var video = mp.video(); 107 var video = mp.video();
88 // 监听原生video事件 108 // 监听原生video事件
89 var _this = this; 109 var _this = this;
90 - video && video.addEventListener('play', function (event) { 110 + video && video.addEventListener('play', function () {
91 _this.handleAction('play', _this.item.id) 111 _this.handleAction('play', _this.item.id)
92 }); 112 });
93 - }, 500) 113 + }, 0)
94 - // 配置16:9高度比 114 + }
95 - const width = document.getElementById('mui-player-' + this.item.id).clientWidth;
96 - const height = (width * 9) / 16;
97 - document.getElementById('mui-player-' + this.item.id).height = height;
98 - },
99 - methods: {
100 } 115 }
101 } 116 }
102 </script> 117 </script>
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 </div> 14 </div>
15 15
16 <div class="video-player"> 16 <div class="video-player">
17 - <video-detail :item="videoInfo" @on-click="onVideoDetail" /> 17 + <video-detail v-if="videoInfo.video" :item="videoInfo" @on-click="onVideoDetail" />
18 </div> 18 </div>
19 19
20 <div class="video-main"> 20 <div class="video-main">
...@@ -33,8 +33,8 @@ ...@@ -33,8 +33,8 @@
33 </van-tab> --> 33 </van-tab> -->
34 <van-tab :title="'留言 ' + comment_num" :title-style="tabClass"> 34 <van-tab :title="'留言 ' + comment_num" :title-style="tabClass">
35 <!-- <router-view ref="childRef" name="comment" /> --> 35 <!-- <router-view ref="childRef" name="comment" /> -->
36 - <video-detail-comment v-if="videoInfo" ref="childRef" :info="videoInfo" /> 36 + <video-detail-comment v-if="videoInfo.id" ref="childRef" :info="videoInfo" />
37 - <donate-bar v-if="videoInfo" :perf-id="videoInfo.perf_id">为TA助力</donate-bar> 37 + <donate-bar v-if="videoInfo.id" :perf-id="videoInfo.perf_id">为TA助力</donate-bar>
38 </van-tab> 38 </van-tab>
39 </van-tabs> 39 </van-tabs>
40 </van-col> 40 </van-col>
...@@ -109,7 +109,7 @@ const tabClass = { ...@@ -109,7 +109,7 @@ const tabClass = {
109 const store = mainStore(); 109 const store = mainStore();
110 const { comment_num } = storeToRefs(store); 110 const { comment_num } = storeToRefs(store);
111 111
112 -const videoInfo = ref(''); 112 +const videoInfo = ref({});
113 const childRef = ref(null); 113 const childRef = ref(null);
114 onMounted(async () => { 114 onMounted(async () => {
115 // 检查$route.path判断tab默认值 115 // 检查$route.path判断tab默认值
...@@ -117,7 +117,9 @@ onMounted(async () => { ...@@ -117,7 +117,9 @@ onMounted(async () => {
117 /** 117 /**
118 * 获取视频详情 118 * 获取视频详情
119 */ 119 */
120 - const { data } = await prodInfoAPI({ prod_id: $route.query.prod_id }); 120 + const res = await prodInfoAPI({ prod_id: $route.query.prod_id });
121 + if (!res) return;
122 + const { data } = res;
121 videoInfo.value = data; 123 videoInfo.value = data;
122 // 动态调整留言数量 124 // 动态调整留言数量
123 store.changeCommentNum(data.comment_num); 125 store.changeCommentNum(data.comment_num);
...@@ -141,7 +143,9 @@ const onVideoDetail = (v) => { ...@@ -141,7 +143,9 @@ const onVideoDetail = (v) => {
141 143
142 onBeforeRouteLeave(async () => { 144 onBeforeRouteLeave(async () => {
143 // 缓存作品信息,给其他页使用 145 // 缓存作品信息,给其他页使用
144 - const { data } = await prodInfoAPI({ prod_id: $route.query.prod_id }); 146 + const res = await prodInfoAPI({ prod_id: $route.query.prod_id });
147 + if (!res) return;
148 + const { data } = res;
145 videoInfo.value = data; 149 videoInfo.value = data;
146 store.changeVideoDetail(videoInfo.value); 150 store.changeVideoDetail(videoInfo.value);
147 }) 151 })
......