|
|
@@ -0,0 +1,409 @@
|
|
|
+// pages/video/detail.js
|
|
|
+import { getVideoDetail, getRealVideoUrl, postWithCommonParams, getRelatedVideos } from '../../api/api'
|
|
|
+import { rnd } from '../../utils/util'
|
|
|
+
|
|
|
+Page({
|
|
|
+ data: {
|
|
|
+ videoId: '',
|
|
|
+ videoInfo: null,
|
|
|
+ videoDetail:null,
|
|
|
+ episodeList: [],
|
|
|
+ currentEpisodeIndex: 0,
|
|
|
+ currentEpisode: null, // 当前播放的剧集
|
|
|
+ isPlaying: false,
|
|
|
+ isLoading: true,
|
|
|
+ title: '',
|
|
|
+ scrollHeight: 0,
|
|
|
+ windowHeight: 0,
|
|
|
+ realVideoUrl: '', // 存储真实的视频播放地址
|
|
|
+ videoStatus: 0, // 0 未播放 1 播放中 2 暂停
|
|
|
+ duration: 0, // 视频总时长
|
|
|
+ currentTime: 0, // 当前播放时间
|
|
|
+ relatedVideos: []
|
|
|
+ },
|
|
|
+
|
|
|
+ onLoad: function (options) {
|
|
|
+ const videoId = parseInt(options.id);
|
|
|
+ const videoTitle = options.title ? decodeURIComponent(options.title) : '';
|
|
|
+
|
|
|
+ console.log('加载视频详情页,ID:', videoId, '标题:', videoTitle);
|
|
|
+
|
|
|
+ // 获取系统信息
|
|
|
+ this.getSystemInfo();
|
|
|
+
|
|
|
+ // 如果有传递标题,则直接使用
|
|
|
+ if (videoTitle) {
|
|
|
+ this.setData({
|
|
|
+ title: videoTitle,
|
|
|
+ videoId: videoId,
|
|
|
+ isLoading: true
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ this.setData({
|
|
|
+ videoId: videoId,
|
|
|
+ isLoading: true
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ this.loadVideoDetail(videoId);
|
|
|
+ },
|
|
|
+
|
|
|
+ // 获取系统信息
|
|
|
+ getSystemInfo() {
|
|
|
+ const systemInfo = wx.getSystemInfoSync();
|
|
|
+ this.setData({
|
|
|
+ windowHeight: systemInfo.windowHeight
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ // 加载视频详情
|
|
|
+ async loadVideoDetail(id) {
|
|
|
+ try {
|
|
|
+ wx.showLoading({
|
|
|
+ title: '加载中...',
|
|
|
+ mask: true
|
|
|
+ });
|
|
|
+
|
|
|
+ // 构建请求参数
|
|
|
+ const params = {
|
|
|
+ id,
|
|
|
+ tag_id: 2072
|
|
|
+ };
|
|
|
+
|
|
|
+ const videoDetail = await getVideoDetail(params);
|
|
|
+ console.log('视频详情数据:', videoDetail);
|
|
|
+
|
|
|
+ if (videoDetail) {
|
|
|
+ // 处理视频信息
|
|
|
+ const formattedVideoInfo = {
|
|
|
+ id: videoDetail.id,
|
|
|
+ title: videoDetail.title || this.data.title,
|
|
|
+ cover: videoDetail.cover_image,
|
|
|
+ playCount: videoDetail.views || '0',
|
|
|
+ playUV: videoDetail.play_uv || '0',
|
|
|
+ videoUrl: '', // 暂时为空,等待获取真实播放地址
|
|
|
+ description: videoDetail.description || '暂无简介',
|
|
|
+ tags: videoDetail.tag_ids || [],
|
|
|
+ likeCount: videoDetail.like || '0',
|
|
|
+ shareCount: videoDetail.share || '0',
|
|
|
+ isCollect: videoDetail.is_collect === 1,
|
|
|
+ video_type: videoDetail.video_type, // 保存原始视频类型
|
|
|
+ tag_ids: videoDetail.tag_ids // 保存标签ID列表
|
|
|
+ };
|
|
|
+
|
|
|
+ this.setData({
|
|
|
+ videoInfo: formattedVideoInfo,
|
|
|
+ title: formattedVideoInfo.title,
|
|
|
+ isLoading: false
|
|
|
+ });
|
|
|
+
|
|
|
+ // 获取真实播放地址
|
|
|
+ this.getVideoPlayUrl(videoDetail);
|
|
|
+
|
|
|
+ // 加载相关视频
|
|
|
+ this.loadRelatedVideos(videoDetail.tag_ids);
|
|
|
+ } else {
|
|
|
+ this.setData({
|
|
|
+ isLoading: false
|
|
|
+ });
|
|
|
+
|
|
|
+ wx.showToast({
|
|
|
+ title: '获取视频详情失败',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取视频详情失败:', error);
|
|
|
+
|
|
|
+ this.setData({
|
|
|
+ isLoading: false
|
|
|
+ });
|
|
|
+
|
|
|
+ wx.showToast({
|
|
|
+ title: '获取视频详情失败',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ } finally {
|
|
|
+ wx.hideLoading();
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ // 获取视频播放地址
|
|
|
+ async getVideoPlayUrl(videoDetail) {
|
|
|
+
|
|
|
+
|
|
|
+ console.log(`videoDetail`,videoDetail)
|
|
|
+ try {
|
|
|
+ // 根据视频类型进行处理
|
|
|
+ switch (videoDetail.video_type) {
|
|
|
+ case 3: // OAS视频
|
|
|
+ // 获取真实播放地址
|
|
|
+ const tagId = videoDetail.tag_ids && videoDetail.tag_ids.length > 0 ? videoDetail.tag_ids[0] : 2072;
|
|
|
+ let parseRes = await getRealVideoUrl(videoDetail.id, tagId);
|
|
|
+
|
|
|
+ console.log('parseRes',parseRes)
|
|
|
+
|
|
|
+ // 默认视频为免费视频
|
|
|
+ videoDetail.lock = false
|
|
|
+ // 标记暂停插屏是否显示过
|
|
|
+ videoDetail.pauseInsertAd = false
|
|
|
+
|
|
|
+
|
|
|
+ // 默认视频不显示插屏
|
|
|
+ videoDetail.insertAd = false
|
|
|
+ videoDetail.adType = 'video'
|
|
|
+ videoDetail.likeNum = rnd(101, 209) / 10
|
|
|
+ videoDetail.like = false
|
|
|
+ videoDetail.shareNum = rnd(51, 99) / 10
|
|
|
+ if (videoDetail.video_type !== 4) {
|
|
|
+ videoDetail.playUrl = parseRes.url_info[0].url
|
|
|
+ }
|
|
|
+ if (videoDetail.is_top === 1) {
|
|
|
+ videoDetail.top = true
|
|
|
+ }
|
|
|
+ this.setData({
|
|
|
+ isLoading:false,
|
|
|
+ videoDetail: videoDetail
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 4: // 微信视频
|
|
|
+ // 直接使用video_vid
|
|
|
+ this.setData({
|
|
|
+ 'videoInfo.videoUrl': videoDetail.video_vid
|
|
|
+ });
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ // 默认使用url字段
|
|
|
+ if (videoDetail.url) {
|
|
|
+ this.setData({
|
|
|
+ 'videoInfo.videoUrl': videoDetail.url
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ throw new Error('不支持的视频类型');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取视频播放地址失败:', error);
|
|
|
+ wx.showToast({
|
|
|
+ title: '无法获取视频播放地址',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ // 切换剧集
|
|
|
+ switchEpisode(e) {
|
|
|
+ const index = e.currentTarget.dataset.index;
|
|
|
+ if (index === this.data.currentEpisodeIndex) return;
|
|
|
+
|
|
|
+ const episode = this.data.episodeList[index];
|
|
|
+ if (!episode) return;
|
|
|
+
|
|
|
+ this.setData({
|
|
|
+ currentEpisodeIndex: index,
|
|
|
+ currentEpisode: episode,
|
|
|
+ isPlaying: true
|
|
|
+ });
|
|
|
+
|
|
|
+ // 获取视频实例并播放
|
|
|
+ const videoContext = wx.createVideoContext('myVideo');
|
|
|
+ videoContext.play();
|
|
|
+ },
|
|
|
+
|
|
|
+ // 视频播放事件
|
|
|
+ onVideoPlay() {
|
|
|
+ this.setData({
|
|
|
+ isPlaying: true,
|
|
|
+ videoStatus: 1
|
|
|
+ });
|
|
|
+
|
|
|
+ // 可以添加播放埋点
|
|
|
+ console.log('视频开始播放:', this.data.videoInfo.id);
|
|
|
+ },
|
|
|
+
|
|
|
+ // 视频暂停事件
|
|
|
+ onVideoPause() {
|
|
|
+ this.setData({
|
|
|
+ isPlaying: false,
|
|
|
+ videoStatus: 2
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ // 视频播放结束事件
|
|
|
+ onVideoEnd() {
|
|
|
+ this.setData({
|
|
|
+ isPlaying: false,
|
|
|
+ videoStatus: 0
|
|
|
+ });
|
|
|
+
|
|
|
+ // 可以添加结束埋点
|
|
|
+ console.log('视频播放结束:', this.data.videoInfo.id);
|
|
|
+ },
|
|
|
+
|
|
|
+ // 视频时间更新事件
|
|
|
+ onVideoUpdate(e) {
|
|
|
+ const { currentTime, duration } = e.detail;
|
|
|
+ this.setData({
|
|
|
+ currentTime,
|
|
|
+ duration
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ // 视频元数据加载完成
|
|
|
+ onVideoLoaded(e) {
|
|
|
+ console.log('视频元数据加载完成', e.detail);
|
|
|
+ },
|
|
|
+
|
|
|
+ // 点击视频控制区
|
|
|
+ onClickVideoControls() {
|
|
|
+ if (this.data.videoStatus === 1) {
|
|
|
+ // 当前正在播放,暂停视频
|
|
|
+ const videoContext = wx.createVideoContext('myVideo');
|
|
|
+ videoContext.pause();
|
|
|
+ } else {
|
|
|
+ // 当前已暂停,开始播放
|
|
|
+ const videoContext = wx.createVideoContext('myVideo');
|
|
|
+ videoContext.play();
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ // 点击相关推荐视频
|
|
|
+ onRelatedVideoTap(e) {
|
|
|
+ const videoId = e.currentTarget.dataset.id;
|
|
|
+ const video = this.data.relatedVideos.find(item => item.id === videoId);
|
|
|
+
|
|
|
+ if (!video) {
|
|
|
+ wx.showToast({
|
|
|
+ title: '视频信息获取失败',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 显示加载中提示
|
|
|
+ wx.showLoading({
|
|
|
+ title: '加载中...',
|
|
|
+ mask: true
|
|
|
+ });
|
|
|
+
|
|
|
+ // 将标题进行URI编码处理
|
|
|
+ const encodedTitle = encodeURIComponent(video.title || '');
|
|
|
+
|
|
|
+ // 跳转到视频详情页
|
|
|
+ wx.navigateTo({
|
|
|
+ url: `/pages/video/detail?id=${videoId}&title=${encodedTitle}`,
|
|
|
+ success: () => {
|
|
|
+ wx.hideLoading();
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('跳转失败:', err);
|
|
|
+ wx.hideLoading();
|
|
|
+ wx.showToast({
|
|
|
+ title: '跳转失败',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ // 返回上一页
|
|
|
+ goBack() {
|
|
|
+ wx.navigateBack();
|
|
|
+ },
|
|
|
+
|
|
|
+ // 分享功能
|
|
|
+ onShare() {
|
|
|
+ wx.showShareMenu({
|
|
|
+ withShareTicket: true,
|
|
|
+ menus: ['shareAppMessage', 'shareTimeline']
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ // 分享给朋友
|
|
|
+ onShareAppMessage() {
|
|
|
+ const videoInfo = this.data.videoInfo;
|
|
|
+ if (!videoInfo) return {};
|
|
|
+
|
|
|
+ return {
|
|
|
+ title: videoInfo.title,
|
|
|
+ path: `/pages/video/detail?id=${videoInfo.id}`,
|
|
|
+ imageUrl: videoInfo.cover
|
|
|
+ };
|
|
|
+ },
|
|
|
+
|
|
|
+ // 分享到朋友圈
|
|
|
+ onShareTimeline() {
|
|
|
+ const videoInfo = this.data.videoInfo;
|
|
|
+ if (!videoInfo) return {};
|
|
|
+
|
|
|
+ return {
|
|
|
+ title: videoInfo.title,
|
|
|
+ query: `id=${videoInfo.id}`,
|
|
|
+ imageUrl: videoInfo.cover
|
|
|
+ };
|
|
|
+ },
|
|
|
+
|
|
|
+ // 收藏视频
|
|
|
+ toggleCollect() {
|
|
|
+ const isCollect = !this.data.videoInfo.isCollect;
|
|
|
+
|
|
|
+ this.setData({
|
|
|
+ 'videoInfo.isCollect': isCollect
|
|
|
+ });
|
|
|
+
|
|
|
+ wx.showToast({
|
|
|
+ title: isCollect ? '收藏成功' : '已取消收藏',
|
|
|
+ icon: 'success'
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ // 点赞视频
|
|
|
+ likeVideo() {
|
|
|
+ const currentLikes = parseInt(this.data.videoInfo.likeCount) || 0;
|
|
|
+
|
|
|
+ this.setData({
|
|
|
+ 'videoInfo.likeCount': (currentLikes + 1).toString()
|
|
|
+ });
|
|
|
+
|
|
|
+ wx.showToast({
|
|
|
+ title: '点赞成功',
|
|
|
+ icon: 'success'
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ // 滚动事件
|
|
|
+ onScroll(e) {
|
|
|
+ this.setData({
|
|
|
+ scrollHeight: e.detail.scrollTop
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ // 加载相关视频
|
|
|
+ async loadRelatedVideos(tagIds) {
|
|
|
+ try {
|
|
|
+ // 使用第一个标签ID获取相关视频
|
|
|
+ const tagId = tagIds && tagIds.length > 0 ? tagIds[0] : 2250;
|
|
|
+ const relatedVideos = await getRelatedVideos({ tag_id: tagId });
|
|
|
+
|
|
|
+ // 处理相关视频数据
|
|
|
+ const formattedVideos = relatedVideos.map(video => ({
|
|
|
+ id: video.id,
|
|
|
+ title: video.title,
|
|
|
+ cover_image: video.cover_image,
|
|
|
+ playCount: video.views || '0',
|
|
|
+ likeCount: video.like || '0',
|
|
|
+ shareCount: video.share || '0'
|
|
|
+ }));
|
|
|
+
|
|
|
+ this.setData({
|
|
|
+ relatedVideos: formattedVideos
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取相关视频失败:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+})
|