| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- // pages/video/detail.js
- import {getVideoDetail, getRealVideoUrl, postWithCommonParams, getRelatedVideos, getUuid} from '../../api/api'
- import {rnd} from '../../utils/util'
- import {apple} from "../../config/config";
- 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: [],
- tagId: 0,
- share_count: 1,
- scene: 0, // 场景值
- },
- onLoad: function (options) {
- const videoId = parseInt(options.id);
- const videoTitle = options.title ? decodeURIComponent(options.title) : '';
- const tagId = parseInt(options.tag_id);
- this.setData({
- scene: wx.getStorageSync('scene') || 1001
- })
- console.log('加载视频详情页,ID:', videoId, '标题:', videoTitle);
- // 获取系统信息
- this.getSystemInfo();
- // 如果有传递标题,则直接使用
- if (videoTitle) {
- this.setData({
- title: videoTitle,
- videoId: videoId,
- isLoading: true,
- tagId: tagId
- });
- } else {
- this.setData({
- videoId: videoId,
- isLoading: true,
- tagId: tagId
- });
- }
- this.loadVideoDetail(videoId);
- },
- onShow() {
- const pages = getCurrentPages();
- const currentPage = pages[pages.length - 1];
- const options = currentPage.options;
- console.log('scene', this.data.scene)
- if (+options.share_count === 1 && this.data.share_count === 1) {
- this.setData({
- share_count: 2
- })
- if(this.data.scene === 1007 || this.data.scene === 1008){
- this.goToBookDetail()
- }
- } else if (+options.share_count === 1 && this.data.share_count === 2) {
- this.setData({
- share_count: 3
- })
- }
- },
- /**
- * 获取uuid
- */
- getUUID() {
- return new Promise((resolve, reject) => {
- wx.login({
- success: res => {
- let body = {
- code: res.code,
- source_type: 1,
- source_id: ''
- }
- getUuid(body).then(async res => {
- resolve(res.uuid)
- }).catch((err) => {
- reject(err)
- })
- }
- })
- })
- },
- // 获取系统信息
- getSystemInfo() {
- const systemInfo = wx.getSystemInfoSync();
- this.setData({
- windowHeight: systemInfo.windowHeight
- });
- },
- // 加载视频详情
- async loadVideoDetail(id) {
- try {
- wx.showLoading({
- title: '加载中...',
- mask: true
- });
- //getUuid
- if (!getApp().globalData.uuid) {
- await this.getUUID().then(uuid => {
- getApp().globalData.uuid = uuid
- })
- }
- // 构建请求参数
- const params = {
- id,
- tag_id: this.data.tagId,
- uuid: getApp().globalData.uuid
- };
- 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) {
- console.log(e.currentTarget.dataset)
- const videoId = e.currentTarget.dataset.id;
- const video = this.data.relatedVideos.find(item => item.id === videoId);
- // console.log('video',video)
- if (!video) {
- wx.showToast({
- title: '视频信息获取失败',
- icon: 'none'
- });
- return;
- }
- const tag_id = video.tag_id;
- // console.log('tag_id',tag_id)
- // 显示加载中提示
- wx.showLoading({
- title: '加载中...',
- mask: true
- });
- // 将标题进行URI编码处理
- const encodedTitle = encodeURIComponent(video.title || '');
- // 跳转到视频详情页
- wx.navigateTo({
- url: `/pages/video/detail?id=${videoId}&title=${encodedTitle}&tag_id=${tag_id}`,
- success: () => {
- wx.hideLoading();
- },
- fail: (err) => {
- console.error('跳转失败:', err);
- wx.hideLoading();
- wx.showToast({
- title: '跳转失败',
- icon: 'none'
- });
- }
- });
- },
- // 跳转到书籍详情
- goToBookDetail() {
- getApp().globalData.isShowDialog = true
- goToBookDetail({
- bookId: 7266,
- wxBookId: 'A1Hcfv456vGtMYxQjxUm8KNdJ8',
- chapterId: null
- });
- },
- // 返回上一页
- goBack() {
- wx.navigateBack({
- delta: 1
- }
- );
- // wx.redirectTo({
- // url: '/pages/video/video',
- // });
- console.log('返回上一页')
- },
- // 分享按钮点击事件
- onShareTap(e) {
- // 阻止事件冒泡
- e.stopPropagation();
- },
- // 分享给朋友
- onShareAppMessage(e) {
- // 如果是从推荐视频分享
- if (e.target && e.target.dataset.video) {
- const video = e.target.dataset.video;
- return {
- title: video.title,
- path: `/pages/video/video?shared_video_id=${video.id}&title=${encodeURIComponent(video.title)}&tag_id=${video.tag_ids[0]}&share_count=1&type=share`,
- imageUrl: video.cover_image,
- success: function (res) {
- wx.showToast({
- title: '分享成功',
- icon: 'success',
- duration: 1500
- });
- },
- fail: function (res) {
- wx.showToast({
- title: '分享失败',
- icon: 'none',
- duration: 1500
- });
- }
- };
- }
- // 默认分享当前视频
- const videoInfo = this.data.videoInfo;
- return {
- title: videoInfo.title,
- path: `/pages/video/video?shared_video_id=${videoInfo.id}&title=${encodeURIComponent(videoInfo.title)}&tag_id=${videoInfo.tag_ids[0]}&share_count=1&type=share`,
- imageUrl: videoInfo.cover,
- success: function (res) {
- wx.showToast({
- title: '分享成功',
- icon: 'success',
- duration: 1500
- });
- },
- fail: function (res) {
- wx.showToast({
- title: '分享失败',
- icon: 'none',
- duration: 1500
- });
- }
- };
- },
- // 分享到朋友圈
- onShareTimeline() {
- const videoInfo = this.data.videoInfo;
- return {
- title: videoInfo.title,
- query: `shared_video_id=${videoInfo.id}&title=${encodeURIComponent(videoInfo.title)}&tag_id=${videoInfo.tag_ids[0]}&share_count=1&type=share`,
- 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;
- console.log("原始ID:", apple.ghid)
- const relatedVideos = await getRelatedVideos({tag_id: tagId, gh_id: apple.ghid});
- // console.log('推荐视频',relatedVideos)
- // 处理相关视频数据
- const formattedVideos = relatedVideos.map(video => ({
- id: video.id,
- title: video.title,
- tag_id: video.tag_ids[0],
- 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);
- }
- }
- })
|