| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- import { DataEventId } from "../../../../../game/data/GameData";
- import { zgSingleton } from "../../zgSingleton";
- import ATSDKMgr from "./ATSDKMgr";
- /**
- * 原生广告
- * @description
- * @author 冯聪
- */
- export class ATNative extends zgSingleton {
- private ATNativeJS: any;
- /** 信息流广告类型 0 弹窗里 1 最底部 2 新车奖励 3 底部小窗口 */
- private nativeAdType: number = 4;
- /** 信息流广告状态 */
- private nativeAdState: NativeAdState = 0;
- /** 是否加载显示 */
- private ifLoadShow: boolean = false;
- private singleClick = false;
- /** 初始化 */
- init() {
- this.ATNativeJS = cc["ATNativeJS"];
- this.ATNativeJS.proxy = this;;
- this.ATNativeJS.init();
- if (this.nativeAdState == NativeAdState.Unload) {
- this.loadNative(4);
- }
- }
- /** 广告位id */
- placementID() {
- if (cc.sys.os === cc.sys.OS_IOS) {
- return ATSDKMgr.topon_native_placementId;
- } else if (cc.sys.os === cc.sys.OS_ANDROID) {
- return ATSDKMgr.topon_native_placementId;
- }
- }
- //广告逻辑部分------------------------------------------------------------------------
- /** 检测是否插屏预备好*/
- hasAdReady() {
- return this.ATNativeJS.hasAdReady();
- }
- /**
- * 加载信息流广告
- * @param type 类型对应不同位置
- * @param ifLoadshow 是否加载完显示显示
- */
- loadNative(type = 4, ifLoadshow = false) {
- this.nativeAdType = type;
- this.ifLoadShow = ifLoadshow;
- this.nativeAdState = NativeAdState.Loading;
- //加载原生广告时需要传入广告展示的宽高
- var frameSize = cc.view.getFrameSize();
- let frameWidth = frameSize.width;
- var per = frameWidth / 750
- var sw = per * 656;
- var sh = per * 440;
- if (this.nativeAdType == 2) {
- sw *= 0.9
- sh *= 0.8
- }
- else if (this.nativeAdType == 3) {
- sw *= 0.9
- sh *= 0.75
- }
- this.ATNativeJS.loadNative(sw, sh);
- }
- /**
- * 显示原生广告
- * @param type 0 弹窗里 1 最底部 2 新车奖励 3 底部小窗口 4 最底部
- */
- showNative(type: number) {
- if (this.nativeAdState == NativeAdState.Show) {
- //如果已经显示则不调用显示
- }
- else {
- if (this.hasAdReady()) {
- if (this.nativeAdType != type) {
- this.destroyNative();
- this.loadNative(type, true);
- }
- else {
- console.log("===[ShowNative", type);
- //加载原生广告时需要传入广告展示的宽高
- let frameSize = cc.view.getFrameSize();
- let frameWidth = frameSize.width;
- let frameHeight = frameSize.height;
- //获取基本的宽高
- let per = frameWidth / 750
- let width = per * 656
- let height = per * 440
- let fontSize = 12
- //设置位置宽高
- let pos_y = 0
- if (type == 0) {
- // posy = this.frameHeight * 0.5 - 100
- pos_y = frameHeight * 0.45
- }
- else if (type == 1) {
- // posy = this.frameHeight * 0.5 + 280
- pos_y = frameHeight * 0.625
- }
- else if (type == 2) {
- // posy = this.frameHeight * 0.5 + 210
- pos_y = frameHeight * 0.595
- width *= 0.9
- height *= 0.8
- fontSize = 8
- }
- else if (type == 3) {
- // posy = this.frameHeight * 0.5 + 420
- pos_y = frameHeight * 0.69
- width *= 0.9
- height *= 0.75
- fontSize = 8
- }
- else if (type == 4) {
- pos_y = frameHeight * 0.75;
- }
- let pos_x = (frameWidth - width) * 0.5
- this.ATNativeJS.showNative(pos_x, pos_y, width, height);
- }
- }
- else {
- //如果正在加载中,则不处理
- if (this.nativeAdState == NativeAdState.Loading) {
- //正在加载不重复加载
- this.ifLoadShow = true;
- }
- else {
- this.loadNative(type, true)
- }
- }
- }
- }
- /** 销毁baaner */
- destroyNative() {
- this.nativeAdState = NativeAdState.Close;
- this.ATNativeJS.destroyNative();
- this.loadNative(this.nativeAdType);
- }
- //广告回调部分------------------------------------------------------------------------
- /**
- * 原生广告加载成功
- * @param {*} placementId
- */
- onNativeAdLoaded(placementId) {
- this.nativeAdState = NativeAdState.LoadSuccess;
- if (this.ifLoadShow) {
- this.showNative(this.nativeAdType);
- }
- }
- /**
- * 原生广告加载失败
- * @param {*} placementId 广告位id
- * @param {*} errorInfo 错误信息
- */
- onNativeAdLoadFail(placementId, errorInfo) {
- this.nativeAdState = NativeAdState.LoadFail;
- // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdLoadFail(" + placementId + ", " + errorInfo + ")");
- gData.adData.updateADLog(12, placementId);
- }
- /**
- * 广告显示成功
- * @param placementId
- * @param callbackInfo
- */
- onNativeAdShow(placementId, callbackInfo) {
- this.nativeAdState = NativeAdState.Show;
- let data = JSON.parse(callbackInfo)
- gData.adData.updateADLog(3, placementId, data);
- this.singleClick = true;
- }
- /**
- * 原生广告被点击
- * @param placementId 广告位Id
- * @param callbackInfo 回调信息
- */
- onNativeAdClick(placementId, callbackInfo) {
- // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdClick(" + placementId + ", " + callbackInfo + ")");
- let data = JSON.parse(callbackInfo)
- gData.adData.updateADLog(53, placementId, data);
- mk.data.sendDataEvent(DataEventId.ad_click, '信息流点击');
- if (this.singleClick) {
- this.singleClick = false;
- gData.adData.updateADLog(73, placementId, data);
- }
- }
- }
- /** 原生信息流广告状态 */
- export enum NativeAdState {
- /** 未加载 */
- Unload = 0,
- /** 加载中 */
- Loading = 1,
- /** 加载成功 */
- LoadSuccess = 2,
- /** 加载失败 */
- LoadFail = 3,
- /** 显示状态 */
- Show = 4,
- /** 关闭状态 */
- Close = 5
- }
|