detail.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. // pages/video/detail.js
  2. import { getVideoDetail, getRealVideoUrl, postWithCommonParams, getRelatedVideos, getUuid } from '../../api/api'
  3. import { rnd, SendEvent } from '../../utils/util'
  4. import { apple } from "../../config/config";
  5. const app = getApp();
  6. let interstitialAd = null
  7. let insertAdInterval = null
  8. Page({
  9. data: {
  10. videoId: '',
  11. videoInfo: null,
  12. videoDetail: null,
  13. episodeList: [],
  14. currentEpisodeIndex: 0,
  15. currentEpisode: null, // 当前播放的剧集
  16. isPlaying: false,
  17. isLoading: true,
  18. title: '',
  19. scrollHeight: 0,
  20. windowHeight: 0,
  21. realVideoUrl: '', // 存储真实的视频播放地址
  22. videoStatus: 0, // 0 未播放 1 播放中 2 暂停
  23. duration: 0, // 视频总时长
  24. currentTime: 0, // 当前播放时间
  25. relatedVideos: [],
  26. tagId: 0,
  27. share_count: 1,
  28. scene: 0, // 场景值
  29. },
  30. onLoad: function (options) {
  31. const videoId = parseInt(options.id);
  32. const videoTitle = options.title ? decodeURIComponent(options.title) : '';
  33. const tagId = parseInt(options.tag_id);
  34. this.setData({
  35. scene: wx.getStorageSync('scene') || 1001
  36. })
  37. SendEvent('show_video', {
  38. "video_id": videoId,
  39. "tag_id": tagId,
  40. "scene": this.data.scene,
  41. "uuid": app.globalData.uuid
  42. });
  43. console.log('加载视频详情页,ID:', videoId, '标题:', videoTitle);
  44. // 获取系统信息
  45. this.getSystemInfo();
  46. // 如果有传递标题,则直接使用
  47. if (videoTitle) {
  48. this.setData({
  49. title: videoTitle,
  50. videoId: videoId,
  51. isLoading: true,
  52. tagId: tagId
  53. });
  54. } else {
  55. this.setData({
  56. videoId: videoId,
  57. isLoading: true,
  58. tagId: tagId
  59. });
  60. }
  61. this.interstitalLoad();
  62. this.loadVideoDetail(videoId);
  63. app.globalData.isEnterVideoDetail = true;
  64. },
  65. onShow() {
  66. const pages = getCurrentPages();
  67. const currentPage = pages[pages.length - 1];
  68. const options = currentPage.options;
  69. console.log('scene', this.data.scene)
  70. if (+options.share_count === 1 && this.data.share_count === 1) {
  71. this.setData({
  72. share_count: 2
  73. })
  74. if ((this.data.scene === 1007 || this.data.scene === 1008)) {
  75. console.log('准备跳转novel')
  76. this.goToBookDetail()
  77. } else {
  78. console.log('没有跳转novel')
  79. }
  80. } else if (+options.share_count === 1 && this.data.share_count === 2) {
  81. this.setData({
  82. share_count: 3
  83. })
  84. }
  85. },
  86. /**
  87. * 获取uuid
  88. */
  89. getUUID() {
  90. return new Promise((resolve, reject) => {
  91. wx.login({
  92. success: res => {
  93. let body = {
  94. code: res.code,
  95. source_type: 1,
  96. source_id: ''
  97. }
  98. getUuid(body).then(async res => {
  99. resolve(res.uuid)
  100. }).catch((err) => {
  101. reject(err)
  102. })
  103. }
  104. })
  105. })
  106. },
  107. // 获取系统信息
  108. getSystemInfo() {
  109. const systemInfo = wx.getSystemInfoSync();
  110. this.setData({
  111. windowHeight: systemInfo.windowHeight
  112. });
  113. },
  114. // 加载视频详情
  115. async loadVideoDetail(id) {
  116. try {
  117. wx.showLoading({
  118. title: '加载中...',
  119. mask: true
  120. });
  121. //getUuid
  122. if (!getApp().globalData.uuid) {
  123. await this.getUUID().then(uuid => {
  124. getApp().globalData.uuid = uuid
  125. })
  126. }
  127. // 构建请求参数
  128. const params = {
  129. id,
  130. tag_id: this.data.tagId,
  131. uuid: getApp().globalData.uuid
  132. };
  133. const videoDetail = await getVideoDetail(params);
  134. console.log('视频详情数据:', videoDetail);
  135. if (videoDetail) {
  136. // 处理视频信息
  137. const formattedVideoInfo = {
  138. id: videoDetail.id,
  139. title: videoDetail.title || this.data.title,
  140. cover: videoDetail.cover_image,
  141. playCount: videoDetail.views || '0',
  142. playUV: videoDetail.play_uv || '0',
  143. videoUrl: '', // 暂时为空,等待获取真实播放地址
  144. description: videoDetail.description || '暂无简介',
  145. tags: videoDetail.tag_ids || [],
  146. likeCount: videoDetail.like || '0',
  147. shareCount: videoDetail.share || '0',
  148. isCollect: videoDetail.is_collect === 1,
  149. video_type: videoDetail.video_type, // 保存原始视频类型
  150. tag_ids: videoDetail.tag_ids // 保存标签ID列表
  151. };
  152. this.setData({
  153. videoInfo: formattedVideoInfo,
  154. title: formattedVideoInfo.title,
  155. isLoading: false
  156. });
  157. // 获取真实播放地址
  158. this.getVideoPlayUrl(videoDetail);
  159. this.interstitalEnterPageShow()
  160. // 加载相关视频
  161. this.loadRelatedVideos(videoDetail.tag_ids);
  162. } else {
  163. this.setData({
  164. isLoading: false
  165. });
  166. wx.showToast({
  167. title: '获取视频详情失败',
  168. icon: 'none'
  169. });
  170. }
  171. } catch (error) {
  172. console.error('获取视频详情失败:', error);
  173. this.setData({
  174. isLoading: false
  175. });
  176. wx.showToast({
  177. title: '获取视频详情失败',
  178. icon: 'none'
  179. });
  180. } finally {
  181. wx.hideLoading();
  182. }
  183. },
  184. // 获取视频播放地址
  185. async getVideoPlayUrl(videoDetail) {
  186. console.log(`videoDetail`, videoDetail)
  187. try {
  188. // 根据视频类型进行处理
  189. switch (videoDetail.video_type) {
  190. case 3: // OAS视频
  191. // 获取真实播放地址
  192. const tagId = videoDetail.tag_ids && videoDetail.tag_ids.length > 0 ? videoDetail.tag_ids[0] : 2072;
  193. let parseRes = await getRealVideoUrl(videoDetail.id, this.data.tagId); // 此处需要使用传入的tag_id,否则视频有多个tag_id时,会无法获取到真实播放地址
  194. console.log('parseRes', parseRes)
  195. // 默认视频为免费视频
  196. videoDetail.lock = false
  197. // 标记暂停插屏是否显示过
  198. videoDetail.pauseInsertAd = false
  199. // 默认视频不显示插屏
  200. videoDetail.insertAd = false
  201. videoDetail.adType = 'video'
  202. videoDetail.likeNum = rnd(101, 209) / 10
  203. videoDetail.like = false
  204. videoDetail.shareNum = rnd(51, 99) / 10
  205. if (videoDetail.video_type !== 4) {
  206. videoDetail.playUrl = parseRes.url_info[0].url
  207. }
  208. if (videoDetail.is_top === 1) {
  209. videoDetail.top = true
  210. }
  211. this.setData({
  212. isLoading: false,
  213. videoDetail: videoDetail
  214. })
  215. break;
  216. case 4: // 微信视频
  217. // 直接使用video_vid
  218. this.setData({
  219. 'videoInfo.videoUrl': videoDetail.video_vid
  220. });
  221. break;
  222. default:
  223. // 默认使用url字段
  224. if (videoDetail.url) {
  225. this.setData({
  226. 'videoInfo.videoUrl': videoDetail.url
  227. });
  228. } else {
  229. throw new Error('不支持的视频类型');
  230. }
  231. }
  232. } catch (error) {
  233. console.error('获取视频播放地址失败:', error);
  234. wx.showToast({
  235. title: '无法获取视频播放地址',
  236. icon: 'none'
  237. });
  238. }
  239. },
  240. // 切换剧集
  241. switchEpisode(e) {
  242. const index = e.currentTarget.dataset.index;
  243. if (index === this.data.currentEpisodeIndex) return;
  244. const episode = this.data.episodeList[index];
  245. if (!episode) return;
  246. this.setData({
  247. currentEpisodeIndex: index,
  248. currentEpisode: episode,
  249. isPlaying: true
  250. });
  251. // 获取视频实例并播放
  252. const videoContext = wx.createVideoContext('myVideo');
  253. videoContext.play();
  254. },
  255. // 视频播放事件
  256. onVideoPlay() {
  257. this.setData({
  258. isPlaying: true,
  259. videoStatus: 1
  260. });
  261. // 可以添加播放埋点
  262. console.log('视频开始播放:', this.data.videoInfo.id);
  263. },
  264. // 视频暂停事件
  265. onVideoPause() {
  266. this.setData({
  267. isPlaying: false,
  268. videoStatus: 2
  269. });
  270. console.log('视频暂停:', this.data.videoInfo.id)
  271. },
  272. // 视频播放结束事件
  273. onVideoEnd() {
  274. this.setData({
  275. isPlaying: false,
  276. videoStatus: 0
  277. });
  278. // 可以添加结束埋点
  279. console.log('视频播放结束:', this.data.videoInfo.id);
  280. },
  281. // 视频时间更新事件
  282. onVideoUpdate(e) {
  283. const { currentTime, duration } = e.detail;
  284. this.setData({
  285. currentTime,
  286. duration
  287. });
  288. },
  289. // 视频元数据加载完成
  290. onVideoLoaded(e) {
  291. console.log('视频元数据加载完成', e.detail);
  292. },
  293. // 视频播放
  294. makeVideoPlay() {
  295. if (!this.data.videoInfo.lock && this.data.videoStatus == 2) {
  296. let video = this.getCurrentVideoContext()
  297. video.play()
  298. }
  299. },
  300. // 视频暂停
  301. makeVideoPause() {
  302. if (this.data.videoStatus == 1) {
  303. let video = this.getCurrentVideoContext()
  304. video.pause()
  305. }
  306. },
  307. // 获取当前视频播放器实例
  308. getCurrentVideoContext() {
  309. return wx.createVideoContext(`myVideo`, this)
  310. },
  311. // 点击视频控制区
  312. onClickVideoControls() {
  313. if (this.data.videoStatus === 1) {
  314. // 当前正在播放,暂停视频
  315. const videoContext = wx.createVideoContext('myVideo');
  316. videoContext.pause();
  317. this.insertAdType = 1;
  318. this.interstitalPlayFn();
  319. } else {
  320. // 当前已暂停,开始播放
  321. const videoContext = wx.createVideoContext('myVideo');
  322. videoContext.play();
  323. }
  324. },
  325. // 点击相关推荐视频
  326. onRelatedVideoTap(e) {
  327. const _this = this
  328. const videoId = e.currentTarget.dataset.id;
  329. const video = this.data.relatedVideos.find(item => item.id === videoId);
  330. if (!video) {
  331. wx.showToast({
  332. title: '视频信息获取失败',
  333. icon: 'none'
  334. });
  335. return;
  336. }
  337. const tag_id = video.tag_ids[0];
  338. // 显示加载中提示
  339. wx.showLoading({
  340. title: '加载中...',
  341. mask: true
  342. });
  343. // 将标题进行URI编码处理
  344. const encodedTitle = encodeURIComponent(video.title || '');
  345. this.unlockUrl = `/pages/video/detail?id=${videoId}&title=${encodedTitle}&tag_id=${tag_id}`;
  346. SendEvent('click_recommend_video', { "video_id": videoId, "tag_id": tag_id, "uuid": app.globalData.uuid });
  347. if (!video.lock) {
  348. // 跳转到视频详情页
  349. wx.navigateTo({
  350. url: this.unlockUrl,
  351. success: () => {
  352. wx.hideLoading();
  353. },
  354. fail: (err) => {
  355. console.error('跳转失败:', err);
  356. wx.hideLoading();
  357. wx.showToast({
  358. title: '跳转失败',
  359. icon: 'none'
  360. });
  361. }
  362. });
  363. } else {
  364. this.rewardVideoLoad()
  365. wx.showModal({
  366. title: '提示',
  367. content: '当前视频需要看完完整广告后才能解锁',
  368. cancelText: '关闭',
  369. confirmText: '解锁视频',
  370. confirmColor: '#02BB00',
  371. success(res) {
  372. if (res.confirm) {
  373. _this.rewardType = 2
  374. _this.rewardVideoPlayFn()
  375. } else if (res.cancel) {
  376. console.log('用户点击取消')
  377. wx.hideLoading();
  378. }
  379. }
  380. })
  381. }
  382. },
  383. // 跳转到书籍详情
  384. goToBookDetail() {
  385. getApp().globalData.isShowDialog = true
  386. goToBookDetail({
  387. bookId: 10745,
  388. wxBookId: 'A1L2D5CPFHZW6Gzr9VdypmijDJ',
  389. chapterId: null
  390. });
  391. },
  392. // 返回上一页
  393. goBack() {
  394. wx.navigateBack({
  395. delta: 1
  396. }
  397. );
  398. // wx.redirectTo({
  399. // url: '/pages/video/video',
  400. // });
  401. console.log('返回上一页')
  402. SendEvent('click_return_list', {
  403. "video_id": this.data.videoId,
  404. "tag_id": this.data.tagId,
  405. "uuid": app.globalData.uuid
  406. });
  407. },
  408. // 分享按钮点击事件
  409. onShareTap(e) {
  410. // 阻止事件冒泡
  411. e.stopPropagation();
  412. },
  413. // 分享给朋友
  414. onShareAppMessage(e) {
  415. const jump = wx.getStorageSync('jump'); // 是否跳转阅读器
  416. let url = '';
  417. // 如果是从推荐视频分享
  418. if (e.target && e.target.dataset.video) {
  419. const video = e.target.dataset.video;
  420. url = `/pages/video/video?shared_video_id=${video.id}&title=${encodeURIComponent(video.title)}&tag_id=${video.tag_ids[0]}&share_count=1&type=share`;
  421. if (jump === 1) {
  422. url += '&jump=1';
  423. }
  424. SendEvent('click_share', { "video_id": video.id, "tag_id": video.tag_ids[0] });
  425. return {
  426. title: video.title,
  427. path: url,
  428. imageUrl: video.cover_image,
  429. success: function (res) {
  430. wx.showToast({
  431. title: '分享成功',
  432. icon: 'success',
  433. duration: 1500
  434. });
  435. },
  436. fail: function (res) {
  437. wx.showToast({
  438. title: '分享失败',
  439. icon: 'none',
  440. duration: 1500
  441. });
  442. }
  443. };
  444. }
  445. // 默认分享当前视频
  446. const videoInfo = this.data.videoInfo;
  447. url = `/pages/video/video?shared_video_id=${videoInfo.id}&title=${encodeURIComponent(videoInfo.title)}&tag_id=${videoInfo.tag_ids[0]}&share_count=1&type=share`;
  448. if (jump === 1) {
  449. url += '&jump=1';
  450. }
  451. SendEvent('click_share', { "video_id": videoInfo.id, "tag_id": videoInfo.tag_ids[0] });
  452. return {
  453. title: videoInfo.title,
  454. path: url,
  455. imageUrl: videoInfo.cover,
  456. success: function (res) {
  457. wx.showToast({
  458. title: '分享成功',
  459. icon: 'success',
  460. duration: 1500
  461. });
  462. },
  463. fail: function (res) {
  464. wx.showToast({
  465. title: '分享失败',
  466. icon: 'none',
  467. duration: 1500
  468. });
  469. }
  470. };
  471. },
  472. // 分享到朋友圈
  473. onShareTimeline() {
  474. const videoInfo = this.data.videoInfo;
  475. const jump = wx.getStorageSync('jump'); // 是否跳转阅读器
  476. let params = `shared_video_id=${videoInfo.id}&title=${encodeURIComponent(videoInfo.title)}&tag_id=${videoInfo.tag_ids[0]}&share_count=1&type=share`;
  477. if (jump === 1) {
  478. params += '&jump=1';
  479. }
  480. SendEvent('click_share_moment', { "video_id": videoInfo.id, "tag_id": videoInfo.tag_ids[0] });
  481. return {
  482. title: videoInfo.title,
  483. query: params,
  484. imageUrl: videoInfo.cover
  485. };
  486. },
  487. // 收藏视频
  488. toggleCollect() {
  489. const isCollect = !this.data.videoInfo.isCollect;
  490. this.setData({
  491. 'videoInfo.isCollect': isCollect
  492. });
  493. wx.showToast({
  494. title: isCollect ? '收藏成功' : '已取消收藏',
  495. icon: 'success'
  496. });
  497. },
  498. // 点赞视频
  499. likeVideo() {
  500. const currentLikes = parseInt(this.data.videoInfo.likeCount) || 0;
  501. this.setData({
  502. 'videoInfo.likeCount': (currentLikes + 1).toString()
  503. });
  504. wx.showToast({
  505. title: '点赞成功',
  506. icon: 'success'
  507. });
  508. },
  509. // 滚动事件
  510. onScroll(e) {
  511. this.setData({
  512. scrollHeight: e.detail.scrollTop
  513. });
  514. },
  515. // 加载相关视频
  516. async loadRelatedVideos(tagIds) {
  517. try {
  518. // 使用第一个标签ID获取相关视频
  519. const tagId = tagIds && tagIds.length > 0 ? tagIds[0] : 2072;
  520. console.log("原始ID:", apple.ghid)
  521. const relatedVideos = await getRelatedVideos({ tag_id: tagId, gh_id: apple.ghid });
  522. // 需要播放激励视频的索引
  523. let rewardIndexList = []
  524. console.log('详情激励配置', app.globalData.ads_config)
  525. if (app.globalData.ads_config.detailListLockIndex) {
  526. rewardIndexList = app.globalData.ads_config.detailListLockIndex.split(' ').map(i => {
  527. return i - 1
  528. })
  529. }
  530. console.log('详情激励顺序', rewardIndexList)
  531. // 处理相关视频数据
  532. // const formattedVideos = relatedVideos.map((video,index) => ({
  533. // id: video.id,
  534. // title: video.title,
  535. // adType:'video',
  536. // tag_id: video.tag_ids[0],
  537. // cover_image: video.cover_image,
  538. // playCount: video.views || '0',
  539. // likeCount: video.like || '0',
  540. // shareCount: video.share || '0',
  541. // }));
  542. const formattedVideos = relatedVideos.map((video, index) => {
  543. video.adType = 'video'
  544. video.lock = rewardIndexList.includes(index);
  545. return video
  546. });
  547. this.setData({
  548. relatedVideos: formattedVideos
  549. });
  550. console.log('相关视频', formattedVideos)
  551. } catch (error) {
  552. console.error('获取相关视频失败:', error);
  553. }
  554. },
  555. // 激励视频加载
  556. rewardVideoLoad() {
  557. if (wx.createRewardedVideoAd) {
  558. let that = this
  559. this.videoAd = wx.createRewardedVideoAd({
  560. adUnitId: apple.ads.rewardAdId
  561. })
  562. this.videoAd.onLoad(() => {
  563. console.log('详情页激励load success')
  564. })
  565. this.videoAd.onError((err) => {
  566. console.log('详情页激励load error', err)
  567. })
  568. this.videoAd.onClose((res) => {
  569. // 用户点击了【关闭广告】按钮
  570. this.setData({
  571. isLoadingVideo: false
  572. });
  573. console.log('详情页激励close', res)
  574. if (res && res.isEnded) {
  575. // 发放奖励
  576. if (this.rewardType === 1) {
  577. this.setData({
  578. [`videoInfo.lock`]: false,
  579. })
  580. const video = this.getCurrentVideoContext()
  581. video.play()
  582. } else {
  583. wx.redirectTo({
  584. url: this.unlockUrl
  585. })
  586. }
  587. } else {
  588. // 未看完整视频
  589. // wx.showModal({
  590. // title: '提示',
  591. // content: '您需要观看完毕才能进行文章查看,重新播放广告吗?',
  592. // success (res) {
  593. // if (res.confirm) {
  594. // that.rewardVideoPlayFn()
  595. // } else if (res.cancel) {
  596. // console.log('用户点击取消')
  597. // }
  598. // }
  599. // })
  600. }
  601. })
  602. }
  603. },
  604. // 激励视频展示
  605. rewardVideoPlayFn() {
  606. wx.showLoading({
  607. title: '解锁视频中',
  608. })
  609. setTimeout(function () {
  610. wx.hideLoading()
  611. }, 2000)
  612. this.videoAd.show().catch((err) => {
  613. console.log('激励视频播放失败', err)
  614. wx.showToast({
  615. title: '激励视频广告播放失败,请稍后再试',
  616. icon: 'none'
  617. })
  618. })
  619. },
  620. // 进入页面插屏
  621. interstitalEnterPageShow() {
  622. insertAdInterval = setTimeout(() => {
  623. console.log('详情页进入页面插屏调用')
  624. this.insertAdType = 2
  625. this.interstitalPlayFn()
  626. }, 5000)
  627. },
  628. // 插屏广告加载
  629. interstitalLoad() {
  630. // const _this = this
  631. interstitialAd = wx.createInterstitialAd({
  632. adUnitId: apple.ads.insertAdId
  633. })
  634. interstitialAd.onLoad(() => {
  635. console.log('视频详情页插屏广告 load success')
  636. })
  637. interstitialAd.onError((err) => {
  638. console.log('视频详情页插屏广告onError', err)
  639. })
  640. interstitialAd.onClose(() => {
  641. console.log('视频详情页插屏广告onClose')
  642. this.makeVideoPlay()
  643. })
  644. },
  645. // 插屏广告展示
  646. interstitalPlayFn() {
  647. // const _this = this
  648. if (interstitialAd) {
  649. interstitialAd.show().then(() => {
  650. console.log('插屏显示,视频暂停')
  651. if (this.insertAdType !== 1) {
  652. setTimeout(() => {
  653. this.makeVideoPause()
  654. }, 500)
  655. }
  656. }).catch((err) => {
  657. if (this.insertAdType !== 1) {
  658. setTimeout(() => {
  659. this.makeVideoPlay()
  660. }, 1000)
  661. }
  662. console.error('视频详情页插屏显示错误', err)
  663. })
  664. }
  665. },
  666. })