Просмотр исходного кода

feat 激励解锁用官方版本

erekook 11 месяцев назад
Родитель
Сommit
91a9d122d2
3 измененных файлов с 74 добавлено и 14 удалено
  1. 70 8
      app.js
  2. 1 3
      app.json
  3. 3 3
      utils/util.js

+ 70 - 8
app.js

@@ -2,16 +2,17 @@ import {
     SendEvent,
 } from '/utils/util.js'
 
-// import {
-//   getChapterList
-// } from '../../api/api.js'
+import {
+    unlockChapter
+} from '/api/api.js'
 
 // app.js
 // 引入阅读器插件
 const novelPlugin = requirePlugin('novel-plugin')
+let videoAd
 
-import {userLogin, getChapterUnlockStatus, getNovelDetail, addBrowsingHistory} from './api/api'
-import {apple} from './config/config'
+import { userLogin, getChapterUnlockStatus, getNovelDetail, addBrowsingHistory } from './api/api'
+import { apple } from './config/config'
 
 App({
     onLaunch(options) {
@@ -119,6 +120,7 @@ App({
         uuid: '',
         isShowDialog: false,
         novelId: '',
+        chapterIndex: 0,
         unlockChapterNum: 1,
         bookmarkAdNum: 3,
         openId: '',
@@ -135,6 +137,7 @@ async function onNovelPluginLoad(data) {
     // data.id - 阅读器实例 id,每个插件页对应一个阅读器实例
     const novelManager = novelPlugin.getNovelManager(data.id)
     getApp().globalData.novelManager = novelManager
+    rewardVideoLoad()
 
     novelManager.setFullScreenComponentStatus({
         show: true,
@@ -161,16 +164,24 @@ async function onNovelPluginLoad(data) {
         novelManager.setContents({
             contents: contents,
         });
-
         novelManager.setChargeWay({
             globalConfig: {
-                mode: 1,
+                mode: 5,
                 buttonText: '解锁',
                 showButton: true,
                 tip: '看完广告之后可继续阅读',
             }
         })
 
+        novelManager.onCountdownDoneUnlock(res => {
+            console.log('novelManager倒计时结束')
+            rewardVideoPlayFn()
+        })
+        novelManager.onUserClickCustomUnlock(res => {
+            console.log('novelManager 点击解锁')
+            rewardVideoPlayFn()
+        })
+
         let chapterConfigs = []
         for (let index = 0; index < bookDetail.latestChapterNo; index++) {
             if ((index + 1) % getApp().globalData.bookmarkAdNum === 0) {
@@ -241,6 +252,7 @@ async function onNovelPluginLoad(data) {
                 SendEvent('change_chapter', {
                     page: '切换到章节'
                 })
+                getApp().globalData.chapterIndex = res.chapter_index
                 console.log('切换到章节:', res.chapter_id);
                 break;
 
@@ -256,4 +268,54 @@ async function onNovelPluginLoad(data) {
                 break;
         }
     })
-}
+}
+
+// 激励视频加载
+function rewardVideoLoad() {
+    if (wx.createRewardedVideoAd) {
+        videoAd = wx.createRewardedVideoAd({
+            adUnitId: apple.ads.rewardAdId
+        })
+        videoAd.offClose()
+        videoAd.onLoad(() => {
+            console.log('激励加载成功')
+        })
+        videoAd.onError((err) => {
+            console.log('激励失败', err)
+        })
+
+        videoAd.onClose((res) => {
+            const novelManager = novelPlugin.getNovelManager(this.properties.novelManagerId)
+            if (res && res.isEnded) {
+                let params = {
+                    chapterNo: getApp().globalData.chapterIndex,
+                    novelId: getApp().globalData.novelId,
+                    type: 1
+                }
+                unlockChapter(params).then(res => {
+                    console.log(res)
+                    novelManager.paymentCompleted()
+                    console.log('章节解锁成功', this.properties.chapterIndex)
+                })
+                this.videoAd.offClose()
+            } else {
+                novelManager.closeChargeDialog()
+                console.log('关闭解锁组件')
+            }
+        })
+    }
+}
+
+// 激励视频展示
+function rewardVideoPlayFn() {
+    wx.showLoading({
+        title: '解锁视频中',
+    })
+    videoAd.show().then(() => {
+        console.log('激励视频播放成功')
+    }).catch((err) => {
+        console.log('激励视频播放失败', err)
+    }).then(() => {
+        wx.hideLoading()
+    })
+}

+ 1 - 3
app.json

@@ -20,7 +20,6 @@
     "navigationBarTextStyle": "black"
   },
   "usingComponents": {
-    "charge-dialog": "/components/charge-dialog/charge-dialog",
     "full-screen": "/components/full-screen/full-screen"
   },
   "plugins": {
@@ -29,7 +28,6 @@
       "provider": "wx293c4b6097a8a4d0",
       "genericsImplementation": {
         "novel": {
-          "charge-dialog": "/components/charge-dialog/charge-dialog",
           "full-screen": "/components/full-screen/full-screen"
         }
       }
@@ -77,4 +75,4 @@
   "componentFramework": "glass-easel",
   "sitemapLocation": "sitemap.json",
   "lazyCodeLoading": "requiredComponents"
-}
+}

+ 3 - 3
utils/util.js

@@ -1,7 +1,7 @@
 import {
     recordLog
 } from '../api/api.js'
-import {apple} from '../config/config'
+import { apple } from '../config/config'
 
 const formatTime = date => {
     const year = date.getFullYear()
@@ -33,7 +33,7 @@ export const goToBookDetail = (options) => {
     // 根据不同参数构建不同的URL
     if (options.wxBookId) {
         // 微信书籍ID跳转
-        url = `plugin-private://wx293c4b6097a8a4d0/pages/novel/index?share=1&bookId=${options.wxBookId}`;
+        url = `plugin-private://wx293c4b6097a8a4d0/pages/novel/index?share=1&bookId=${options.wxBookId}&disableAutoShowChargeDialog=1`;
 
         // 如果有章节ID,添加到URL
         if (options.chapterId) {
@@ -131,7 +131,7 @@ export function deepCopy(data) {
 export const SendEvent = (eventName, eventParam, self) => {
     try {
         const app = self || getApp()
-        let eParam = {...eventParam}
+        let eParam = { ...eventParam }
         eParam.from = app.globalData.from || ''
 
         const { brand, model, system, platform } = wx.getDeviceInfo()