ATNative.ts 6.9 KB

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