123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191 |
- import AdAgent from "./AdAgent";
- import { AttributedKey, AttributedType, AttributedValue, BannerLocation, SubLocation } from "./YZ_Constant";
- import PlatUtils from "./PlatUtils";
- import { utils } from "./Utils";
- import RewardInsert from "./RewardInsert";
- import CompatibleTool from "./CompatibleTool";
- import BeforGameOverRecGamesPanel from "./BeforGameOverRecGamesPanel";
- import { CustomAdInfo } from "./CommonConfig";
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class AdAgentWechat extends AdAgent {
- _curBannerAd: any = null;
- _videoAd: any = null;
- _videoCallback: Function = null;
- _insertAd: any = null;
- _rewardInsertNode: cc.Node = null;
- public get ServerConfig() {
- return utils.wechatTool.ServerConfig;
- }
- _sysInfo: any = null;
- public get sysInfo() {
- if (this._sysInfo) return this._sysInfo;
- this._sysInfo = utils.wechatTool.getSystemInfo();
- return this._sysInfo;
- }
- public Init() {
- if (PlatUtils.IsWechat) {
- utils.registerServerInitEvent(() => {
- this._initVideoAd();
- this._initInsertAd();
- if (utils.getConfigByKey("game_time_report")) {
- utils.scheduleOnce(() => {
- utils.wechatTool.reportAttributedEvent(AttributedType.GameAddiction, AttributedKey.GameAddiction, AttributedValue.GameTimeAction);
- }, utils.getConfigByKey("game_time_report"));
- }
- }, this);
- }
- }
- _initVideoAd() {
- if (PlatUtils.IsWechat) {
- if (!utils.wechatTool.isOverMinVersion("2.0.4")) {
- utils.showLog("当前版本不支持视频广告!");
- return;
- }
- if (!this._videoAd) {
- utils.showLog("初始化视频!");
- if (!utils.config.wechatconfig.videoId) {
- utils.showLog("视频ID配置错误!");
- return;
- }
- utils.showLog("视频广告ID:", utils.config.wechatconfig.videoId.trim());
- //@ts-ignore
- this._videoAd = wx.createRewardedVideoAd({
- adUnitId: utils.config.wechatconfig.videoId
- });
- let self = this;
- if (this._videoAd) {
- utils.showLog("初始化注册视频回调!");
- this._videoAd.onLoad(function () {
- utils.showLog("激励视频加载成功");
- // this._isVideoLoaded = true;
- }.bind(this));
- this._videoAd.onError(function (err) {
- utils.showLog("激励视频加载失败!", err.code, err.msg);
- // this._isVideoLoaded = false;
- }.bind(this));
- this._videoAd.onClose(function (res) {
- // this._isVideoLoaded = false;
- if (res && res.isEnded || res === undefined) {
- // 正常播放结束,可以下发游戏奖励
- utils.showLog('激励视频广告完成,发放奖励');
- if (self._videoCallback) {
- self._videoCallback(true, "");
- self._videoCallback = null;
- }
- } else {
- // 播放中途退出,不下发游戏奖励
- utils.showLog('激励视频广告取消关闭,不发放奖励');
- if (self._videoCallback) {
- self._videoCallback(false, "观看完视频才能获得奖励!");
- self._videoCallback = null;
- }
- }
- }.bind(this));
- } else {
- utils.showLog("激励视频初始化失败!");
- }
- }
- }
- }
- _bannerSizePercent: number = 0.1;
- _bannerBottom: number = 0;
- _oldBannerLocation: BannerLocation = BannerLocation.None;
- _createBanner(location: BannerLocation, args: any = null) {
- if (PlatUtils.IsWechat) {
- let locationTmp: BannerLocation = location;
- let argsTmp: any = args;
- if (argsTmp && argsTmp.width) {
- this._bannerSizePercent = argsTmp.width < 0 ? 0 : argsTmp.width;
- this._bannerSizePercent = argsTmp.width > 1 ? 1 : argsTmp.width;
- }
- if (argsTmp && argsTmp.bottom) {
- this._bannerBottom = argsTmp.bottom < 0 ? 0 : argsTmp.bottom;
- this._bannerBottom = argsTmp.bottom > cc.winSize.height ? cc.winSize.height : argsTmp.bottom;
- }
- let params = {
- adUnitId: utils.config.wechatconfig.getBannerId(locationTmp),
- style: {
- left: 0,
- width: 300,
- top: 0
- }
- };
- if (this._oldBannerLocation != locationTmp && this._curBannerAd) {
- this._curBannerAd.destroy();
- this._oldBannerLocation = locationTmp;
- }
- //@ts-ignore
- let curBannerAd = wx.createBannerAd(params);
- if (curBannerAd) {
- curBannerAd.onError((err) => {
- utils.showLog("广告条加载失败! ", JSON.stringify(err));
- if (curBannerAd) {
- curBannerAd.destroy();
- }
- if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "default") {
- this.showNativeBannerAd(location, args);
- }
- });
- let self = this;
- curBannerAd.onLoad(() => {
- curBannerAd.show().then(() => {
- let old = self._curBannerAd;
- if (old) {
- old.destroy();
- }
- self._curBannerAd = curBannerAd;
- utils.showLog("默认Banner广告显示成功!");
- this.HideNativeBanner();
- }).catch((err) => {
- utils.showLog("Banner广告出错", JSON.stringify(err));
- if (curBannerAd) {
- curBannerAd.destroy();
- }
- if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "default") {
- this.showNativeBannerAd(location, args);
- }
- });
- });
- curBannerAd.onResize((res) => {
- curBannerAd.style.width = utils.wechatTool.getSystemInfo().screenWidth * self._bannerSizePercent;
- curBannerAd.style.left = (utils.wechatTool.getSystemInfo().screenWidth - res.width) * 0.5;
- if (self._bannerBottom == cc.winSize.height) {
- curBannerAd.style.top = 0;
- } else {
- curBannerAd.style.top = utils.wechatTool.getSystemInfo().screenHeight - res.height - self._bannerBottom;
- }
- });
- } else {
- utils.showLog("广告条创建失败!");
- }
- }
- }
- createCustomADBanner(location: BannerLocation = BannerLocation.Game, args: any = null) {
- if (PlatUtils.IsWechat) {
- let locationTmp: BannerLocation = location;
- if (!utils.config.wechatconfig.getBannerId(locationTmp)) {
- locationTmp = BannerLocation.Home;
- }
- let argsTmp: any = args;
- if (argsTmp && argsTmp.width) {
- this._bannerSizePercent = argsTmp.width < 0 ? 0 : argsTmp.width;
- this._bannerSizePercent = argsTmp.width > 1 ? 1 : argsTmp.width;
- }
- if (argsTmp && argsTmp.bottom) {
- this._bannerBottom = argsTmp.bottom < 0 ? 0 : argsTmp.bottom;
- this._bannerBottom = argsTmp.bottom > cc.winSize.height ? cc.winSize.height : argsTmp.bottom;
- }
- let params = {
- adUnitId: utils.config.wechatconfig.getBannerId(locationTmp),
- style: {
- left: 0,
- width: 300,
- top: 0
- }
- };
- if (this._oldBannerLocation != locationTmp && this._curBannerAd) {
- this._curBannerAd.destroy();
- this._oldBannerLocation = locationTmp;
- }
- //@ts-ignore
- let curBannerAd = wx.createBannerAd(params);
- if (curBannerAd) {
- curBannerAd.onError((err) => {
- utils.showLog("广告条加载失败! ", JSON.stringify(err));
- if (curBannerAd) {
- curBannerAd.destroy();
- }
- });
- let self = this;
- curBannerAd.onLoad(() => {
- cc.director.on("CloseCustomADPanel", (() => {
- curBannerAd.show().then(() => {
- let old = self._curBannerAd;
- if (old) {
- old.destroy();
- }
- self._curBannerAd = curBannerAd;
- utils.showLog("Banner广告显示成功!");
- let interval: number = 18;
- if (utils.wechatTool.ServerConfig.refresh_ad_time) {
- interval = utils.wechatTool.ServerConfig.refresh_ad_time;
- }
- clearInterval(self._showBannerTimerId);
- //@ts-ignore
- self._showBannerTimerId = setInterval(function () {
- utils.showLog(`显示Banner广告!location:${locationTmp}; args:${JSON.stringify(argsTmp)}; 间隔时间:${utils.wechatTool.ServerConfig.refresh_ad_time}`);
- self._showBannerTimer(locationTmp, argsTmp);
- }.bind(self), interval * 1000);
- }).catch((err) => {
- utils.showLog("Banner广告出错", JSON.stringify(err));
- if (curBannerAd) {
- curBannerAd.destroy();
- }
- });
- cc.director.targetOff(self);
- }), self);
- });
- curBannerAd.onResize((res) => {
- curBannerAd.style.width = utils.wechatTool.getSystemInfo().screenWidth * self._bannerSizePercent;
- curBannerAd.style.left = (utils.wechatTool.getSystemInfo().screenWidth - res.width) * 0.5;
- if (self._bannerBottom == cc.winSize.height) {
- curBannerAd.style.top = 0;
- } else {
- curBannerAd.style.top = utils.wechatTool.getSystemInfo().screenHeight - res.height - self._bannerBottom;
- }
- });
- } else {
- utils.showLog("广告条创建失败!");
- }
- }
- }
- _initInsertAd() {
- if (PlatUtils.IsWechat) {
- if (!utils.wechatTool.isOverMinVersion("2.6.0")) {
- utils.showLog("当前版本不支持插屏广告!");
- return;
- }
- if (!this._insertAd) {
- utils.showLog("初始化插屏广告!");
- if (!utils.config.wechatconfig.insertId) {
- utils.showLog("插屏广告ID配置错误!");
- return;
- }
- utils.showLog("插屏广告ID:", utils.config.wechatconfig.insertId.trim());
- //@ts-ignore
- this._insertAd = wx.createInterstitialAd({
- adUnitId: utils.config.wechatconfig.insertId.trim()
- });
- if (this._insertAd) {
- this._insertAd.onLoad(function () {
- utils.showLog("插屏广告拉取成功!");
- });
- this._insertAd.onError((err) => {
- utils.showLog("插屏广告拉取失败!", JSON.stringify(err));
- if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "default") {
- this.showNativeIntersititialAd();
- }
- });
- this._insertAd.onClose(function () {
- utils.showLog("插屏广告被关闭!");
- });
- } else {
- utils.showLog("插屏组件初始化失败!");
- }
- }
- }
- }
- _showBannerTimer(location: BannerLocation, args: any) {
- let locationTmp: BannerLocation = location;
- let argsTmp: any = args;
- utils.showLog(`显示Banner广告xxx!location:${locationTmp}; args:${JSON.stringify(argsTmp)}; 间隔时间:${utils.wechatTool.ServerConfig.refresh_ad_time};优先级:${this.ServerConfig.banner_first_ad}`);
- if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "native") {
- this.showNativeBannerAd(location, args);
- } else {
- if (locationTmp == BannerLocation.None) {
- utils.showLog("未定义的BannerLocation,", locationTmp);
- } else {
- if (utils.config.wechatconfig.getBannerId(locationTmp)) {
- this._createBanner(locationTmp, argsTmp);
- } else {
- utils.showLog(`未找到位置为 ${locationTmp} 的广告ID!`);
- this._createBanner(BannerLocation.Home, argsTmp);
- }
- }
- }
- }
- _showBannerTimerId: number = 0;
- public ShowBanner(location: BannerLocation = BannerLocation.Home, args: any = null) {
- if (PlatUtils.IsWechat) {
- if (!utils.wechatTool.isOverMinVersion("2.0.4")) {
- utils.showLog("当前版本不支持Banner广告!");
- return;
- }
- if (utils.wechatTool.ServerConfig) {
- let locationTmp: BannerLocation = location;
- let argsTmp: any = args;
- this._showBannerTimer(locationTmp, argsTmp);
- let interval: number = 18;
- if (utils.wechatTool.ServerConfig.refresh_ad_time) {
- interval = utils.wechatTool.ServerConfig.refresh_ad_time;
- }
- clearInterval(this._showBannerTimerId);
- //@ts-ignore
- this._showBannerTimerId = setInterval(function () {
- utils.showLog(`显示Banner广告!location:${locationTmp}; args:${JSON.stringify(argsTmp)}; 间隔时间:${utils.wechatTool.ServerConfig.refresh_ad_time}`);
- this._showBannerTimer(locationTmp, argsTmp);
- }.bind(this), interval * 1000);
- } else {
- utils.showLog("服务器配置数据未初始化!");
- }
- }
- }
- public HideBanner(location: BannerLocation = BannerLocation.Home) {
- if (PlatUtils.IsWechat) {
- utils.showLog("隐藏广告条");
- clearInterval(this._showBannerTimerId);
- this.HideDefaultBanner();
- this.HideNativeBanner();
- }
- }
- private HideDefaultBanner(): void {
- this._curBannerAd && this._curBannerAd.hide();
- }
- private HideNativeBanner(): void {
- this.nativeBannerAd && this.nativeBannerAd.hide();
- }
- public ShowInterstitial(location: BannerLocation = BannerLocation.Home) {
- if (PlatUtils.IsWechat) {
- if (!utils.wechatTool.isOverMinVersion("2.6.0")) {
- utils.showLog("当前版本不支持插屏广告!");
- return;
- }
- let delayTime: number = 0;
- if (utils.wechatTool
- && utils.wechatTool.ServerConfig
- && utils.wechatTool.ServerConfig.intersititia_delay_show_time) {
- delayTime = utils.wechatTool.ServerConfig.intersititia_delay_show_time;
- }
- utils.showLog(`插屏广告延时展示! delayTime:${delayTime}秒`);
- setTimeout(() => {
- if (this.ServerConfig && this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "box") {
- utils.showLog("优先展示盒子插屏广告!");
- if (this.canShowBoxInsertAd()) {
- this._createBoxInsertAd();
- } else {
- utils.showLog("盒子插屏广告展示失败,展示默认插屏!");
- this._createMiniGameInsertAd();
- }
- } else if (this.ServerConfig && this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "native") {
- this.showNativeIntersititialAd();
- } else {
- utils.showLog("优先展示小游戏插屏广告!");
- this._createMiniGameInsertAd();
- }
- }, delayTime * 1000);
- }
- }
- _createBoxInsertAd() {
- utils.rewardCallFunc = null;
- utils.rewardCloseFunc = null;
- if (utils.config && utils.config.otherconfig.beforGameOverRecGamesPanel && utils.getRecommondGameList()) {
- let panel = cc.instantiate(utils.config.otherconfig.beforGameOverRecGamesPanel);
- panel.zIndex = 999999;
- let morePanel: BeforGameOverRecGamesPanel = panel.getComponent("BeforGameOverRecGamesPanel");
- cc.director.getScene().addChild(panel);
- morePanel._location = SubLocation.isBoxInsertAd;
- morePanel.init(utils.getRecommondGameList());
- morePanel.show();
- } else {
- utils.showLog("互推插屏展示失败!");
- }
- }
- /**
- * 是否能够显示互推插屏
- */
- canShowBoxInsertAd() {
- let jumpList: any = utils.getRecommondGameList();
- if (jumpList && jumpList.length > 0) {
- return true;
- }
- return false;
- }
- isFirstShowInsertAd: boolean = true; //首次展示广告
- _createMiniGameInsertAd() {
- if (this._insertAd) {
- this._insertAd.show().then(() => {
- utils.showLog("插屏广告展示成功!");
- if (this.isFirstShowInsertAd && utils.getConfigByKey("insert_ad_first_show_active") == "true") {
- this.isFirstShowInsertAd = false;
- utils.wechatTool.reportAttributedEvent(AttributedType.GameAddiction, AttributedKey.GameAddiction, AttributedValue.InsertAdFirstShowAction);
- }
- }).catch((err) => {
- utils.showLog("插屏广告展示失败!", JSON.stringify(err));
- if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "default") {
- this.showNativeIntersititialAd();
- }
- // if (this.ServerConfig && this.ServerConfig.intersititial_first_ad == "default") {
- // utils.showLog("优先展示默认插屏,显示备选盒子插屏!");
- // if (this.canShowBoxInsertAd()) {
- // this._createBoxInsertAd();
- // }
- // }
- });
- } else {
- utils.showLog("插屏广告未初始化");
- if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "default") {
- this.showNativeIntersititialAd();
- }
- // if (this.ServerConfig && this.ServerConfig.intersititial_first_ad == "default") {
- // utils.showLog("优先展示默认插屏,显示备选盒子插屏!");
- // if (this.canShowBoxInsertAd()) {
- // this._createBoxInsertAd();
- // }
- // }
- }
- }
- public ShowVideo(callback: Function) {
- if (PlatUtils.IsWechat) {
- this._videoCallback = callback;
- if (this.checkRewardInsertIsShow() && utils.wechatTool
- && utils.wechatTool.ServerConfig
- && utils.wechatTool.ServerConfig.reward_first_ad && utils.wechatTool.ServerConfig.reward_first_ad != "video") {
- utils.showLog("<<<服务器默认优先展示激励插屏>>>");
- this.showRewardInsert();
- return;
- }
- if (!utils.wechatTool.isOverMinVersion("2.0.4")) {
- utils.showLog("当前版本不支持视频广告!");
- if (this._videoCallback) {
- // this._videoCallback(false, "暂无视频广告!");
- this.showRewardInsert();
- }
- return;
- }
- if (this._videoAd) {
- this._videoAd.show().then(function () {
- utils.showLog("视频显示成功!");
- }.bind(this)).catch(function (err) {
- utils.showLog("视频未加载!");
- this._videoAd.load();
- if (this._videoCallback) {
- // this._videoCallback(false, "暂无视频广告!");
- this.showRewardInsert();
- }
- }.bind(this));
- } else {
- utils.showLog("视频未初始化!");
- if (this._videoCallback) {
- // this._videoCallback(false, "暂无视频广告!");
- this.showRewardInsert();
- }
- }
- }
- }
- /**
- * 验证是否显示激励插屏
- */
- private checkRewardInsertIsShow() {
- let jumpList = utils.getRecommondGameList();
- if (utils.isSupportnavigateToMiniGame()) {
- if (utils.wechatTool
- && utils.wechatTool.ServerConfig
- && utils.wechatTool.ServerConfig.is_reward_intersititia) {
- if (utils.wechatTool.ServerConfig.is_reward_intersititia == "true"
- && jumpList && jumpList.length > 0) {
- utils.showLog("激励插屏显示环境验证通过!");
- return true;
- } else {
- cc.warn("is_reward_intersititia 参数为false,激励插屏组件不显示!");
- return false;
- }
- } else {
- cc.warn("配置中没有is_reward_intersititia参数,激励插屏组件组件不显示!");
- return false;
- }
- }
- utils.showLog("当前平台不支持小程序跳转!");
- return false;
- }
- /**
- * 显示激励插屏组件
- */
- public showRewardInsert() {
- utils.showLog("show reward");
- if (!this.checkRewardInsertIsShow()) {
- utils.adManager.videoCallBack && utils.adManager.videoCallBack(false, "暂无视频广告!");
- utils.adManager.videoCallBack = null;
- return;
- }
- if (((!cc.isValid(this._rewardInsertNode)) || !this._rewardInsertNode) && utils.config.otherconfig.rewardInsert) {
- utils.showLog("创建激励插屏广告");
- this._rewardInsertNode = cc.instantiate(utils.config.otherconfig.rewardInsert);
- this._rewardInsertNode.position = CompatibleTool.position(cc.winSize.width / 2, cc.winSize.height / 2);
- cc.director.getScene().addChild(this._rewardInsertNode, 9999);
- }
- if (this._rewardInsertNode) {
- let rewardInsert: RewardInsert = this._rewardInsertNode.getComponent("RewardInsert");
- if (rewardInsert) {
- rewardInsert.isShow = false;
- utils.showLog("显示激励插屏组件!");
- } else {
- utils.showLog("RewardInsert组件不存在!");
- }
- } else {
- utils.showLog("激励插屏没有创建!");
- }
- }
- public hideRewardInsert() {
- if (this._rewardInsertNode) {
- let rewardInsert: RewardInsert = this._rewardInsertNode.getComponent("RewardInsert");
- if (rewardInsert) {
- rewardInsert.hide();
- utils.showLog("隐藏激励插屏组件!");
- } else {
- utils.showLog("RewardInsert组件不存在!");
- }
- } else {
- utils.showLog("激励插屏没有创建!");
- }
- }
- public ShowCloseBtnBanner(location: BannerLocation = BannerLocation.Home, args: any) {
- if (PlatUtils.IsWechat) {
- if (utils.wechatTool && !utils.wechatTool.isOverMinVersion("2.0.4")) {
- utils.showLog("当前版本不支持Banner广告!");
- return;
- }
- if (utils.wechatTool.ServerConfig && (!utils.wechatTool.ServerConfig.isMoveBtn || utils.wechatTool.ServerConfig.isMoveBtn != "true")) {
- utils.showLog("服务器没有开启移动按钮,不显示广告!");
- return;
- }
- if (utils.wechatTool && utils.wechatTool.ServerConfig) {
- if (PlatUtils.IsWechat) {
- let locationTmp: BannerLocation = location;
- // let argsTmp: any = args;
- let params = {
- adUnitId: utils.config.wechatconfig.getBannerId(locationTmp),
- style: {
- left: 0,
- width: 300,
- top: 0
- }
- };
- if (this._oldBannerLocation != locationTmp && this._curBannerAd) {
- this._curBannerAd.destroy();
- this._oldBannerLocation = locationTmp;
- }
- //@ts-ignore
- let curBannerAd = wx.createBannerAd(params);
- if (curBannerAd) {
- curBannerAd.onError((err) => {
- utils.showLog("广告条加载失败! ", JSON.stringify(err));
- if (curBannerAd) {
- curBannerAd.destroy();
- }
- });
- let self = this;
- curBannerAd.onLoad(() => {
- curBannerAd.show().then(() => {
- let old = self._curBannerAd;
- if (old) {
- old.destroy();
- }
- self._curBannerAd = curBannerAd;
- let closeBtn = args.closeBtn;
- if (!closeBtn) {
- return;
- }
- // closeBtn.active = true;
- //调整关闭按钮位置
- let winHeight = cc.winSize.height;
- var adY = self.getBannerAdHeight();
- utils.showLog('utils - adY:', adY);
- if (adY > 0) {
- closeBtn.y = -(winHeight / 2 - adY) + closeBtn.height;
- utils.showLog("btnClose.y", closeBtn.y);
- }
- utils.showLog("关闭按钮---Banner广告显示成功!");
- }).catch((err) => {
- utils.showLog("Banner广告出错", JSON.stringify(err));
- if (curBannerAd) {
- curBannerAd.destroy();
- }
- });
- });
- curBannerAd.onResize((res) => {
- curBannerAd.style.width = utils.wechatTool.getSystemInfo().screenWidth * 0.6;
- curBannerAd.style.left = (utils.wechatTool.getSystemInfo().screenWidth - res.width) * 0.5;
- // if (self._bannerBottom == cc.winSize.height) {
- // curBannerAd.style.top = 0;
- // } else {
- curBannerAd.style.top = utils.wechatTool.getSystemInfo().screenHeight - res.height;
- // }
- });
- } else {
- utils.showLog("广告条创建失败!");
- }
- }
- } else {
- utils.showLog("服务器配置数据未初始化!");
- }
- }
- }
- /**
- * 是否能显示6个元素的交叉推广组件
- */
- public canShowCrossWidget6() {
- if (PlatUtils.IsWechat) {
- if (utils.isSupportnavigateToMiniGame()) {
- return true;
- } else {
- cc.warn("当前平台不支持游戏内跳转,6元素交叉推广组件不显示!");
- return false;
- }
- }
- }
- /**
- * 显示6元素交叉推广组件
- */
- public showCrossWidget6(): cc.Node {
- if (this.canShowCrossWidget6()) {
- if (utils.config.otherconfig.crossWidget6) {
- return cc.instantiate(utils.config.otherconfig.crossWidget6);
- } else {
- utils.showLog("未找到预制体 CrossWidget6, 请查看CommonUtils组件上是否赋值!");
- }
- }
- return null;
- }
- /**
- * 显示结算广告
- * @param data 参数: closeBtn:
- * statement_type
- * 1:只显示小游戏插屏广告
- * 2:只显示6个互推广告
- * 3:显示插屏广告+6个互推
- */
- showStatementAds(data?: any): any {
- let result: any = { "type": 0, "node": null };
- let node: cc.Node = null;
- this.ShowInterstitial();
- if (utils.canShowCrossWidget6()) {
- utils.showLog("服务器配置显示6个互推组件");
- node = this.showCrossWidget6();
- result.type = 1;
- result.node = node;
- return result;
- }
- return result;
- // if (this.ServerConfig && this.ServerConfig.statement_type) {
- // // let type = this.ServerConfig.statement_type;
- // let node: cc.Node = null;
- // let resType: number = 0;
- // this.ShowInterstitial();
- // if (this.canShowCrossWidget6()) {
- // node = this.showCrossWidget6();
- // resType = 1;
- // }
- // // switch (type) {
- // // case 1:
- // // utils.showLog("结算广告 >> 只显示小游戏插屏广告");
- // // this.ShowInterstitial();
- // // break;
- // // case 2:
- // // utils.showLog("结算广告 >> 只显示6个互推广告");
- // // node = this.showCrossWidget6();
- // // resType = 1;
- // // break;
- // // case 3:
- // // utils.showLog("结算广告 >> 显示插屏广告+6个互推");
- // // this.ShowInterstitial();
- // // node = this.showCrossWidget6();
- // // resType = 1;
- // // break;
- // // default:
- // // utils.showLog("非法的结算广告类型,:", type)
- // // break;
- // // }
- // result.type = resType;
- // result.node = node;
- // return result;
- // } else {
- // cc.warn("配置未初始化!");
- // return result;
- // }
- }
- /**
- * 获取banner广告高度
- */
- public getBannerAdHeight() {
- if (this._curBannerAd) {
- let i = this._curBannerAd.style.realHeight * 2;
- if (i === null || i === undefined || isNaN(i)) {
- return 0;
- } else {
- return i;
- }
- }
- return 0;
- }
- _customAdObjs: any[] = [];
- public showNativeTryGameWidget(params: any = null) {
- if (!utils.wechatTool.isOverMinVersion("2.11.1")) {
- utils.showLog("当前版本不支持原生广告!");
- return;
- }
- if (!this.ServerConfig) {
- utils.showLog("组件未初始化!");
- return;
- }
- if (!utils.config.wechatconfig.bannerBoxId) {
- utils.showLog("原生广告ID不存在");
- return;
- }
- let top = 0;
- let left = 0;
- if (params.top) {
- top = params.top / cc.winSize.height * this.sysInfo.screenHeight
- } else {
- top = this.sysInfo.screenHeight - 112 - params.bottom / cc.winSize.height * this.sysInfo.screenHeight;
- }
- if (params.left) {
- left = params.left / cc.winSize.width * this.sysInfo.screenWidth;
- } else if (params.right) {
- left = this.sysInfo.screenWidth - 70 - params.right / cc.winSize.width * this.sysInfo.screenWidth;
- }
- //@ts-ignore
- let customAd = wx.createCustomAd({
- // adUnitId: utils.config.wechatconfig.bannerBoxId,
- adUnitId: utils.config.wechatconfig.bannerBoxId,
- adIntervals: 30,
- style: {
- left: left,
- top: top
- }
- });
- customAd.onError((erro) => {
- utils.showLog("原生广告异常!>>>>> #code=" + erro.errCode + " #msg=" + erro.errMsg);
- if (params.onError) {
- utils.showLog("执行微信原生广告异常回调!")
- params.onError();
- }
- })
- customAd.onLoad(() => {
- utils.showLog("微信原生广告加载成功!");
- customAd.show().then(() => {
- if (params.onSuccess) {
- utils.showLog("执行微信原生广告显示成功回调!")
- params.onSuccess();
- }
- utils.showLog("微信原生广告显示成功!");
- }).catch((err) => {
- utils.showLog("微信原生广告显示失败!");
- });
- })
- this._customAdObjs.push(customAd);
- }
- _nativeCustomAdObjs: any[] = [];
- public showCustomAd(params: any = null) {
- if (!utils.wechatTool.isOverMinVersion("2.11.1")) {
- utils.showLog("当前版本不支持原生模版广告!");
- return;
- }
- if (!this.ServerConfig) {
- utils.showLog("组件未初始化!");
- return;
- }
- if (utils.config.wechatconfig.customAdInfos.length < 1) {
- utils.showLog("原生广告配置不存在");
- return;
- }
- let customAdInfo: CustomAdInfo = utils.config.wechatconfig.getCustomAdInfoInfo(params.location);
- if (!customAdInfo) {
- utils.showLog("当前位置未配置模版广告!");
- return;
- }
- // utils.showLog("显示原生模版广告:位置:" + params.location + ",配置:" + customAdInfo.toStrong);
- let top = 0;
- let left = 0;
- // about:左右居中
- // updown:上下
- // all:上下和左右居中
- // false:自定义对齐方式
- if (customAdInfo.is_center == "false") {
- if (customAdInfo.top > -1) {
- top = customAdInfo.top / cc.winSize.height * this.sysInfo.screenHeight
- } else {
- top = this.sysInfo.screenHeight - customAdInfo.height - customAdInfo.bottom / cc.winSize.height * this.sysInfo.screenHeight;
- }
- if (customAdInfo.left > -1) {
- left = customAdInfo.left / cc.winSize.width * this.sysInfo.screenWidth;
- } else {
- left = this.sysInfo.screenWidth - customAdInfo.width - customAdInfo.right / cc.winSize.width * this.sysInfo.screenWidth;
- }
- } else if (customAdInfo.is_center == "updown") {
- utils.showLog("原生模版上下居中对齐!")
- if (customAdInfo.left > -1) {
- left = customAdInfo.left / cc.winSize.width * this.sysInfo.screenWidth;
- } else {
- left = this.sysInfo.screenWidth - customAdInfo.width - customAdInfo.right / cc.winSize.width * this.sysInfo.screenWidth;
- }
- top = (this.sysInfo.screenHeight - customAdInfo.height) / 2;
- } else if (customAdInfo.is_center == "about") {
- utils.showLog("原生模版左右居中对齐!")
- left = (this.sysInfo.screenWidth - customAdInfo.width) / 2;
- if (customAdInfo.top > -1) {
- top = customAdInfo.top / cc.winSize.height * this.sysInfo.screenHeight
- } else {
- top = this.sysInfo.screenHeight - customAdInfo.height - customAdInfo.bottom / cc.winSize.height * this.sysInfo.screenHeight;
- }
- }
- else {
- utils.showLog("原生模版上下左右居中对齐!")
- left = (this.sysInfo.screenWidth - customAdInfo.width) / 2;
- top = (this.sysInfo.screenHeight - customAdInfo.height) / 2;
- }
- //@ts-ignore
- let customAd = wx.createCustomAd({
- adUnitId: customAdInfo.id,
- adIntervals: customAdInfo.refresh_time,
- style: {
- left: left,
- top: top,
- width: customAdInfo.width
- }
- });
- customAd.location = params.location;
- utils.showLog("customAd style left=" + left + ",top=" + top + "; screenWidth=" + this.sysInfo.screenWidth + ", screenHeight=" + this.sysInfo.screenHeight)
- customAd.onError((erro) => {
- utils.showLog("原生广告异常!>>>>> #code=" + erro.errCode + " #msg=" + erro.errMsg);
- if (params.onError) {
- utils.showLog("执行微信原生广告异常回调!")
- params.onError();
- }
- })
- customAd.onLoad(() => {
- utils.showLog("微信原生广告加载成功!");
- customAd.show().then(() => {
- if (params.onSuccess) {
- utils.showLog("执行微信原生广告显示成功回调!")
- params.onSuccess();
- }
- utils.showLog("微信原生广告显示成功!");
- }).catch((err) => {
- utils.showLog("微信原生广告显示失败!");
- });
- })
- this._customAdObjs.push(customAd);
- }
- /**
- * 隐藏原生模版广告
- */
- hideCustomAd(params?: any) {
- for (let i = 0; i < this._customAdObjs.length; i++) {
- if (this._customAdObjs[i]) {
- if (params && "location" in params) {
- if (this._customAdObjs[i].location == params.location) {
- utils.showLog("隐藏位置:" + params.location + "的原生模版广告");
- this._customAdObjs[i].destroy()
- this._customAdObjs.splice(i, 1)
- }
- } else {
- this._customAdObjs[i].destroy()
- this._customAdObjs.splice(i, 1)
- }
- }
- }
- if (!params || !("location" in params)) utils.showLog("隐藏所有位置的原生模版广告");
- }
- /**
- * 隐藏原生试玩广告
- */
- hideNativeTryGameWidget() {
- for (let i = 0; i < this._customAdObjs.length; i++) {
- if (this._customAdObjs[i]) {
- this._customAdObjs[i].destroy()
- }
- }
- for (let i = 0; i < this._customAdObjs.length; i++) {
- this._customAdObjs.splice(i, 1)
- }
- }
- nativeBannerAd: any = null;
- public showNativeBannerAd(location: BannerLocation, args: any = null) {
- utils.showLog("展示原生banner广告!");
- if (!utils.wechatTool.isOverMinVersion("2.11.1")) {
- utils.showLog("当前版本不支持原生模版广告!");
- return;
- }
- if (!this.ServerConfig) {
- utils.showLog("组件未初始化!");
- return;
- }
- if (!utils.config.wechatconfig.nativeBannerId) {
- utils.showLog("原生Banner广告ID配置不存在");
- return;
- }
- let bannerWidth = 325;
- let bannerHeight = 100;
- let left = (this.sysInfo.screenWidth - bannerWidth) / 2;
- let top = this.sysInfo.screenHeight - bannerHeight;
- if (this.nativeBannerAd) {
- if (this.nativeBannerAd.isShow()) {
- utils.showLog("原生Banner正在显示,不重新创建!")
- } else {
- this.nativeBannerAd.show().then(() => {
- utils.showLog("原生Banner广告显示成功!");
- this.HideDefaultBanner();
- }).catch((err) => {
- utils.showLog("原生Banner广告显示失败!");
- this.nativeBannerAd && this.nativeBannerAd.destroy();
- this.nativeBannerAd = null;
- if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "native") {
- this._createBanner(location, args);
- }
- });
- }
- return;
- }
- //@ts-ignore
- let customAd = wx.createCustomAd({
- adUnitId: utils.config.wechatconfig.nativeBannerId,
- adIntervals: utils.wechatTool.ServerConfig.refresh_ad_time || 30,
- style: {
- left: left,
- top: top,
- width: bannerWidth
- }
- });
- utils.showLog("native nanner ad style left=" + left + ",top=" + top + "; screenWidth=" + this.sysInfo.screenWidth + ", screenHeight=" + this.sysInfo.screenHeight)
- customAd.onError((erro) => {
- utils.showLog("原生Banner广告异常!>>>>> #code=" + erro.errCode + " #msg=" + erro.errMsg);
- this.nativeBannerAd && this.nativeBannerAd.destroy();
- this.nativeBannerAd = null;
- if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "native") {
- this._createBanner(location, args);
- }
- })
- customAd.onLoad(() => {
- utils.showLog("原生Banner广告加载成功!");
- customAd.show().then(() => {
- this.nativeBannerAd && this.nativeBannerAd.destroy();
- this.nativeBannerAd = customAd;
- utils.showLog("原生Banner广告显示成功!");
- this.HideDefaultBanner();
- }).catch((err) => {
- utils.showLog("原生Banner广告显示失败!");
- this.nativeBannerAd && this.nativeBannerAd.destroy();
- this.nativeBannerAd = null;
- if (this.ServerConfig.banner_first_ad && this.ServerConfig.banner_first_ad == "native") {
- this._createBanner(location, args);
- }
- });
- })
- }
- nativeIntersititialAd: any = null;
- public showNativeIntersititialAd() {
- utils.showLog("展示原生插屏广告!");
- if (!utils.wechatTool.isOverMinVersion("2.11.1")) {
- utils.showLog("当前版本不支持原生模版广告!");
- return;
- }
- if (!this.ServerConfig) {
- utils.showLog("组件未初始化!");
- return;
- }
- if (!utils.config.wechatconfig.nativeInsertIds) {
- utils.showLog("原生插屏广告ID配置不存在");
- return;
- }
- let insertWidth = 345;
- let insertHeight = 420;
- if (cc.winSize.width > cc.winSize.height) {
- insertHeight = 300
- }
- let left = (this.sysInfo.screenWidth - insertWidth) / 2;
- let top = (this.sysInfo.screenHeight - insertHeight) / 2;
- if (this.nativeIntersititialAd) {
- if (this.nativeIntersititialAd.isShow()) {
- utils.showLog("原生插屏正在显示,不重新创建!")
- } else {
- this.nativeIntersititialAd.show().then(() => {
- utils.showLog("原生插屏广告显示成功!");
- }).catch((err) => {
- utils.showLog("原生插屏广告显示失败!");
- this.nativeIntersititialAd && this.nativeIntersititialAd.destroy();
- this.nativeIntersititialAd = null;
- if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "native") {
- this._createMiniGameInsertAd();
- }
- });
- }
- return;
- }
- //@ts-ignore
- let customAd = wx.createCustomAd({
- adUnitId: utils.config.wechatconfig.nativeInsertIds,
- adIntervals: 60,
- style: {
- left: left,
- top: top,
- width: insertWidth
- }
- });
- utils.showLog("native insert ad style left=" + left + ",top=" + top + "; screenWidth=" + this.sysInfo.screenWidth + ", screenHeight=" + this.sysInfo.screenHeight)
- customAd.onError((erro) => {
- utils.showLog("原生插屏广告异常!>>>>> #code=" + erro.errCode + " #msg=" + erro.errMsg);
- this.nativeIntersititialAd && this.nativeIntersititialAd.destroy();
- this.nativeIntersititialAd = null;
- if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "native") {
- this._createMiniGameInsertAd();
- }
- })
- customAd.onLoad(() => {
- utils.showLog("原生插屏广告加载成功!");
- customAd.show().then(() => {
- this.nativeIntersititialAd && this.nativeIntersititialAd.destroy();
- this.nativeIntersititialAd = customAd;
- utils.showLog("原生插屏广告显示成功!");
- }).catch((err) => {
- utils.showLog("原生插屏广告显示失败!");
- this.nativeIntersititialAd && this.nativeIntersititialAd.destroy();
- this.nativeIntersititialAd = null;
- if (this.ServerConfig.intersititial_first_ad && this.ServerConfig.intersititial_first_ad == "native") {
- this._createMiniGameInsertAd();
- }
- });
- })
- }
- }
|