Showing
2 changed files
with
51 additions
and
32 deletions
| ... | @@ -60,43 +60,58 @@ export default { | ... | @@ -60,43 +60,58 @@ 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(); |
| 70 | - var mp = new MuiPlayer({ | ||
| 71 | - container: '#mui-player-' + this.item.id, | ||
| 72 | - title: this.item.title, | ||
| 73 | - src: this.item.video, | ||
| 74 | - poster: this.item.cover ? this.item.cover : DEFAULT_COVER, | ||
| 75 | - // poster: DEFAULT_COVER, | ||
| 76 | - autoFit: false, | ||
| 77 | - videoAttribute: [ // 声明启用同层播放, 不让会自动全屏播放 | ||
| 78 | - {attrKey:'webkit-playsinline',attrValue:'webkit-playsinline'}, | ||
| 79 | - {attrKey:'playsinline',attrValue:'playsinline'}, | ||
| 80 | - {attrKey:'x5-video-player-type',attrValue:'h5-page'}, | ||
| 81 | - ] | ||
| 82 | - }) | ||
| 83 | - this.detail = _.cloneDeep(this.item); | ||
| 84 | - // 传回数据 | ||
| 85 | - this.$emit('on-click', this.detail); | ||
| 86 | - this.mp = mp; | ||
| 87 | - var video = mp.video(); | ||
| 88 | - // 监听原生video事件 | ||
| 89 | - var _this = this; | ||
| 90 | - video && video.addEventListener('play', function (event) { | ||
| 91 | - _this.handleAction('play', _this.item.id) | ||
| 92 | - }); | ||
| 93 | - }, 500) | ||
| 94 | // 配置16:9高度比 | 72 | // 配置16:9高度比 |
| 95 | const width = document.getElementById('mui-player-' + this.item.id).clientWidth; | 73 | const width = document.getElementById('mui-player-' + this.item.id).clientWidth; |
| 96 | const height = (width * 9) / 16; | 74 | const height = (width * 9) / 16; |
| 97 | document.getElementById('mui-player-' + this.item.id).height = height; | 75 | document.getElementById('mui-player-' + this.item.id).height = height; |
| 98 | }, | 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 | + }, | ||
| 99 | methods: { | 86 | methods: { |
| 87 | + initPlayer() { | ||
| 88 | + if (!this.item || !this.item.id || !this.item.video) return; | ||
| 89 | + this.initTimer = setTimeout(() => { | ||
| 90 | + var mp = new MuiPlayer({ | ||
| 91 | + container: '#mui-player-' + this.item.id, | ||
| 92 | + title: this.item.title, | ||
| 93 | + src: this.item.video, | ||
| 94 | + poster: this.item.cover ? this.item.cover : DEFAULT_COVER, | ||
| 95 | + // poster: DEFAULT_COVER, | ||
| 96 | + autoFit: false, | ||
| 97 | + videoAttribute: [ // 声明启用同层播放, 不让会自动全屏播放 | ||
| 98 | + {attrKey:'webkit-playsinline',attrValue:'webkit-playsinline'}, | ||
| 99 | + {attrKey:'playsinline',attrValue:'playsinline'}, | ||
| 100 | + {attrKey:'x5-video-player-type',attrValue:'h5-page'}, | ||
| 101 | + ] | ||
| 102 | + }) | ||
| 103 | + this.detail = _.cloneDeep(this.item); | ||
| 104 | + // 传回数据 | ||
| 105 | + this.$emit('on-click', this.detail); | ||
| 106 | + this.mp = mp; | ||
| 107 | + var video = mp.video(); | ||
| 108 | + // 监听原生video事件 | ||
| 109 | + var _this = this; | ||
| 110 | + video && video.addEventListener('play', function () { | ||
| 111 | + _this.handleAction('play', _this.item.id) | ||
| 112 | + }); | ||
| 113 | + }, 0) | ||
| 114 | + } | ||
| 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 | }) | ... | ... |
-
Please register or login to post a comment