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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| export class WxCustomBigAd { static showStatus: boolean = false; static loadStatus: boolean = false; static cache: WechatMinigame.CustomAd = null; static curAdUnitId: string = ""; static windowWidth: number; static windowHeight: number; static pixelRatio: number;
private static style: WechatMinigame.CustomAdStyle = null;
static getStyle(): WechatMinigame.CustomAdStyle { if (!WxCustomBigAd.style) { let width:number = WxCustomBigAd.windowWidth; width = Math.max(width,300); const left:number = (WxCustomBigAd.windowWidth -width) * 0.5 WxCustomBigAd.style = { top: (WxCustomBigAd.windowHeight - width *1.25 * 1.2) * 0.5, left:left, width: width, fixed: false }; WxCustomBigAd.showStatus = true; } console.log("WxCustomBigAd.style", JSON.stringify(WxCustomBigAd.style)); return WxCustomBigAd.style; }
static init(obj: {adUnitId:string} = {adUnitId:""}): Promise<void> { return new Promise((resolve, reject) => { let systemInfo: any = wx.getSystemInfoSync(); console.log("systemInfo", JSON.stringify(systemInfo, null, 2)); WxCustomBigAd.windowHeight = systemInfo.windowHeight; WxCustomBigAd.windowWidth = systemInfo.windowWidth; WxCustomBigAd.pixelRatio = systemInfo.pixelRatio; if (obj.adUnitId) { WxCustomBigAd.curAdUnitId = obj.adUnitId; } WxCustomBigAd.load().then((t) => { console.log("原生模板广告加载耗时", new Date().getTime() - t, "ms"); resolve() }).catch(err => { console.log(err) reject(); }); }) }
static load(): Promise<any> { return new Promise((resolve, reject) => { const t = new Date().getTime(); const style: any = WxCustomBigAd.getStyle(); const adUnitId: any = WxCustomBigAd.curAdUnitId; WxCustomBigAd.cache && WxCustomBigAd.cache.destroy(); WxCustomBigAd.cache = wx.createCustomAd({adUnitId: adUnitId, style: style, adIntervals: 30}); WxCustomBigAd.loadStatus = false; WxCustomBigAd.cache.onLoad(WxCustomBigAd.onLoad); WxCustomBigAd.cache.onError(WxCustomBigAd.onError); WxCustomBigAd.cache.onHide(WxCustomBigAd.onHide); WxCustomBigAd.cache.onClose(WxCustomBigAd.onClose); resolve(t); }) }
static show(options: any = {}, end: Function = null): void { WxCustomBigAd.showStatus = true; if (options.adUnitId) { WxCustomBigAd.init(options).then(() => { WxCustomBigAd.show({}, end); }); } else if (options.realtime) { WxCustomBigAd.load().then((t) => { console.log("原生模板广告加载耗时", new Date().getTime() - t, "ms"); WxCustomBigAd.show({}, end); }).catch(err => console.log(err)); } else { const t = new Date().getTime(); WxCustomBigAd.cache.show().then(() => { console.log("原生模板广告显示成功耗时", new Date().getTime() - t, "ms"); end && end(true); }).catch(err => { console.log(err) end && end(false); }); } }
static hide(): void { WxCustomBigAd.showStatus = false; WxCustomBigAd.loadStatus = false; if (WxCustomBigAd.cache&& WxCustomBigAd.cache.isShow()) WxCustomBigAd.cache.hide().then(() => console.log('原生模板广告隐藏!!')); }
static destroy(): void { if (WxCustomBigAd.cache) WxCustomBigAd.cache.destroy(); }
static onLoad(): void { console.log('原生模板广告加载成功'); WxCustomBigAd.cache.offLoad(WxCustomBigAd.onLoad); if (WxCustomBigAd.showStatus === false) { WxCustomBigAd.hide(); return; }else { WxCustomBigAd.loadStatus = true; WxCustomBigAd.showStatus = true; } }
static onClose(): void { console.log('原生模板广告关闭') WxCustomBigAd.cache.offClose(WxCustomBigAd.onClose); WxCustomBigAd.cache.offHide(WxCustomBigAd.onHide); WxCustomBigAd.cache.offError(WxCustomBigAd.onError); WxCustomBigAd.cache = null; WxCustomBigAd.showStatus = false; WxCustomBigAd.loadStatus = false; }
static onHide(): void { console.log('原生模板广告隐藏'); WxCustomBigAd.showStatus = false; WxCustomBigAd.loadStatus = false; }
static onError(res: any): void { console.error("原生模板广告加载错误", res); WxCustomBigAd.cache.offError(WxCustomBigAd.onError); switch (res.errCode) { case 1002: case 1005: case 1006: case 1007: case 1008: break; case 1000: case 1001: case 1003: case 1004: default: setTimeout(() => { WxCustomBigAd.load(); }, 3000); break; } } }
|