说明

appInfo至少需要包含三个字段,id,WxRewardedVideoID,WxInterstitialID

调用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var require_stdin = __commonJS({
"<stdin>"(exports) {
exports.WxAd.show(
"WxInterstitial",
function() {
},
function(err) {
console.log(err);
}
);
}
});
var stdin_default = require_stdin();
export {
stdin_default as default
};

代码段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
request, toastMessage
}
from '@/common/utils.js';
export class InsAd {
constructor() {
this.appInfo = null;
//微信小程序信息 // this.WxRewardedVideo = null;
//微信激励视频
}
getInfo() {
const t = this;
return new Promise(function (resolve, reject) {
if (t.appInfo&&t.appInfo.id&&/^\d+$/.test(t.appInfo.id)) {
return resolve(t.appInfo);
}
return request('xxxxxxxxxxxx', {
}
, {
loading:false
}
).then((e) => {
if (e&&e.data&&e.data.appInfo) {
t.appInfo = e.data.appInfo;
return resolve(e.data.appInfo);
}
return reject( {
errMsg:'支付广告参数获取失败'
});
}
).catch ((e) => {
return reject( {
errMsg:e.message
});
}
)
}
)
}
show(type, susFun, errFun) {
const t = this;
showLoading('加载中...');
t.load(type).then(() => {
t[type].susFun = susFun
t[type].show().then(() => {
hideLoading();
}
).catch (err => {
return (typeof errFun == 'function')?errFun(err):false;
});
}
).catch ((err) => {
return (typeof errFun == 'function')?errFun(err):false;
}
)
}
load(type) {
const t = this;
return new Promise((resolve, reject) => {
//如果已经初始化load了,则直接返回
if (t[type]&&t[type].$exist) {
return resolve(t[type]);
}
// 获取初始化实例
t.getIns(type).then(() => {
t[type]['expire'] = setTimeout(() => {
if (!t[type].$exist) {
reject( {
errMsg:'广告加载超时'
});
clearTimeout(t[type]['expire']);
return;
}
}
, 10000);
t[type].onClose(function (v) {
if (v&&v.isEnded&&(typeof t[type].susFun == 'function')) {
t[type].susFun(t[type]);
}
});
t[type].onError((err) => {
return reject(err);
}
)
t[type].onLoad((ppp) => {
if (!t[type].$exist) {
t[type].$exist = true;
return resolve(t[type]);
}
}
)
}
).catch ((err) => {
return reject(err);
}
)
}
)
}
getIns(type) {
const t = this;
return new Promise((resolve, reject) => {
if (t[type]) {
return resolve(t[type]);
}
t.getInfo().then(() => {
if (type=='WxRewardedVideo'&&t.appInfo&&t.appInfo.WxRewardedVideoID&&uni.createRewardedVideoAd) {
const AdID = t.appInfo.WxRewardedVideoID;
if ((typeof AdID == 'string')&&(AdID.length>0)) {
t[type] = uni.createRewardedVideoAd( {
adpid:AdID, adUnitId:AdID
});
return resolve(t[type]);
}
}
else if (type=='WxInterstitial'&&t.appInfo&&t.appInfo.WxInterstitialID&&uni.createInterstitialAd) {
const AdID = t.appInfo.WxInterstitialID;
if ((typeof AdID == 'string')&&(AdID.length>0)) {
t[type] = uni.createInterstitialAd( {
adpid:AdID, adUnitId:AdID
});
return resolve(t[type]);
}
}
else {
return reject('暂不支持该类型广告');
}
}
).catch ((err) => {
return reject(err);
}
)
}
)
}
}
function showLoading(msg)
{
uni.showLoading( {
title:msg
}
)
}
function hideLoading()
{
try {
uni.hideLoading();
}
catch (e) {
//TODO handle the exception
}
}

本文早期发布于个人 CSDN 博客:查看原文

站内搜索

没有找到内容!