Skip to content

Commit 7ed7dd3

Browse files
docs: ✏️ 微信小程序演示demo提供激励视频广告页面 (#783)
1 parent be324c9 commit 7ed7dd3

File tree

4 files changed

+149
-5
lines changed

4 files changed

+149
-5
lines changed

src/components/page-wraper/page-wraper.vue

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<wd-config-provider :theme="theme" :theme-vars="isRed ? themeVars : {}">
3-
<view class="page-wraper">
3+
<view class="page-wraper" @click="closeOutside">
44
<wd-cell title="切换暗黑" title-width="240px" center v-if="showDarkMode">
55
<wd-switch v-model="isDark" />
66
</wd-cell>
@@ -24,6 +24,16 @@
2424
</view>
2525
<wd-notify />
2626
<wd-toast />
27+
<!-- #ifdef MP-WEIXIN -->
28+
<wd-fab v-model:active="fabActive" draggable type="error" :gap="{ bottom: 58 }" v-if="enableRewardFab">
29+
<wd-button type="error" round @click="goToReward">
30+
<view style="display: flex; align-items: center">
31+
<wd-icon name="thumb-up" size="22px"></wd-icon>
32+
观看激励视频广告为我助力
33+
</view>
34+
</wd-button>
35+
</wd-fab>
36+
<!-- #endif -->
2737
</wd-config-provider>
2838
</template>
2939
<script lang="ts">
@@ -37,24 +47,32 @@ export default {
3747
</script>
3848
<script lang="ts" setup>
3949
import { computed, ref, onMounted, nextTick } from 'vue'
40-
import { setNotifyDefaultOptions, type ConfigProviderThemeVars } from '@/uni_modules/wot-design-uni'
50+
import { setNotifyDefaultOptions, useQueue, type ConfigProviderThemeVars } from '@/uni_modules/wot-design-uni'
4151
import { useDark } from '../../store'
4252
4353
interface Props {
4454
showDarkMode?: boolean
4555
safeAreaInsetBottom?: boolean
4656
useWxAd?: boolean
57+
useRewardFab?: boolean
4758
}
4859
4960
const props = withDefaults(defineProps<Props>(), {
5061
showDarkMode: false,
5162
safeAreaInsetBottom: true,
52-
useWxAd: process.env.NODE_ENV === 'development' ? false : true
63+
useWxAd: process.env.NODE_ENV === 'development' ? false : true,
64+
useRewardFab: false
65+
})
66+
67+
const enableRewardFab = computed(() => {
68+
return props.useRewardFab && (process.env.NODE_ENV === 'development' ? false : true)
5369
})
5470
5571
const darkMode = useDark()
72+
const { closeOutside } = useQueue()
5673
const isDark = ref<boolean>(false)
5774
const isRed = ref<boolean>(false)
75+
const fabActive = ref<boolean>(false)
5876
// #ifdef MP-WEIXIN
5977
// 横幅广告和格子广告可以共存,但插屏广告展示时,不显示横幅广告和格子广告
6078
const showWxAd = ref<boolean>(Math.random() > 0.5) // 横幅广告
@@ -71,6 +89,13 @@ const theme = computed(() => {
7189
return darkMode.isDark.value || isDark.value ? 'dark' : 'light'
7290
})
7391
92+
function goToReward() {
93+
fabActive.value = false
94+
uni.navigateTo({
95+
url: '/pages/wxRewardAd/Index'
96+
})
97+
}
98+
7499
onMounted(() => {
75100
setNotifyDefaultOptions({
76101
onClick: (event) => console.log('onClick', event),
@@ -82,10 +107,10 @@ onMounted(() => {
82107
if (uni.createInterstitialAd && showWxAd3.value && props.useWxAd) {
83108
interstitialAd = uni.createInterstitialAd({ adUnitId: 'adunit-fc8522e2b1185c89' })
84109
nextTick(() => {
85-
console.log(interstitialAd)
86110
interstitialAd && interstitialAd.show()
87111
})
88112
}
113+
89114
// #endif
90115
})
91116
</script>

src/pages.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,16 @@
831831
},
832832
"navigationBarTitleText": "floatingPanel 浮动面板"
833833
}
834+
},
835+
{
836+
"path": "pages/wxRewardAd/Index",
837+
"name": "wxRewardAd",
838+
"style": {
839+
"mp-alipay": {
840+
"allowsBounceVertical": "NO"
841+
},
842+
"navigationBarTitleText": "感谢激励"
843+
}
834844
}
835845
],
836846
// "tabBar": {

src/pages/index/Index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<page-wraper :use-wx-ad="false">
2+
<page-wraper :use-wx-ad="false" :use-reward-fab="true">
33
<view class="page">
44
<view class="page__hd">
55
<view class="page__title">

src/pages/wxRewardAd/Index.vue

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<!--
2+
* @Author: weisheng
3+
* @Date: 2024-12-12 21:33:43
4+
* @LastEditTime: 2024-12-12 22:52:14
5+
* @LastEditors: weisheng
6+
* @Description:
7+
* @FilePath: /wot-design-uni/src/pages/wxRewardAd/Index.vue
8+
* 记得注释
9+
-->
10+
<template>
11+
<page-wraper :use-wx-ad="false">
12+
<div class="ad-completion-message">
13+
<div class="message-title">感谢你愿意来观看广告!</div>
14+
<div class="message-content">观看成功就已经成功为我助力!</div>
15+
<div class="message-content">请继续使用我们的组件,您的支持对我们非常重要!</div>
16+
<view class="button-group">
17+
<wd-button type="success" block @click="back">返回使用</wd-button>
18+
<wd-button type="error" block @click="showAd">再次观看</wd-button>
19+
</view>
20+
</div>
21+
</page-wraper>
22+
</template>
23+
<script setup lang="ts">
24+
import { useToast } from '@/uni_modules/wot-design-uni'
25+
import { onMounted } from 'vue'
26+
const { loading: showLoading, close: closeLoading } = useToast()
27+
let rewardVideoAd: UniApp.InterstitialAdContext | null = null
28+
29+
function showRewardAd() {
30+
showLoading({ msg: '正在加载激励视频...' })
31+
rewardVideoAd &&
32+
rewardVideoAd
33+
.show()
34+
.then(() => {
35+
closeLoading()
36+
})
37+
.catch(() => {
38+
rewardVideoAd!
39+
.load()
40+
.then(() =>
41+
rewardVideoAd!.show().finally(() => {
42+
closeLoading()
43+
})
44+
)
45+
.catch((err) => {
46+
closeLoading()
47+
console.log('激励视频 广告显示失败')
48+
})
49+
})
50+
}
51+
52+
onMounted(() => {
53+
// 接入微信小程序激励视频广告
54+
if (uni.createRewardedVideoAd) {
55+
rewardVideoAd = uni.createRewardedVideoAd({ adUnitId: 'adunit-91e0e9b07b57557a' })
56+
rewardVideoAd.onLoad(() => {
57+
console.log('激励视频 广告加载成功')
58+
})
59+
rewardVideoAd.onError((err) => {
60+
console.log('激励视频 广告加载失败', err)
61+
})
62+
showRewardAd()
63+
}
64+
})
65+
66+
function back() {
67+
uni.navigateBack()
68+
}
69+
70+
function showAd() {
71+
showRewardAd()
72+
}
73+
</script>
74+
<style lang="scss" scoped>
75+
.ad-completion-message {
76+
text-align: center;
77+
padding: 20px;
78+
}
79+
80+
.message-title {
81+
font-size: 24px;
82+
color: #4caf50;
83+
}
84+
85+
.message-content {
86+
font-size: 16px;
87+
margin: 16px 0;
88+
}
89+
90+
.continue-button {
91+
padding: 10px 20px;
92+
font-size: 16px;
93+
background-color: #007bff;
94+
color: white;
95+
border: none;
96+
border-radius: 5px;
97+
cursor: pointer;
98+
}
99+
100+
.continue-button:hover {
101+
background-color: #0056b3;
102+
}
103+
104+
.button-group {
105+
display: flex;
106+
justify-content: space-around;
107+
margin-top: 20px;
108+
}
109+
</style>

0 commit comments

Comments
 (0)