ATInterstitial.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { DataEventId } from "../../../../../game/data/GameData";
  2. import { zgSingleton } from "../../zgSingleton";
  3. import ATSDKMgr from "./ATSDKMgr";
  4. /**
  5. * inter插屏广告
  6. * @description
  7. * @author 冯聪
  8. */
  9. export default class ATInterstitial extends zgSingleton {
  10. /** */
  11. private ATInterstitialJS: any;
  12. /** 插屏广告类型 0 插屏 1全屏 */
  13. private interAdType: number = -1;
  14. /** 插屏广告状态 */
  15. private interAdState: InterAdState = 0;
  16. /** 是否加载完显示 */
  17. private ifLoadShow: boolean = false;
  18. private singleClick = false;
  19. private singleClickFull = false;
  20. /** 初始化 */
  21. init() {
  22. this.ATInterstitialJS = cc["ATInterstitialJS"];
  23. this.ATInterstitialJS.proxy = this;;
  24. this.ATInterstitialJS.init();
  25. if (this.interAdState == InterAdState.Unload) {
  26. this.loadInter(0);
  27. this.loadInter(1);
  28. }
  29. }
  30. /** 广告位id
  31. * @param 0 插屏 1 全屏
  32. */
  33. placementID() {
  34. if (this.interAdType == 0) {
  35. if (cc.sys.os === cc.sys.OS_IOS) {
  36. return ATSDKMgr.topon_inter_placementId;
  37. } else if (cc.sys.os === cc.sys.OS_ANDROID) {
  38. return ATSDKMgr.topon_inter_placementId;
  39. }
  40. }
  41. else {
  42. if (cc.sys.os === cc.sys.OS_IOS) {
  43. return ATSDKMgr.topon_fullinter_placementId;
  44. } else if (cc.sys.os === cc.sys.OS_ANDROID) {
  45. return ATSDKMgr.topon_fullinter_placementId;
  46. }
  47. }
  48. }
  49. /**
  50. * 检查当前广告类型
  51. * @param placementId 当前广告id
  52. * @returns 0 插屏 1 全屏
  53. */
  54. checkPlacementIDType(placementId) {
  55. if (cc.sys.os === cc.sys.OS_IOS) {
  56. if (placementId == ATSDKMgr.topon_inter_placementId) {
  57. return 0;
  58. }
  59. else if (placementId == ATSDKMgr.topon_fullinter_placementId) {
  60. return 1;
  61. }
  62. } else if (cc.sys.os === cc.sys.OS_ANDROID) {
  63. if (placementId == ATSDKMgr.topon_inter_placementId) {
  64. return 0;
  65. }
  66. else if (placementId == ATSDKMgr.topon_fullinter_placementId) {
  67. return 1;
  68. }
  69. }
  70. }
  71. //广告逻辑部分------------------------------------------------------------------------
  72. /** 检测是否插屏预备好了 */
  73. hasAdReady() {
  74. return this.ATInterstitialJS.hasAdReady();
  75. }
  76. /**
  77. * 加载插屏广告
  78. * @param type 0 插屏 1 全屏
  79. */
  80. loadInter(type: number, ifLoadShow: boolean = false) {
  81. this.ifLoadShow = ifLoadShow;
  82. this.interAdState = InterAdState.Loading;
  83. this.ATInterstitialJS.loadInter(type);
  84. }
  85. /**
  86. * 显示插屏广告
  87. * @param type 0 插屏 1 全屏
  88. */
  89. showInter(type: number) {
  90. if (this.interAdState == InterAdState.Show && type == this.interAdType) {
  91. //正在显示不重复弹出
  92. console.log('当前插屏类型正在展示');
  93. }
  94. else {
  95. this.interAdType = type;
  96. if (this.hasAdReady()) {
  97. this.interAdState = InterAdState.Show;
  98. this.ATInterstitialJS.showInter(type);
  99. }
  100. else {
  101. //如果正在加载中,则不处理
  102. if (this.interAdState == InterAdState.Loading) {
  103. //正在加载不重复加载
  104. this.ifLoadShow = true;
  105. }
  106. else {
  107. this.loadInter(type, true)
  108. }
  109. }
  110. }
  111. }
  112. //广告回调部分------------------------------------------------------------------------
  113. onInterstitialAdLoaded(placementId) {
  114. this.interAdState = InterAdState.LoadSuccess;
  115. if (this.ifLoadShow) {
  116. let type = this.checkPlacementIDType(placementId);
  117. this.showInter(type);
  118. }
  119. }
  120. onInterstitialAdLoadFail(placementId, errorInfo) {
  121. this.interAdState = InterAdState.LoadFail;
  122. let id = 11
  123. let type = this.checkPlacementIDType(placementId);
  124. if (type == 0) {
  125. id = 11
  126. }
  127. else if (type == 1) {
  128. // mk.tip.pop('拉取全屏视频失败,请稍后再试');
  129. id = 10
  130. }
  131. gData.adData.updateADLog(id, placementId);
  132. }
  133. //广告展示成功
  134. onInterstitialAdShow(placementId, callbackInfo) {
  135. this.interAdState = InterAdState.Show;
  136. // // console.log('99999999 onInterstitialAdShow')
  137. let data = JSON.parse(callbackInfo)
  138. let id = 2
  139. let type = this.checkPlacementIDType(placementId);
  140. if (type == 0) {
  141. id = 2
  142. this.singleClick = true;
  143. }
  144. else if (type == 1) {
  145. id = 9
  146. this.singleClickFull = true;
  147. }
  148. gData.adData.updateADLog(id, placementId, data);
  149. // if (GameM.commonData.ifUROI) {
  150. // let eventID = EncryptUtil.randomString(32);
  151. // let adAdn = this.getAdAdn(data.network_firm_id)
  152. // let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
  153. // AdM.sendUROIEvent(eventID, data.id, placementId, "ad_show", "interstitial", adAdn, price)
  154. // }
  155. }
  156. onInterstitialAdClick(placementId, callbackInfo) {
  157. // let data = JSON.parse(callbackInfo)
  158. // if (GameM.commonData.ifUROI) {
  159. // let eventID = EncryptUtil.randomString(32);
  160. // let adAdn = this.getAdAdn(data.network_firm_id)
  161. // let price = GameM.commonData.getAdPrice(data.adsource_id, data.adsource_price)
  162. // AdM.sendUROIEvent(eventID, data.id, placementId, "ad_click", "interstitial", adAdn, price)
  163. // }
  164. let id = 52;
  165. let singleId = 72;
  166. let type = this.checkPlacementIDType(placementId);
  167. let data = JSON.parse(callbackInfo)
  168. if (type == 0) {
  169. id = 52;
  170. singleId = 72;
  171. mk.data.sendDataEvent(DataEventId.ad_click, '插屏点击');
  172. if (this.singleClick) {
  173. this.singleClick = false;
  174. gData.adData.updateADLog(singleId, placementId, data);
  175. }
  176. }
  177. else if (type == 1) {
  178. id = 56;
  179. singleId = 76;
  180. mk.data.sendDataEvent(DataEventId.ad_click, '全屏插屏点击');
  181. if (this.singleClickFull) {
  182. this.singleClickFull = false;
  183. gData.adData.updateADLog(singleId, placementId, data);
  184. }
  185. }
  186. gData.adData.updateADLog(id, placementId, data);
  187. }
  188. //插屏广告关闭
  189. onInterstitialAdClose(placementId, callbackInfo) {
  190. this.interAdState = InterAdState.Close;
  191. // //给奖励
  192. let type = this.checkPlacementIDType(placementId);
  193. if (type == 1) {
  194. let data = JSON.parse(callbackInfo);
  195. gData.adData.updateADLog(8, placementId, data);
  196. this.loadInter(1);
  197. }
  198. else {
  199. this.loadInter(0);
  200. }
  201. }
  202. onInterstitialAdFailedToPlayVideo(placementId, errorInfo) {
  203. mk.console.logSingle('onInterstitialAdFailedToPlayVideo placementId >> ', placementId);
  204. mk.console.logSingle('onInterstitialAdFailedToPlayVideo ', errorInfo);
  205. }
  206. }
  207. /** 插屏广告加载状态 */
  208. export enum InterAdState {
  209. /** 未加载 */
  210. Unload = 0,
  211. /** 加载中 */
  212. Loading = 1,
  213. /** 加载成功 */
  214. LoadSuccess = 2,
  215. /** 加载失败 */
  216. LoadFail = 3,
  217. /** 显示状态 */
  218. Show = 4,
  219. /** 关闭状态 */
  220. Close = 5
  221. }