Browse Source

联调激励视频任务

sunxf 2 years ago
parent
commit
9f7efa6688
3 changed files with 45 additions and 25 deletions
  1. 40 22
      component/rewardedVideo/index.js
  2. 3 1
      pages/my/index.js
  3. 2 2
      pages/my/index.wxml

+ 40 - 22
component/rewardedVideo/index.js

@@ -1,3 +1,6 @@
+import {
+    submitTask
+} from '~/api/global'
 var videoAd = null;
 Component({
     /**
@@ -8,6 +11,32 @@ Component({
     },
     lifetimes: {
         attached() {
+            this.createVideo()
+        }
+    },
+    /**
+     * 组件的初始数据
+     */
+    data: {
+
+    },
+
+    /**
+     * 组件的方法列表
+     */
+    methods: {
+        rewardedVideo() {
+            if (!videoAd) {
+                this.createVideo()
+            }
+            videoAd.show().catch(err => {
+                // 失败重试
+                videoAd.load()
+                    .then(() => videoAd.show())
+            })
+        },
+        // 创建广告对象并监听
+        createVideo() {
             if (wx.createRewardedVideoAd) {
                 // 加载激励视频广告
                 videoAd = wx.createRewardedVideoAd({
@@ -19,9 +48,19 @@ Component({
                 })
                 // 监听关闭
                 videoAd.onClose((status) => {
+                    videoAd.offClose()
+                    videoAd = null
                     if (status && status.isEnded || status === undefined) {
                         // 正常播放结束,下发奖励
-                        // continue you code
+                        submitTask({
+                            id: 3
+                        }).then(res => {
+                            wx.showToast({
+                                icon: 'none',
+                                title: '观看成功!',
+                            })
+                            this.triggerEvent('taskOver')
+                        })
                     } else {
                         // 播放中途退出,进行提示
                         wx.showToast({
@@ -32,26 +71,5 @@ Component({
                 })
             }
         }
-    },
-    /**
-     * 组件的初始数据
-     */
-    data: {
-
-    },
-
-    /**
-     * 组件的方法列表
-     */
-    methods: {
-        rewardedVideo() {
-            if (videoAd) {
-                videoAd.show().catch(err => {
-                    // 失败重试
-                    videoAd.load()
-                        .then(() => videoAd.show())
-                })
-            }
-        }
     }
 })

+ 3 - 1
pages/my/index.js

@@ -56,7 +56,6 @@ Page({
     },
     async getTasks() {
         let tasks = await getTasks()
-        console.log(tasks);
         this.setData({
             tasks
         })
@@ -114,6 +113,9 @@ Page({
         });
     },
     rewardedVideo() {
+        if (this.data.tasks.length != 3 || this.data.tasks[2].completed) {
+            return
+        }
         this.selectComponent('#advert').rewardedVideo();
     }
 })

+ 2 - 2
pages/my/index.wxml

@@ -144,7 +144,7 @@
                 </view>
             </view>
             <view class="taskRightBox">
-                <view class="taskRight {{tasks[2].completed?' taskRight-close':''}}" bindtap=" rewardedVideo">去观看
+                <view class="taskRight {{tasks[2].completed?'taskRight-close':''}}" bindtap="rewardedVideo">去观看
                 </view>
                 <view class="taskSurplus">
                     剩余次数:{{tasks[2].completedNum}}/{{tasks[2].num}}
@@ -153,4 +153,4 @@
         </view>
     </view>
 </view>
-<rewardedVideo id='advert'></rewardedVideo>
+<rewardedVideo id='advert' bind:taskOver="getTasks"></rewardedVideo>