ad.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * @Author: xiaolin
  3. * @Date: 2022-10-12 12:09:56
  4. * @LastEditors: xiaolin
  5. * @LastEditTime: 2022-10-26 18:09:45
  6. * @Description: file content
  7. * @FilePath: \image.platform.com\xyxJsSdkV2\src\ad.js
  8. */
  9. import request from "../utils/request.js"
  10. import {randomString} from "../utils/index.js"
  11. /**
  12. * @description: 创建广告
  13. * @return {*}
  14. */
  15. let ad_life_id = null
  16. export const createAd = async (data) => {
  17. try {
  18. console.log("createAd入参" + JSON.stringify(data))
  19. const res = await request.api("COMMON", {
  20. do: "getWxGameAd",
  21. ...data,
  22. })
  23. ad_life_id = randomString(16)
  24. switch (res.ad_type) {
  25. case "video":
  26. //激励视频广告
  27. await createRewardedVideoAd(res)
  28. break
  29. case "interstitial":
  30. //插屏广告
  31. await createInterstitialAd(res)
  32. break
  33. case "grid":
  34. //(格子) 广告
  35. await createGridAd(res)
  36. break
  37. case "custom":
  38. //原生模板广告
  39. await createCustomAd(res)
  40. break
  41. case "banner":
  42. default:
  43. //banner 广告
  44. await createBannerAd(res)
  45. break
  46. }
  47. return Promise.resolve(res)
  48. } catch (error) {
  49. return Promise.reject(error)
  50. }
  51. }
  52. //激励视频广告
  53. const createRewardedVideoAd = (res) => {
  54. return new Promise((resolve, reject) => {
  55. let rewardedVideoAd = wx.createRewardedVideoAd({
  56. adUnitId: res.ad_unit_id,
  57. })
  58. rewardedVideoAd
  59. .show()
  60. .then(() => {
  61. upWxGameAdLog({
  62. ad_unit_id: res.ad_unit_id,
  63. ad_type: res.ad_type,
  64. result: 1,
  65. code: 10003,
  66. content: "",
  67. ad_life_id: ad_life_id,
  68. })
  69. // resolve();
  70. })
  71. .catch((error) => {
  72. rewardedVideoAd.load()
  73. .then(() => {
  74. rewardedVideoAd.show().then(() => {
  75. upWxGameAdLog({
  76. ad_unit_id: res.ad_unit_id,
  77. ad_type: res.ad_type,
  78. result: 1,
  79. code: 10003,
  80. content: "",
  81. ad_life_id: ad_life_id,
  82. })
  83. }).catch((error) => {
  84. upWxGameAdLog({
  85. ad_unit_id: res.ad_unit_id,
  86. ad_type: res.ad_type,
  87. result: 0,
  88. code: 10004,
  89. content: JSON.stringify(error),
  90. ad_life_id: ad_life_id,
  91. })
  92. // rewardedVideoAd.load();
  93. reject(error)
  94. })
  95. })
  96. })
  97. // 此处有毒,千万别按微信文档上方法,不然onLoad会多次叠加,需要用offLoad消除事件
  98. if (rewardedVideoAd.loadHandler) {
  99. rewardedVideoAd.offLoad(rewardedVideoAd.loadHandler)
  100. }
  101. rewardedVideoAd.loadHandler = function () {
  102. upWxGameAdLog({
  103. ad_unit_id: res.ad_unit_id,
  104. ad_type: res.ad_type,
  105. result: 0,
  106. code: 10001,
  107. content: "",
  108. ad_life_id: ad_life_id,
  109. })
  110. }
  111. rewardedVideoAd.onLoad(rewardedVideoAd.loadHandler)
  112. rewardedVideoAd.onError((error) => {
  113. upWxGameAdLog({
  114. ad_unit_id: res.ad_unit_id,
  115. ad_type: res.ad_type,
  116. result: 0,
  117. code: 10002,
  118. content: JSON.stringify(error),
  119. ad_life_id: ad_life_id,
  120. })
  121. rewardedVideoAd.offError()
  122. reject(error)
  123. })
  124. // 此处有毒,千万别按微信文档上方法,不然onClose会多次叠加,需要用offClose消除事件
  125. if (rewardedVideoAd.closeHandler) {
  126. rewardedVideoAd.offClose(rewardedVideoAd.closeHandler)
  127. }
  128. rewardedVideoAd.closeHandler = function (rest) {
  129. console.log(rest)
  130. // 用户点击了【关闭广告】按钮
  131. if (rest && rest.isEnded || rest === undefined) {
  132. // 正常播放结束,可以下发奖励
  133. upWxGameAdLog({
  134. ad_unit_id: res.ad_unit_id,
  135. ad_type: res.ad_type,
  136. result: 1,
  137. code: 10005,
  138. content: JSON.stringify({
  139. msg: '正常播放结束'
  140. }),
  141. ad_life_id: ad_life_id,
  142. })
  143. resolve(rest)
  144. } else {
  145. //提前关闭小程序
  146. upWxGameAdLog({
  147. ad_unit_id: res.ad_unit_id,
  148. ad_type: res.ad_type,
  149. result: 1,
  150. code: 10005,
  151. content: JSON.stringify({
  152. msg: '提前关闭'
  153. }),
  154. ad_life_id: ad_life_id,
  155. })
  156. reject(rest)
  157. }
  158. }
  159. rewardedVideoAd.onClose(rewardedVideoAd.closeHandler)
  160. })
  161. }
  162. //插屏广告
  163. const createInterstitialAd = (res) => {
  164. return new Promise((resolve, reject) => {
  165. let interstitialAd = wx.createInterstitialAd({
  166. adUnitId: res.ad_unit_id,
  167. })
  168. interstitialAd
  169. .show()
  170. .then(() => {
  171. upWxGameAdLog({
  172. ad_unit_id: res.ad_unit_id,
  173. ad_type: res.ad_type,
  174. result: 1,
  175. code: 10003,
  176. content: "",
  177. ad_life_id: ad_life_id,
  178. })
  179. resolve()
  180. })
  181. .catch((error) => {
  182. upWxGameAdLog({
  183. ad_unit_id: res.ad_unit_id,
  184. ad_type: res.ad_type,
  185. result: 0,
  186. code: 10004,
  187. content: JSON.stringify(error),
  188. ad_life_id: ad_life_id,
  189. })
  190. reject(error)
  191. })
  192. interstitialAd.onLoad(() => {
  193. upWxGameAdLog({
  194. ad_unit_id: res.ad_unit_id,
  195. ad_type: res.ad_type,
  196. result: 0,
  197. code: 10001,
  198. content: "",
  199. ad_life_id: ad_life_id,
  200. })
  201. })
  202. interstitialAd.onError((error) => {
  203. upWxGameAdLog({
  204. ad_unit_id: res.ad_unit_id,
  205. ad_type: res.ad_type,
  206. result: 0,
  207. code: 10002,
  208. content: JSON.stringify(error),
  209. ad_life_id: ad_life_id,
  210. })
  211. interstitialAd.offError()
  212. reject(error)
  213. })
  214. interstitialAd.onClose(rest => {
  215. upWxGameAdLog({
  216. ad_unit_id: res.ad_unit_id,
  217. ad_type: res.ad_type,
  218. result: 1,
  219. code: 10005,
  220. content: JSON.stringify(rest),
  221. ad_life_id: ad_life_id,
  222. })
  223. })
  224. })
  225. }
  226. //(格子) 广告
  227. const createGridAd = (res) => {
  228. return new Promise((resolve, reject) => {
  229. let GridAd = wx.createGridAd({
  230. adUnitId: res.ad_unit_id,
  231. adTheme: res.adTheme,
  232. gridCount: res.gridCount,
  233. style: {
  234. left: res.left,
  235. top: res.top,
  236. width: res.width,
  237. opacity: res.opacity,
  238. },
  239. })
  240. GridAd.show()
  241. .then(() => {
  242. upWxGameAdLog({
  243. ad_unit_id: res.ad_unit_id,
  244. ad_type: res.ad_type,
  245. result: 1,
  246. code: 10003,
  247. content: "",
  248. ad_life_id: ad_life_id,
  249. })
  250. resolve()
  251. })
  252. .catch((error) => {
  253. upWxGameAdLog({
  254. ad_unit_id: res.ad_unit_id,
  255. ad_type: res.ad_type,
  256. result: 0,
  257. code: 10004,
  258. content: JSON.stringify(error),
  259. ad_life_id: ad_life_id,
  260. })
  261. reject(error)
  262. })
  263. GridAd.onLoad(() => {
  264. upWxGameAdLog({
  265. ad_unit_id: res.ad_unit_id,
  266. ad_type: res.ad_type,
  267. result: 0,
  268. code: 10001,
  269. content: "",
  270. ad_life_id: ad_life_id,
  271. })
  272. })
  273. GridAd.onError((error) => {
  274. upWxGameAdLog({
  275. ad_unit_id: res.ad_unit_id,
  276. ad_type: res.ad_type,
  277. result: 0,
  278. code: 10002,
  279. content: JSON.stringify(error),
  280. ad_life_id: ad_life_id,
  281. })
  282. GridAd.offError()
  283. reject(error)
  284. })
  285. GridAd.onResize((resq) => {
  286. upWxGameAdLog({
  287. ad_unit_id: res.ad_unit_id,
  288. ad_type: res.ad_type,
  289. result: 1,
  290. code: 10006,
  291. content: JSON.stringify({
  292. width: resq.width,
  293. height: resq.height
  294. }),
  295. ad_life_id: ad_life_id,
  296. })
  297. })
  298. })
  299. }
  300. //原生模板广告
  301. const createCustomAd = (res) => {
  302. return new Promise((resolve, reject) => {
  303. let customAd = wx.createCustomAd({
  304. adUnitId: res.ad_unit_id,
  305. style: {
  306. left: res.left,
  307. top: res.top,
  308. width: res.width, // 用于设置组件宽度,只有部分模板才支持,如矩阵格子模板
  309. fixed: true, // fixed 只适用于小程序环境
  310. },
  311. })
  312. customAd
  313. .show()
  314. .then(() => {
  315. upWxGameAdLog({
  316. ad_unit_id: res.ad_unit_id,
  317. ad_type: res.ad_type,
  318. result: 1,
  319. code: 10003,
  320. content: "",
  321. ad_life_id: ad_life_id,
  322. })
  323. resolve()
  324. })
  325. .catch((error) => {
  326. upWxGameAdLog({
  327. ad_unit_id: res.ad_unit_id,
  328. ad_type: res.ad_type,
  329. result: 0,
  330. code: 10004,
  331. content: JSON.stringify(error),
  332. ad_life_id: ad_life_id,
  333. })
  334. reject(error)
  335. })
  336. customAd.onLoad(() => {
  337. upWxGameAdLog({
  338. ad_unit_id: res.ad_unit_id,
  339. ad_type: res.ad_type,
  340. result: 0,
  341. code: 10001,
  342. content: "",
  343. ad_life_id: ad_life_id,
  344. })
  345. })
  346. customAd.onError((error) => {
  347. upWxGameAdLog({
  348. ad_unit_id: res.ad_unit_id,
  349. ad_type: res.ad_type,
  350. result: 0,
  351. code: 10002,
  352. content: JSON.stringify(error),
  353. ad_life_id: ad_life_id,
  354. })
  355. customAd.offError()
  356. reject(error)
  357. })
  358. customAd.onClose(rest => {
  359. upWxGameAdLog({
  360. ad_unit_id: res.ad_unit_id,
  361. ad_type: res.ad_type,
  362. result: 1,
  363. code: 10005,
  364. content: JSON.stringify(rest),
  365. ad_life_id: ad_life_id,
  366. })
  367. })
  368. })
  369. }
  370. //banner 广告
  371. const createBannerAd = (res) => {
  372. return new Promise((resolve, reject) => {
  373. let bannerAd = wx.createBannerAd({
  374. adUnitId: res.ad_unit_id,
  375. style: {
  376. left: res.left,
  377. top: res.top,
  378. width: res.width,
  379. },
  380. })
  381. bannerAd
  382. .show()
  383. .then(() => {
  384. upWxGameAdLog({
  385. ad_unit_id: res.ad_unit_id,
  386. ad_type: res.ad_type,
  387. result: 1,
  388. code: 10003,
  389. content: "",
  390. ad_life_id: ad_life_id,
  391. })
  392. resolve()
  393. })
  394. .catch((error) => {
  395. upWxGameAdLog({
  396. ad_unit_id: res.ad_unit_id,
  397. ad_type: res.ad_type,
  398. result: 0,
  399. code: 10004,
  400. content: JSON.stringify(error),
  401. ad_life_id: ad_life_id,
  402. })
  403. reject(error)
  404. })
  405. bannerAd.onLoad(() => {
  406. upWxGameAdLog({
  407. ad_unit_id: res.ad_unit_id,
  408. ad_type: res.ad_type,
  409. result: 0,
  410. code: 10001,
  411. content: "",
  412. ad_life_id: ad_life_id,
  413. })
  414. })
  415. bannerAd.onError((error) => {
  416. upWxGameAdLog({
  417. ad_unit_id: res.ad_unit_id,
  418. ad_type: res.ad_type,
  419. result: 0,
  420. code: 10002,
  421. content: JSON.stringify(error),
  422. ad_life_id: ad_life_id,
  423. })
  424. bannerAd.offError()
  425. reject(error)
  426. })
  427. bannerAd.onResize((resq) => {
  428. upWxGameAdLog({
  429. ad_unit_id: res.ad_unit_id,
  430. ad_type: res.ad_type,
  431. result: 1,
  432. code: 10006,
  433. content: JSON.stringify({
  434. width: resq.width,
  435. height: resq.height
  436. }),
  437. ad_life_id: ad_life_id,
  438. })
  439. })
  440. })
  441. }
  442. // 流量变现打点日志
  443. export const upWxGameAdLog = async (data) => {
  444. try {
  445. const res = await request.api("COMMON", {
  446. do: "upWxGameAdLog",
  447. ...data,
  448. })
  449. return Promise.resolve(res)
  450. } catch (error) {
  451. return Promise.reject(error)
  452. }
  453. }