hank 1 year ago
parent
commit
d894b09153
4 changed files with 228 additions and 58 deletions
  1. 3 1
      app.js
  2. 24 23
      pages/mine/mine.js
  3. 198 32
      pages/video/detail.js
  4. 3 2
      pages/video/video.js

+ 3 - 1
app.js

@@ -117,7 +117,9 @@ App({
         bookmarkAdNum: 3,
         openId: '',
         unionId: '',
-        isEnterVideoDetail: false  // 是否进入过视频详情页
+        isEnterVideoDetail: false,  // 是否进入过视频详情页
+        ads_config: {}, // 广告策略配置
+        userCode: '',
     }
 })
 

+ 24 - 23
pages/mine/mine.js

@@ -18,10 +18,11 @@ Page({
         this.setData({
             userCode: app.globalData.userCode
         });
-        console.log('userCode', app.globalData.userCode);
+        console.log('', app.globalData);
     },
 
     onShow() {
+
     },
 
     onTabItemTap(item) {
@@ -33,29 +34,29 @@ Page({
 
     // 自动登录
     async autoLogin() {
-      try {
-        // 检查是否已经登录
-        const token = wx.getStorageSync('accessToken');
-        if (token) {
-          this.setData({
-            isLoggedIn: true
-          });
-          // 已经登录
-          console.log('已经登录')
-          // 页面首次加载时的处理
-          this.interstitalLoad()
-          return;
+        try {
+            // 检查是否已经登录
+            const token = wx.getStorageSync('accessToken');
+            if (token) {
+                this.setData({
+                    isLoggedIn: true
+                });
+                // 已经登录
+                console.log('已经登录')
+                // 页面首次加载时的处理
+                this.interstitalLoad()
+                return;
+            }
+            // 未登录,执行登录
+            const res = await app.login();
+            this.setData({
+                isLoggedIn: true
+            });
+            console.log('重新登录', res)
+            this.interstitalLoad();
+        } catch (error) {
+            console.error('登录失败:', error);
         }
-        // 未登录,执行登录
-        const res = await app.login();
-        this.setData({
-          isLoggedIn: true
-        });
-        console.log('重新登录', res)
-        this.interstitalLoad();
-      } catch (error) {
-        console.error('登录失败:', error);
-      }
     },
 
     // 插屏广告加载

+ 198 - 32
pages/video/detail.js

@@ -4,6 +4,8 @@ import {rnd, SendEvent} from '../../utils/util'
 import {apple} from "../../config/config";
 
 const app = getApp();
+let interstitialAd = null
+let insertAdInterval = null
 
 Page({
     data: {
@@ -56,7 +58,8 @@ Page({
                 tagId: tagId
             });
         }
-
+        this.rewardVideoLoad();
+        this.interstitalLoad();
         this.loadVideoDetail(videoId);
         app.globalData.isEnterVideoDetail = true;
     },
@@ -167,7 +170,7 @@ Page({
 
                 // 获取真实播放地址
                 this.getVideoPlayUrl(videoDetail);
-
+                this.interstitalEnterPageShow()
                 // 加载相关视频
                 this.loadRelatedVideos(videoDetail.tag_ids);
             } else {
@@ -198,8 +201,6 @@ Page({
 
     // 获取视频播放地址
     async getVideoPlayUrl(videoDetail) {
-
-
         console.log(`videoDetail`, videoDetail)
         try {
             // 根据视频类型进行处理
@@ -299,6 +300,7 @@ Page({
             isPlaying: false,
             videoStatus: 2
         });
+        console.log('视频暂停:', this.data.videoInfo.id)
     },
 
     // 视频播放结束事件
@@ -326,12 +328,35 @@ Page({
         console.log('视频元数据加载完成', e.detail);
     },
 
+    // 视频播放
+    makeVideoPlay() {
+        if (!this.data.videoInfo.lock && this.data.videoStatus == 2) {
+            let video = this.getCurrentVideoContext()
+            video.play()
+        }
+    },
+
+    // 视频暂停
+    makeVideoPause() {
+        if (this.data.videoStatus == 1) {
+            let video = this.getCurrentVideoContext()
+            video.pause()
+        }
+    },
+
+    // 获取当前视频播放器实例
+    getCurrentVideoContext() {
+        return wx.createVideoContext(`myVideo`, this)
+    },
+
     // 点击视频控制区
     onClickVideoControls() {
         if (this.data.videoStatus === 1) {
             // 当前正在播放,暂停视频
             const videoContext = wx.createVideoContext('myVideo');
             videoContext.pause();
+            this.insertAdType = 1;
+            this.interstitalPlayFn();
         } else {
             // 当前已暂停,开始播放
             const videoContext = wx.createVideoContext('myVideo');
@@ -352,32 +377,38 @@ Page({
             });
             return;
         }
-        const tag_id = video.tag_id;
+        const tag_id = video.tag_ids[0];
         // console.log('tag_id',tag_id)
         // 显示加载中提示
         wx.showLoading({
             title: '加载中...',
             mask: true
         });
-
         // 将标题进行URI编码处理
         const encodedTitle = encodeURIComponent(video.title || '');
+        this.unlockUrl = `/pages/video/detail?id=${videoId}&title=${encodedTitle}&tag_id=${tag_id}`;
         SendEvent('click_recommend_video', {"video_id": videoId, "tag_id": tag_id});
-        // 跳转到视频详情页
-        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'
-                });
-            }
-        });
+        if (!video.lock) {
+            // 跳转到视频详情页
+            wx.navigateTo({
+                url: this.unlockUrl,
+                success: () => {
+                    wx.hideLoading();
+                },
+                fail: (err) => {
+                    console.error('跳转失败:', err);
+                    wx.hideLoading();
+                    wx.showToast({
+                        title: '跳转失败',
+                        icon: 'none'
+                    });
+                }
+            });
+        } else {
+            this.rewardType = 2
+            this.rewardVideoPlayFn()
+        }
+
     },
 
     // 跳转到书籍详情
@@ -528,23 +559,158 @@ Page({
             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)
+            // 需要播放激励视频的索引
+            let rewardIndexList = []
+            console.log('详情激励配置', app.globalData.ads_config)
+            if (app.globalData.ads_config.detailListLockIndex) {
+                rewardIndexList = app.globalData.ads_config.detailListLockIndex.split(' ').map(i => {
+                    return i - 1
+                })
+            }
+            console.log('详情激励顺序', rewardIndexList)
             // 处理相关视频数据
-            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'
-            }));
+            // const formattedVideos = relatedVideos.map((video,index) => ({
+            //     id: video.id,
+            //     title: video.title,
+            //     adType:'video',
+            //     tag_id: video.tag_ids[0],
+            //     cover_image: video.cover_image,
+            //     playCount: video.views || '0',
+            //     likeCount: video.like || '0',
+            //     shareCount: video.share || '0',
+            // }));
+
+            const formattedVideos = relatedVideos.map((video, index) => {
+                video.adType = 'video'
+                video.lock = rewardIndexList.includes(index);
+                return video
+            });
 
             this.setData({
                 relatedVideos: formattedVideos
             });
+            console.log('相关视频', formattedVideos)
         } catch (error) {
             console.error('获取相关视频失败:', error);
         }
-    }
+    },
+
+    // 激励视频加载
+    rewardVideoLoad() {
+        if (wx.createRewardedVideoAd) {
+            let that = this
+            this.videoAd = wx.createRewardedVideoAd({
+                adUnitId: apple.ads.rewardAdId
+            })
+
+            this.videoAd.onLoad(() => {
+                console.log('详情页激励load success')
+            })
+            this.videoAd.onError((err) => {
+                console.log('详情页激励load error', err)
+            })
+            this.videoAd.onClose((res) => {
+                // 用户点击了【关闭广告】按钮
+                this.setData({
+                    isLoadingVideo: false
+                });
+                console.log('详情页激励close', res)
+                if (res && res.isEnded) {
+                    // 发放奖励
+                    if (this.rewardType === 1) {
+                        this.setData({
+                            [`videoInfo.lock`]: false,
+                        })
+                        const video = this.getCurrentVideoContext()
+                        video.play()
+                    } else {
+                        wx.redirectTo({
+                            url: this.unlockUrl
+                        })
+                    }
+                } else {
+                    // 未看完整视频
+                    // wx.showModal({
+                    //   title: '提示',
+                    //   content: '您需要观看完毕才能进行文章查看,重新播放广告吗?',
+                    //   success (res) {
+                    //     if (res.confirm) {
+                    //       that.rewardVideoPlayFn()
+                    //     } else if (res.cancel) {
+                    //       console.log('用户点击取消')
+                    //     }
+                    //   }
+                    // })
+                }
+            })
+        }
+    },
+
+    // 激励视频展示
+    rewardVideoPlayFn() {
+        wx.showLoading({
+            title: '解锁视频中',
+        })
+        setTimeout(function () {
+            wx.hideLoading()
+        }, 2000)
+        this.videoAd.show().catch((err) => {
+            console.log('激励视频播放失败', err)
+            wx.showToast({
+                title: '激励视频广告播放失败,请稍后再试',
+                icon: 'none'
+            })
+        })
+    },
+
+    // 进入页面插屏
+    interstitalEnterPageShow() {
+        insertAdInterval = setTimeout(() => {
+            console.log('详情页进入页面插屏调用')
+            this.insertAdType = 2
+            this.interstitalPlayFn()
+        }, 2000)
+    },
+
+    // 插屏广告加载
+    interstitalLoad() {
+        // const _this = this
+        interstitialAd = wx.createInterstitialAd({
+            adUnitId: apple.ads.insertAdId
+        })
+        interstitialAd.onLoad(() => {
+            console.log('详情视频页插屏广告 load success')
+        })
+        interstitialAd.onError((err) => {
+            console.log('详情视频页插屏广告onError', err)
+        })
+        interstitialAd.onClose(() => {
+            console.log('详情视频页插屏广告onClose')
+            this.makeVideoPlay()
+        })
+    },
+
+    // 插屏广告展示
+    interstitalPlayFn() {
+        // const _this = this
+        if (interstitialAd) {
+            interstitialAd.show().then(() => {
+                console.log('插屏显示,视频暂停')
+                if (this.insertAdType !== 1) {
+                    setTimeout(() => {
+                        this.makeVideoPause()
+                    }, 500)
+                }
+            }).catch((err) => {
+                if (this.insertAdType !== 1) {
+                    setTimeout(() => {
+                        this.makeVideoPlay()
+                    }, 1000)
+                }
+                console.error('详情页插屏显示错误', err)
+            })
+        }
+    },
+
+
 }) 

+ 3 - 2
pages/video/video.js

@@ -70,7 +70,8 @@ Page({
         }).then(res => {
             this.setData({
                 globalConfig: res.globalConfig
-            })
+            });
+            app.globalData.ads_config = res.globalConfig;
             // 获取视频分类
             this.getVideoCategories()
         })
@@ -222,7 +223,7 @@ Page({
         }, 1000)
     },
 
-    // 插屏广告加载
+    // 插屏广告加载 
     interstitalLoad() {
         let adId = apple.ads.insertAdId
         interstitialAd = wx.createInterstitialAd({