| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- /*
- * @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)
- }
- }
|