/* * @Author: xiaolin * @Date: 2022-10-12 12:09:56 * @LastEditors: xiaolin * @LastEditTime: 2022-10-26 18:09:45 * @Description: file content * @FilePath: \image.platform.com\xyxJsSdkV2\src\ad.js */ import request from "../utils/request.js" import {randomString} from "../utils/index.js" /** * @description: 创建广告 * @return {*} */ let ad_life_id = null export const createAd = async (data) => { try { console.log("createAd入参" + JSON.stringify(data)) const res = await request.api("COMMON", { do: "getWxGameAd", ...data, }) ad_life_id = randomString(16) switch (res.ad_type) { case "video": //激励视频广告 await createRewardedVideoAd(res) break case "interstitial": //插屏广告 await createInterstitialAd(res) break case "grid": //(格子) 广告 await createGridAd(res) break case "custom": //原生模板广告 await createCustomAd(res) break case "banner": default: //banner 广告 await createBannerAd(res) break } return Promise.resolve(res) } catch (error) { return Promise.reject(error) } } //激励视频广告 const createRewardedVideoAd = (res) => { return new Promise((resolve, reject) => { let rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: res.ad_unit_id, }) rewardedVideoAd .show() .then(() => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10003, content: "", ad_life_id: ad_life_id, }) // resolve(); }) .catch((error) => { rewardedVideoAd.load() .then(() => { rewardedVideoAd.show().then(() => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10003, content: "", ad_life_id: ad_life_id, }) }).catch((error) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10004, content: JSON.stringify(error), ad_life_id: ad_life_id, }) // rewardedVideoAd.load(); reject(error) }) }) }) // 此处有毒,千万别按微信文档上方法,不然onLoad会多次叠加,需要用offLoad消除事件 if (rewardedVideoAd.loadHandler) { rewardedVideoAd.offLoad(rewardedVideoAd.loadHandler) } rewardedVideoAd.loadHandler = function () { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10001, content: "", ad_life_id: ad_life_id, }) } rewardedVideoAd.onLoad(rewardedVideoAd.loadHandler) rewardedVideoAd.onError((error) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10002, content: JSON.stringify(error), ad_life_id: ad_life_id, }) rewardedVideoAd.offError() reject(error) }) // 此处有毒,千万别按微信文档上方法,不然onClose会多次叠加,需要用offClose消除事件 if (rewardedVideoAd.closeHandler) { rewardedVideoAd.offClose(rewardedVideoAd.closeHandler) } rewardedVideoAd.closeHandler = function (rest) { console.log(rest) // 用户点击了【关闭广告】按钮 if (rest && rest.isEnded || rest === undefined) { // 正常播放结束,可以下发奖励 upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10005, content: JSON.stringify({ msg: '正常播放结束' }), ad_life_id: ad_life_id, }) resolve(rest) } else { //提前关闭小程序 upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10005, content: JSON.stringify({ msg: '提前关闭' }), ad_life_id: ad_life_id, }) reject(rest) } } rewardedVideoAd.onClose(rewardedVideoAd.closeHandler) }) } //插屏广告 const createInterstitialAd = (res) => { return new Promise((resolve, reject) => { let interstitialAd = wx.createInterstitialAd({ adUnitId: res.ad_unit_id, }) interstitialAd .show() .then(() => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10003, content: "", ad_life_id: ad_life_id, }) resolve() }) .catch((error) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10004, content: JSON.stringify(error), ad_life_id: ad_life_id, }) reject(error) }) interstitialAd.onLoad(() => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10001, content: "", ad_life_id: ad_life_id, }) }) interstitialAd.onError((error) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10002, content: JSON.stringify(error), ad_life_id: ad_life_id, }) interstitialAd.offError() reject(error) }) interstitialAd.onClose(rest => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10005, content: JSON.stringify(rest), ad_life_id: ad_life_id, }) }) }) } //(格子) 广告 const createGridAd = (res) => { return new Promise((resolve, reject) => { let GridAd = wx.createGridAd({ adUnitId: res.ad_unit_id, adTheme: res.adTheme, gridCount: res.gridCount, style: { left: res.left, top: res.top, width: res.width, opacity: res.opacity, }, }) GridAd.show() .then(() => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10003, content: "", ad_life_id: ad_life_id, }) resolve() }) .catch((error) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10004, content: JSON.stringify(error), ad_life_id: ad_life_id, }) reject(error) }) GridAd.onLoad(() => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10001, content: "", ad_life_id: ad_life_id, }) }) GridAd.onError((error) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10002, content: JSON.stringify(error), ad_life_id: ad_life_id, }) GridAd.offError() reject(error) }) GridAd.onResize((resq) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10006, content: JSON.stringify({ width: resq.width, height: resq.height }), ad_life_id: ad_life_id, }) }) }) } //原生模板广告 const createCustomAd = (res) => { return new Promise((resolve, reject) => { let customAd = wx.createCustomAd({ adUnitId: res.ad_unit_id, style: { left: res.left, top: res.top, width: res.width, // 用于设置组件宽度,只有部分模板才支持,如矩阵格子模板 fixed: true, // fixed 只适用于小程序环境 }, }) customAd .show() .then(() => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10003, content: "", ad_life_id: ad_life_id, }) resolve() }) .catch((error) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10004, content: JSON.stringify(error), ad_life_id: ad_life_id, }) reject(error) }) customAd.onLoad(() => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10001, content: "", ad_life_id: ad_life_id, }) }) customAd.onError((error) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10002, content: JSON.stringify(error), ad_life_id: ad_life_id, }) customAd.offError() reject(error) }) customAd.onClose(rest => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10005, content: JSON.stringify(rest), ad_life_id: ad_life_id, }) }) }) } //banner 广告 const createBannerAd = (res) => { return new Promise((resolve, reject) => { let bannerAd = wx.createBannerAd({ adUnitId: res.ad_unit_id, style: { left: res.left, top: res.top, width: res.width, }, }) bannerAd .show() .then(() => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10003, content: "", ad_life_id: ad_life_id, }) resolve() }) .catch((error) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10004, content: JSON.stringify(error), ad_life_id: ad_life_id, }) reject(error) }) bannerAd.onLoad(() => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10001, content: "", ad_life_id: ad_life_id, }) }) bannerAd.onError((error) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 0, code: 10002, content: JSON.stringify(error), ad_life_id: ad_life_id, }) bannerAd.offError() reject(error) }) bannerAd.onResize((resq) => { upWxGameAdLog({ ad_unit_id: res.ad_unit_id, ad_type: res.ad_type, result: 1, code: 10006, content: JSON.stringify({ width: resq.width, height: resq.height }), ad_life_id: ad_life_id, }) }) }) } // 流量变现打点日志 export const upWxGameAdLog = async (data) => { try { const res = await request.api("COMMON", { do: "upWxGameAdLog", ...data, }) return Promise.resolve(res) } catch (error) { return Promise.reject(error) } }