From 7db888ef570230c5bc8b9e382514eeb5143a18dc Mon Sep 17 00:00:00 2001 From: xiangpei <xiangpei@timesnew.cn> Date: 星期二, 27 五月 2025 10:50:49 +0800 Subject: [PATCH] 视频播放记录保存 --- pages/tabbar/index/home.vue | 94 ++++++++++++++++++++++++++++++++-------------- 1 files changed, 65 insertions(+), 29 deletions(-) diff --git a/pages/tabbar/index/home.vue b/pages/tabbar/index/home.vue index 0fb56be..c42d93d 100644 --- a/pages/tabbar/index/home.vue +++ b/pages/tabbar/index/home.vue @@ -13,7 +13,7 @@ <view class="play-icon" @click="togglePlay(index)" - v-if="!currentVideoIsPlaying" + v-if="currentVideoIsPlaying != null && !currentVideoIsPlaying" > <image src="/static/video/play.png" style="width: 45px;height: 45px" mode="aspectFit"></image> </view> @@ -26,10 +26,11 @@ :loop="true" :object-fit="item.objectFit" class="video-item" - @play="onPlay(index)" + @play="onPlay(item.id, index)" @pause="onPause(index)" @ended="onEnded(index)" @click="togglePlay(index)" + @timeupdate="onTimeUpdate($event)" ></video> <!-- 鎮寕鍟嗗搧閾炬帴灞� --> @@ -95,26 +96,32 @@ </template> <script> -import { getRecommendVideos } from "@/api/video.js"; +import { getRecommendVideos, savePlayRecord } from "@/api/video.js"; import { changeCollect } from "@/api/collect.js"; export default { data() { return { - currentVideoIsPlaying: false, // 褰撳墠瑙嗛鏄惁姝e湪鎾斁 - isFullScreen: false, - windowHeight: 0, - currentIndex: 0, // 褰撳墠鎾斁鐨勮棰戠储寮� - videoList: [ + playRecord: { + videoId: null, + viewDuration: 0, // 杩欎釜瑙嗛鎬诲叡瑙傜湅浜嗗涔� + playAt: 0 ,// 杩欎釜瑙嗛鎾斁鍒板摢浜� + startPlayTime: 0 // 杩欎釜瑙嗛浠庝粈涔堟椂鍊欏紑濮嬫挱鏀剧殑 + }, + currentVideoIsPlaying: null, // 褰撳墠瑙嗛鏄惁姝e湪鎾斁 + isFullScreen: false, + windowHeight: 0, + currentIndex: 0, // 褰撳墠鎾斁鐨勮棰戠储寮� + videoList: [ - ], // 瑙嗛鍒楄〃鏁版嵁 - videoContexts: [], // 瑙嗛涓婁笅鏂囧璞¢泦鍚� - loading: false, // 鏄惁姝e湪鍔犺浇 - page: 1, // 褰撳墠椤电爜 - pageSize: 10 // 姣忛〉鏁伴噺 + ], // 瑙嗛鍒楄〃鏁版嵁 + videoContexts: [], // 瑙嗛涓婁笅鏂囧璞¢泦鍚� + loading: false, // 鏄惁姝e湪鍔犺浇 + page: 1, // 褰撳墠椤电爜 + pageSize: 10 // 姣忛〉鏁伴噺 } }, onShow() { - this.loadVideos(); + // this.loadVideos(); }, onLoad() { this.loadVideos(); @@ -155,18 +162,21 @@ // 婊戝姩鍒囨崲瑙嗛 onSwiperChange(e) { - const oldIndex = this.currentIndex; - this.currentIndex = e.detail.current; - - // 鏆傚仠涓婁竴涓棰� - if (this.videoContexts[oldIndex]) { - this.videoContexts[oldIndex].pause(); - } - - // 鎾斁褰撳墠瑙嗛 - if (this.videoContexts[this.currentIndex]) { - this.videoContexts[this.currentIndex].play(); - } + // 淇濆瓨涓婁竴涓棰戠殑鎾斁璁板綍 + this.savePlayRecord() + const oldIndex = this.currentIndex; + console.log("瑙嗛涓婁笅鏂�",this.videoContexts[oldIndex]); + this.currentIndex = e.detail.current; + + // 鏆傚仠涓婁竴涓棰� + if (this.videoContexts[oldIndex]) { + this.videoContexts[oldIndex].pause(); + } + + // 鎾斁褰撳墠瑙嗛 + if (this.videoContexts[this.currentIndex]) { + this.videoContexts[this.currentIndex].play(); + } }, // 鏀惰棌/鍙栨秷鏀惰棌 @@ -200,8 +210,13 @@ } }, // 瑙嗛鎾斁浜嬩欢 - onPlay(index) { + onPlay(id, index) { this.currentVideoIsPlaying = true; + this.playRecord.videoId = id; + // 娌″垵濮嬪寲鎵嶈祴鍊硷紝鍥犱负涓�涓棰戦噸澶嶆挱鏀緊nPlay浼氶噸澶嶈Е鍙� + if(this.playRecord.startPlayTime === 0) { + this.playRecord.startPlayTime = Date.now(); + } }, // 瑙嗛鏆傚仠浜嬩欢 @@ -211,8 +226,29 @@ // 瑙嗛缁撴潫浜嬩欢 onEnded(index) { - this.currentVideoIsPlaying = false; - } + // this.currentVideoIsPlaying = false; + }, + + // 璁板綍鎾斁鏃堕暱 + onTimeUpdate(e) { + this.playRecord.playAt = e.detail.currentTime + }, + + // 淇濆瓨鎾斁璁板綍 + async savePlayRecord() { + const data = { + videoId: this.playRecord.videoId, + viewDuration: Date.now() - this.playRecord.startPlayTime, + playAt: this.playRecord.playAt + } + this.playRecord = { + videoId: null, + viewDuration: 0, // 杩欎釜瑙嗛鎬诲叡瑙傜湅浜嗗涔� + playAt: 0 ,// 杩欎釜瑙嗛鎾斁鍒板摢浜� + startPlayTime: 0 // 杩欎釜瑙嗛浠庝粈涔堟椂鍊欏紑濮嬫挱鏀剧殑 + } + savePlayRecord(data) + } } } </script> -- Gitblit v1.8.0