ATNative.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { DataEventId } from "../../../../../game/data/GameData";
  2. import { zgSingleton } from "../../zgSingleton";
  3. import ATSDKMgr from "./ATSDKMgr";
  4. /**
  5. * 原生广告
  6. * @description
  7. * @author 冯聪
  8. */
  9. export class ATNative extends zgSingleton {
  10. private ATNativeJS: any;
  11. /** 信息流广告类型 0 弹窗里 1 最底部 2 新车奖励 3 底部小窗口 */
  12. private nativeAdType: number = 4;
  13. /** 信息流广告状态 */
  14. private nativeAdState: NativeAdState = 0;
  15. /** 是否加载显示 */
  16. private ifLoadShow: boolean = false;
  17. private singleClick = false;
  18. /** 初始化 */
  19. init() {
  20. this.ATNativeJS = cc["ATNativeJS"];
  21. this.ATNativeJS.proxy = this;;
  22. this.ATNativeJS.init();
  23. if (this.nativeAdState == NativeAdState.Unload) {
  24. this.loadNative(4);
  25. }
  26. }
  27. /** 广告位id */
  28. placementID() {
  29. if (cc.sys.os === cc.sys.OS_IOS) {
  30. return ATSDKMgr.topon_native_placementId;
  31. } else if (cc.sys.os === cc.sys.OS_ANDROID) {
  32. return ATSDKMgr.topon_native_placementId;
  33. }
  34. }
  35. //广告逻辑部分------------------------------------------------------------------------
  36. /** 检测是否插屏预备好*/
  37. hasAdReady() {
  38. return this.ATNativeJS.hasAdReady();
  39. }
  40. /**
  41. * 加载信息流广告
  42. * @param type 类型对应不同位置
  43. * @param ifLoadshow 是否加载完显示显示
  44. */
  45. loadNative(type = 4, ifLoadshow = false) {
  46. this.nativeAdType = type;
  47. this.ifLoadShow = ifLoadshow;
  48. this.nativeAdState = NativeAdState.Loading;
  49. //加载原生广告时需要传入广告展示的宽高
  50. var frameSize = cc.view.getFrameSize();
  51. let frameWidth = frameSize.width;
  52. var per = frameWidth / 750
  53. var sw = per * 656;
  54. var sh = per * 440;
  55. if (this.nativeAdType == 2) {
  56. sw *= 0.9
  57. sh *= 0.8
  58. }
  59. else if (this.nativeAdType == 3) {
  60. sw *= 0.9
  61. sh *= 0.75
  62. }
  63. this.ATNativeJS.loadNative(sw, sh);
  64. }
  65. /**
  66. * 显示原生广告
  67. * @param type 0 弹窗里 1 最底部 2 新车奖励 3 底部小窗口 4 最底部
  68. */
  69. showNative(type: number) {
  70. if (this.nativeAdState == NativeAdState.Show) {
  71. //如果已经显示则不调用显示
  72. }
  73. else {
  74. if (this.hasAdReady()) {
  75. if (this.nativeAdType != type) {
  76. this.destroyNative();
  77. this.loadNative(type, true);
  78. }
  79. else {
  80. console.log("===[ShowNative", type);
  81. //加载原生广告时需要传入广告展示的宽高
  82. let frameSize = cc.view.getFrameSize();
  83. let frameWidth = frameSize.width;
  84. let frameHeight = frameSize.height;
  85. //获取基本的宽高
  86. let per = frameWidth / 750
  87. let width = per * 656
  88. let height = per * 440
  89. let fontSize = 12
  90. //设置位置宽高
  91. let pos_y = 0
  92. if (type == 0) {
  93. // posy = this.frameHeight * 0.5 - 100
  94. pos_y = frameHeight * 0.45
  95. }
  96. else if (type == 1) {
  97. // posy = this.frameHeight * 0.5 + 280
  98. pos_y = frameHeight * 0.625
  99. }
  100. else if (type == 2) {
  101. // posy = this.frameHeight * 0.5 + 210
  102. pos_y = frameHeight * 0.595
  103. width *= 0.9
  104. height *= 0.8
  105. fontSize = 8
  106. }
  107. else if (type == 3) {
  108. // posy = this.frameHeight * 0.5 + 420
  109. pos_y = frameHeight * 0.69
  110. width *= 0.9
  111. height *= 0.75
  112. fontSize = 8
  113. }
  114. else if (type == 4) {
  115. pos_y = frameHeight * 0.75;
  116. }
  117. let pos_x = (frameWidth - width) * 0.5
  118. this.ATNativeJS.showNative(pos_x, pos_y, width, height);
  119. }
  120. }
  121. else {
  122. //如果正在加载中,则不处理
  123. if (this.nativeAdState == NativeAdState.Loading) {
  124. //正在加载不重复加载
  125. this.ifLoadShow = true;
  126. }
  127. else {
  128. this.loadNative(type, true)
  129. }
  130. }
  131. }
  132. }
  133. /** 销毁baaner */
  134. destroyNative() {
  135. this.nativeAdState = NativeAdState.Close;
  136. this.ATNativeJS.destroyNative();
  137. this.loadNative(this.nativeAdType);
  138. }
  139. //广告回调部分------------------------------------------------------------------------
  140. /**
  141. * 原生广告加载成功
  142. * @param {*} placementId
  143. */
  144. onNativeAdLoaded(placementId) {
  145. this.nativeAdState = NativeAdState.LoadSuccess;
  146. if (this.ifLoadShow) {
  147. this.showNative(this.nativeAdType);
  148. }
  149. }
  150. /**
  151. * 原生广告加载失败
  152. * @param {*} placementId 广告位id
  153. * @param {*} errorInfo 错误信息
  154. */
  155. onNativeAdLoadFail(placementId, errorInfo) {
  156. this.nativeAdState = NativeAdState.LoadFail;
  157. // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdLoadFail(" + placementId + ", " + errorInfo + ")");
  158. gData.adData.updateADLog(12, placementId);
  159. }
  160. /**
  161. * 广告显示成功
  162. * @param placementId
  163. * @param callbackInfo
  164. */
  165. onNativeAdShow(placementId, callbackInfo) {
  166. this.nativeAdState = NativeAdState.Show;
  167. let data = JSON.parse(callbackInfo)
  168. gData.adData.updateADLog(3, placementId, data);
  169. this.singleClick = true;
  170. }
  171. /**
  172. * 原生广告被点击
  173. * @param placementId 广告位Id
  174. * @param callbackInfo 回调信息
  175. */
  176. onNativeAdClick(placementId, callbackInfo) {
  177. // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdClick(" + placementId + ", " + callbackInfo + ")");
  178. let data = JSON.parse(callbackInfo)
  179. gData.adData.updateADLog(53, placementId, data);
  180. mk.data.sendDataEvent(DataEventId.ad_click, '信息流点击');
  181. if (this.singleClick) {
  182. this.singleClick = false;
  183. gData.adData.updateADLog(73, placementId, data);
  184. }
  185. }
  186. }
  187. /** 原生信息流广告状态 */
  188. export enum NativeAdState {
  189. /** 未加载 */
  190. Unload = 0,
  191. /** 加载中 */
  192. Loading = 1,
  193. /** 加载成功 */
  194. LoadSuccess = 2,
  195. /** 加载失败 */
  196. LoadFail = 3,
  197. /** 显示状态 */
  198. Show = 4,
  199. /** 关闭状态 */
  200. Close = 5
  201. }