hank vor 1 Jahr
Ursprung
Commit
03a0cd46ae
1 geänderte Dateien mit 86 neuen und 509 gelöschten Zeilen
  1. 86 509
      pages/video/video.js

+ 86 - 509
pages/video/video.js

@@ -1,33 +1,41 @@
 // pages/video/video.js
-import {getVideoList, getTemplateConfig, getUuid} from '../../api/api'
-import {rnd, deepCopy, goToBookDetail} from '../../utils/util'
+import {
+    getVideoList,
+    getTemplateConfig,
+    getUuid
+} from '../../api/api'
+import {
+    rnd,
+    deepCopy,
+    goToBookDetail
+} from '../../utils/util'
 
 const app = getApp()
 let interstitialAd = null
 
 Page({
-  data: {
-    videoList: [],
-    loading: false,
-    hasMore: true,
-    page: 1,
-    size: 10,
-    // 状态栏高度
-    statusBarHeight: 20,
-    navHeight: 64, // 导航栏高度 = 状态栏 + 44
-    navStyle: '',
-    listStyle: '',
-    // 添加防抖时间戳
-    lastLoadTime: 0,
-    ads: null,
-    globalConfig: null,
-    // 视频分类列表
-    categoryList: [],
-    // 当前选中的分类ID
-    currentCategoryId: null,
-
-    share_count: 1
-  },
+    data: {
+        videoList: [],
+        loading: false,
+        hasMore: true,
+        page: 1,
+        size: 10,
+        // 状态栏高度
+        statusBarHeight: 20,
+        navHeight: 64, // 导航栏高度 = 状态栏 + 44
+        navStyle: '',
+        listStyle: '',
+        // 添加防抖时间戳
+        lastLoadTime: 0,
+        ads: null,
+        globalConfig: null,
+        // 视频分类列表
+        categoryList: [],
+        // 当前选中的分类ID
+        currentCategoryId: null,
+
+        share_count: 1
+    },
 
     async onLoad() {
         console.log('打开视频页面--------------------------')
@@ -37,6 +45,7 @@ Page({
             })
         }
 
+        this.interstitalLoad()
         getTemplateConfig({
             box_type: 67,
             uuid: getApp().globalData.uuid
@@ -50,11 +59,14 @@ Page({
 
         // 获取状态栏高度
         this.getSystemInfo()
-
     },
 
     onShow() {
         console.log('页面显示--------------------------')
+        setTimeout(() => {
+            this.interstitalPlayFn()
+        }, 1000)
+
         // 每次显示页面时刷新列表
         if (this.data.videoList.length === 0) {
             this.setData({
@@ -67,10 +79,9 @@ Page({
         const pages = getCurrentPages();
         const currentPage = pages[pages.length - 1];
         const options = currentPage.options;
-        console.log('video_id:', options.shared_video_id)
+
         // 处理分享进入的情况
         if (options.shared_video_id) {
-            console.log('share_count:', this.data.share_count)
             if (+options.share_count === 1 && this.data.share_count === 1) {
                 this.setData({
                     share_count: 2
@@ -80,37 +91,51 @@ Page({
                 this.setData({
                     share_count: 3
                 })
-                console.log('share_count2:', this.data.share_count)
-
+                // 延迟跳转,确保列表页已经加载完成
+                setTimeout(() => {
+                    wx.navigateTo({
+                        url: `/pages/video/detail?id=${options.shared_video_id}&title=${options.title || ''}&tag_id=${options.tag_id || ''}`,
+                        fail: (err) => {
+                            console.error('跳转到视频详情页失败:', err);
+                            wx.showToast({
+                                title: '打开视频失败',
+                                icon: 'none'
+                            });
+                        }
+                    });
+                }, 500);
             }
-            // 延迟跳转,确保列表页已经加载完成
-            setTimeout(() => {
-                wx.navigateTo({
-                    url: `/pages/video/detail?id=${options.shared_video_id}&title=${options.title || ''}&tag_id=${options.tag_id || ''}`,
-                    fail: (err) => {
-                        console.error('跳转到视频详情页失败:', err);
-                        wx.showToast({
-                            title: '打开视频失败',
-                            icon: 'none'
-                        });
-                    }
-                });
-            }, 500);
         }
     },
 
-    handleEntryPage() {
-        if (!options || Object.keys(options).length === 0) {
-            return
-        }
-        try {
-            let type = options.type
-            if (type == 'share' || type == 'tuiguang'){
+    onTabItemTap(item) {
+        console.log(item.index)
+    },
 
-            }
-        }
-        catch (e){
-            console.error('处理分享进入失败:', e);
+    // 插屏广告加载
+    interstitalLoad() {
+        const _this = this
+        let adId = 'adunit-37ee342a8fc046b7'
+        interstitialAd = wx.createInterstitialAd({
+            adUnitId: adId
+        })
+        interstitialAd.onLoad(() => {
+            console.log('视频页插屏广告onLoad')
+        })
+        interstitialAd.onError((err) => {
+            console.log('视频页插屏广告onError', err)
+        })
+        interstitialAd.onClose(() => {
+            console.log('视频页插屏广告onClose')
+        })
+    },
+
+    // 插屏广告展示
+    interstitalPlayFn() {
+        if (interstitialAd) {
+            interstitialAd.show().catch((err) => {
+                console.error(err)
+            })
         }
     },
 
@@ -194,7 +219,9 @@ Page({
     async loadVideoList() {
         if (this.data.loading) return
 
-        this.setData({loading: true})
+        this.setData({
+            loading: true
+        })
 
         try {
             const params = {
@@ -202,9 +229,9 @@ Page({
                 size: this.data.size,
                 tag_id: this.data.currentCategoryId,
                 // 如果不是第一页,传递最后一个视频的ID作为next_id
-                next_id: this.data.page > 1 && this.data.videoList.length > 0
-                    ? this.data.videoList[this.data.videoList.length - 1].id
-                    : ""
+                next_id: this.data.page > 1 && this.data.videoList.length > 0 ?
+                    this.data.videoList[this.data.videoList.length - 1].id :
+                    ""
             }
 
             let videoList = []
@@ -258,7 +285,8 @@ Page({
      */
     formatVideoAdList(arr) {
         // 原生广告间隔,激励广告间隔
-        let templateGap = 0, rewardGap = 0
+        let templateGap = 0,
+            rewardGap = 0
 
         // 第二个固定是原生广告
         let result = [arr.shift(), {
@@ -467,455 +495,4 @@ Page({
             chapterId: null
         });
     }
-
-    this.interstitalLoad()
-    getTemplateConfig({
-      box_type: 67,
-      uuid: getApp().globalData.uuid
-    }).then(res => {
-      this.setData({
-        globalConfig: res.globalConfig
-      })
-      // 获取视频分类
-      this.getVideoCategories()
-    })
-
-    // 获取状态栏高度
-    this.getSystemInfo()
-  },
-
-  onShow() {
-    console.log('页面显示--------------------------')
-    setTimeout(() => {
-      this.interstitalPlayFn()
-    }, 1000)
-
-    // 每次显示页面时刷新列表
-    if (this.data.videoList.length === 0) {
-      this.setData({
-        page: 1
-      })
-      // this.loadVideoList()
-    }
-
-    // 获取当前页面的参数
-    const pages = getCurrentPages();
-    const currentPage = pages[pages.length - 1];
-    const options = currentPage.options;
-
-    // 处理分享进入的情况
-    if (options.shared_video_id) {
-      if (+options.share_count === 1 && this.data.share_count === 1) {
-        this.setData({
-          share_count: 2
-        })
-        this.goToBookDetail()
-      } else if (+options.share_count === 1 && this.data.share_count === 2) {
-        this.setData({
-          share_count: 3
-        })
-        // 延迟跳转,确保列表页已经加载完成
-        setTimeout(() => {
-          wx.navigateTo({
-            url: `/pages/video/detail?id=${options.shared_video_id}&title=${options.title || ''}&tag_id=${options.tag_id || ''}`,
-            fail: (err) => {
-              console.error('跳转到视频详情页失败:', err);
-              wx.showToast({
-                title: '打开视频失败',
-                icon: 'none'
-              });
-            }
-          });
-        }, 500);
-      }
-    }
-  },
-
-  onTabItemTap(item) {
-    console.log(item.index)
-  },
-
-  // 插屏广告加载
-  interstitalLoad() {
-    const _this = this
-    let adId = 'adunit-37ee342a8fc046b7'
-    interstitialAd = wx.createInterstitialAd({
-      adUnitId: adId
-    })
-    interstitialAd.onLoad(() => {
-      console.log('视频页插屏广告onLoad')
-    })
-    interstitialAd.onError((err) => {
-      console.log('视频页插屏广告onError', err)
-    })
-    interstitialAd.onClose(() => {
-      console.log('视频页插屏广告onClose')
-    })
-  },
-
-  // 插屏广告展示
-  interstitalPlayFn() {
-    if (interstitialAd) {
-      interstitialAd.show().catch((err) => {
-        console.error(err)
-      })
-    }
-  },
-
-  /**
-   * 获取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()
-    const statusBarHeight = systemInfo.statusBarHeight
-    const navHeight = statusBarHeight + 44
-
-    this.setData({
-      statusBarHeight: statusBarHeight,
-      navHeight: navHeight,
-      navStyle: `height:${navHeight}px`,
-      listStyle: `top:${navHeight}px`
-    })
-  },
-
-  // 获取视频分类
-  getVideoCategories() {
-    console.log('getVideoCategories', this.data.globalConfig)
-    // 筛选出type=1的视频分类
-    const videoCategories = this.data.globalConfig.categoryList.filter(item => item.type === 1)
-
-    console.log('videoCategories', videoCategories)
-
-    if (videoCategories.length > 0) {
-      // 设置分类列表和默认选中的分类
-      this.setData({
-        categoryList: videoCategories,
-        currentCategoryId: videoCategories[0].id
-      })
-      // 加载第一个分类的视频列表
-      this.loadVideoList()
-    } else {
-      console.error('未找到视频分类')
-      wx.showToast({
-        title: '未找到视频分类',
-        icon: 'none'
-      })
-    }
-  },
-
-  // 切换分类
-  switchCategory(e) {
-    const categoryId = e.currentTarget.dataset.id
-    if (categoryId === this.data.currentCategoryId) return
-
-    this.setData({
-      currentCategoryId: categoryId,
-      page: 1,
-      videoList: [],
-      hasMore: true
-    })
-    this.loadVideoList()
-  },
-
-  // 加载视频列表
-  async loadVideoList() {
-    if (this.data.loading) return
-
-    this.setData({
-      loading: true
-    })
-
-    try {
-      const params = {
-        page: this.data.page,
-        size: this.data.size,
-        tag_id: this.data.currentCategoryId,
-        // 如果不是第一页,传递最后一个视频的ID作为next_id
-        next_id: this.data.page > 1 && this.data.videoList.length > 0 ?
-          this.data.videoList[this.data.videoList.length - 1].id :
-          ""
-      }
-
-      let videoList = []
-      try {
-        videoList = await getVideoList(params)
-
-        if (!Array.isArray(videoList)) {
-          throw new Error('返回数据格式错误')
-        }
-
-        videoList = videoList.map(item => ({
-          ...item,
-          views: rnd(1, 10),
-          favors: rnd(5000, 10000),
-          adType: "video"
-        }))
-
-        videoList = this.formatVideoAdList(videoList);
-
-        console.log("format videoList", videoList)
-
-      } catch (apiError) {
-        console.error('API调用失败:', apiError)
-        throw apiError
-      }
-
-      // 如果是第一页,直接替换数据
-      if (this.data.page === 1) {
-        this.setData({
-          videoList: videoList,
-          loading: false,
-          hasMore: videoList.length >= this.data.size
-        })
-      } else {
-        // 否则追加数据
-        this.setData({
-          videoList: [...this.data.videoList, ...videoList],
-          loading: false,
-          hasMore: videoList.length >= this.data.size
-        })
-      }
-    } catch (error) {
-      console.error('加载视频列表失败:', error)
-    }
-  },
-  /* ------------------ 广告相关 ----------------- */
-  /**
-   * 标记广告位置
-   * @param {} arr
-   * @returns
-   */
-  formatVideoAdList(arr) {
-    // 原生广告间隔,激励广告间隔
-    let templateGap = 0,
-      rewardGap = 0
-
-    // 第二个固定是原生广告
-    let result = [arr.shift(), {
-      adType: 'templateAd'
-    }]
-    // 辅助计算间隔变量
-    let markIndex = 0
-    let templateAdFlag = true
-    let rewardAdFlag = true
-
-    do {
-      markIndex += 1
-      if (templateGap > 0) {
-        if (templateAdFlag && markIndex - 1 === templateGap) {
-          markIndex = 0
-          result.push({
-            adType: "templateAd"
-          })
-          templateAdFlag = false
-          rewardAdFlag = true
-          continue
-        }
-      } else {
-        rewardAdFlag = true
-      }
-      if (rewardGap > 0) {
-        if (rewardAdFlag && markIndex - 1 === rewardGap) {
-          markIndex = 0
-          templateAdFlag = true
-          rewardAdFlag = false
-          let rewardItem = arr.shift()
-          rewardItem.lock = true
-          result.push(rewardItem)
-          continue
-        }
-      } else {
-        templateAdFlag = true
-      }
-
-      result.push(arr.shift())
-    } while (arr.length > 0);
-
-    return result;
-  },
-  // 加载更多
-  loadMore() {
-    console.log("触发loadMore")
-    console.log("当前状态:", {
-      loading: this.data.loading,
-      hasMore: this.data.hasMore,
-      page: this.data.page,
-      videoListLength: this.data.videoList.length
-    })
-
-    // 添加防抖,避免频繁触发
-    const now = Date.now()
-    if (now - this.data.lastLoadTime < 500) {
-      console.log("防抖触发,跳过本次加载")
-      return
-    }
-
-    if (this.data.hasMore && !this.data.loading) {
-      console.log("开始加载更多数据")
-      this.setData({
-        page: this.data.page + 1,
-        lastLoadTime: now
-      })
-      this.loadVideoList()
-    } else {
-      console.log("不满足加载条件:", {
-        hasMore: this.data.hasMore,
-        loading: this.data.loading
-      })
-    }
-  },
-
-  // 刷新列表
-  refreshList() {
-    wx.showLoading({
-      title: '刷新中...',
-    })
-
-    this.setData({
-      page: 1,
-      videoList: [],
-      hasMore: true
-    })
-
-    this.loadVideoList().then(() => {
-      wx.hideLoading()
-      wx.showToast({
-        title: '刷新成功',
-        icon: 'success',
-        duration: 1500
-      })
-    }).catch(() => {
-      wx.hideLoading()
-    })
-  },
-
-  // 点击视频
-  onVideoTap(e) {
-    const videoId = e.currentTarget.dataset.id;
-
-    // 显示加载中提示
-    wx.showLoading({
-      title: '加载中...',
-      mask: true
-    });
-
-    // 获取当前点击的视频项
-    const video = this.data.videoList.find(item => item.id === videoId);
-
-    // 确保视频对象存在
-    if (!video) {
-      wx.hideLoading();
-      wx.showToast({
-        title: '视频数据错误',
-        icon: 'none'
-      });
-      return;
-    }
-
-    // 将标题进行URI编码处理(避免特殊字符导致的问题)
-    const encodedTitle = encodeURIComponent(video.title || '');
-
-    // 跳转到视频详情页
-    wx.navigateTo({
-      url: `/pages/video/detail?id=${videoId}&title=${encodedTitle}&tag_id=${e.currentTarget.dataset.info.tag_ids[0]}`,
-      success: () => {
-        wx.hideLoading();
-      },
-      fail: (err) => {
-        console.error('跳转失败:', err);
-        wx.hideLoading();
-        wx.showToast({
-          title: '跳转失败',
-          icon: 'none'
-        });
-      }
-    });
-  },
-
-  // 下拉刷新
-  onPullDownRefresh() {
-    this.setData({
-      page: 1,
-      videoList: []
-    })
-    this.loadVideoList().then(() => {
-      wx.stopPullDownRefresh()
-    })
-  },
-
-  // 分享按钮点击事件
-  onShareTap(e) {
-    // 阻止事件冒泡
-    e.stopPropagation();
-  },
-
-  // 分享给朋友
-  onShareAppMessage(e) {
-    const video = e.target.dataset.video
-    if (!video) return {}
-
-    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`,
-      imageUrl: video.cover_image,
-      success: function (res) {
-        wx.showToast({
-          title: '分享成功',
-          icon: 'success',
-          duration: 1500
-        })
-      },
-      fail: function (res) {
-        wx.showToast({
-          title: '分享失败',
-          icon: 'none',
-          duration: 1500
-        })
-      }
-    }
-  },
-
-  // 分享到朋友圈
-  onShareTimeline(e) {
-    const video = e.target.dataset.video
-    if (!video) return {}
-
-    return {
-      title: video.title,
-      query: `shared_video_id=${video.id}&title=${encodeURIComponent(video.title)}&tag_id=${video.tag_ids[0]}&share_count=1`,
-      imageUrl: video.cover_image
-    }
-  },
-
-  // 跳转到书籍详情
-  goToBookDetail() {
-    getApp().globalData.isShowDialog = true
-    // 使用公共的goToBookDetail函数
-    goToBookDetail({
-      bookId: 7266,
-      wxBookId: 'A1Hcfv456vGtMYxQjxUm8KNdJ8',
-      chapterId: null
-    });
-  }
 })