const CCComFun = require("CCComFun")
var AdsFuncWD = {
bannerId: null, //横幅广告id
videoId: null, //视频id
insertId: null, //插屏id
bannerAd: null,
videoAd: null,
insertAd: null,
netState: true,
isInitNode: false,
bannerNode: null,
cur_bannerHeight: 0,
isShowBanner: true,
isChangeSize: false, //是否正在轮转
isInCreateBanner: false, //是否正在创建
isCenterBanner: false,
isShowInsert: true, //是否显示插屏
blockId: null, //积木广告id
blockAd: null, //积木广告
isShowBlock: true, //是否展示积木广告
blockNode: null, //用于定位积木广告的节点
seconds: 0, //计时用
timing: null, //记录当前是否在计时中
//判断网络状态
initAd: function (obj) {
this.initParms();
this.getNetworkStatusChange();
obj.bannerId && (this.bannerId = obj.bannerId);
obj.videoId && (this.videoId = obj.videoId);
obj.insertId && (this.insertId = obj.insertId);
obj.blockId && (this.blockId = obj.blockId);
},
getNetworkStatusChange: function () {
qq.getNetworkType({
success: (res) => {
if (res.networkType != "none") {
this.netState = true
} else {
this.netState = false
}
},
fail: (res) => {
}
});
qq.onNetworkStatusChange(res => {
console.log("net:" + res.isConnected);
this.netState = res.isConnected;
})
return this.netState;
},
initParms: function () {
this.systemInfo = qq.getSystemInfoSync();
this.commonSize = cc.view.getFrameSize();
this.totalSize = cc.view.getVisibleSize();
//广告距离 游戏底部和设备底部的距离 iphoneX 50 其他20(暂时修改成 与iphonx的比例值)
//默认dis_side = 30
if (this.checkIphoneXDevice()) {
this.dis_side = 30;
} else {
this.dis_side = 30 * (this.systemInfo.windowHeight / 812);
}
},
//2分钟轮转
changeBanner: function () {
let cur_time = cc.sys.localStorage.getItem("QQ_bannerTime");
let now_time = Date.parse(new Date());
if (!cur_time) {
cc.sys.localStorage.setItem("QQ_bannerTime", now_time);
return true;
}
let dis_time = now_time * 1 - cur_time;
if (dis_time < 120 * 1000) {
return false;
} else {
cc.sys.localStorage.setItem("QQ_bannerTime", now_time);
}
return true;
},
createBanner: function (bannerID) {
// if (!this.netState || !this.bannerId) return;
// isHide ? this.setShowBanner(false) : this.setShowBanner(true);
//refreshPerMin参数为true表示banner每分钟刷新
this.makeBanner(bannerID);
},
//banner实例已存在,直接展示;
showExistBanner() {
if (this.isShowBanner) {
let adshow = this.bannerAd && this.bannerAd.show();
adshow && adshow.then(() => {
if (!this.isShowBanner) {
this.hideBanner();
}
}).catch();
} else {
this.hideBanner();
}
this.bannerAd.style.top = this.commonSize.height - this.h;
// this.onResize(this.bannerAd);
},
//创建新的广告实例
makeBanner(id) {
let bannerAd = qq.createBannerAd({
adUnitId: id,
style: {
left: 0,
top: this.commonSize.height - this.h, //this.commonSize.height
width: this.systemInfo.windowWidth,
}
})
this.loadFun = () => {
console.log("banner显示成功");
this.bannerAd = bannerAd;
if (this.isShowBanner) {
let adshow = bannerAd && bannerAd.show()
adshow && adshow.then(() => {
if (!this.isShowBanner) {
this.hideBanner();
}
}).catch()
} else {
this.hideBanner();
}
this.onResize(bannerAd);
}
if (bannerAd) {
this.loadFun && bannerAd.offLoad(this.loadFun)
bannerAd.onLoad(this.loadFun);
bannerAd.onError((res) => {
console.log('banner-onError', res.errMsg, res.errCode);
});
}
},
onResize: function (bannerAd) {
bannerAd.onResize(res => {
bannerAd.style.top = this.commonSize.height - this.h;
bannerAd.style.left = (this.systemInfo.windowWidth - res.width) / 2
})
},
showBanner: function () {
this.bannerAd.show();
},
hideBanner: function () {
if (!this.bannerAd) return;
this.bannerAd.hide();
},
moveBannerOut: function () {
if (!this.bannerAd) return;
this.bannerAd.style.top = this.commonSize.height;
},
/**
*
* @param {node} node 如果有预留界面,在createNewBanner之前,更新bannerNode
*/
initBannerNode: function (node) {
if (!node) return;
this.isInitNode = true;
this.bannerNode = node;
this.commonSize = cc.view.getFrameSize();
this.isShowBanner = true;
//固定创建广告宽度:300(最小)
this.totalSize = cc.view.getVisibleSize();
this.systemInfo = qq.getSystemInfoSync();
let nodeSize = this.bannerNode.getContentSize();
let referPos = cc.v2(this.bannerNode.x, this.bannerNode.y - nodeSize.height / 2);
let pos = this.bannerNode.parent.convertToWorldSpaceAR(referPos);
this.h = (pos.y / this.totalSize.height) * this.systemInfo.windowHeight;
},
/**
*
* @param clearBannerNode 清理广告位 节点
*/
clearBannerNode: function () {
this.isInitNode = false;
this.bannerNode = null;
},
/**
*
* @param {node} node 如果有预留界面,在createBlockAd之前,更新blockNode
*/
initBlockNode: function (node) {
if (!node) return;
this.blockNode = node;
this.commonSize = cc.view.getFrameSize();
//固定创建广告宽度:300(最小)
this.totalSize = cc.view.getVisibleSize();
this.systemInfo = qq.getSystemInfoSync();
},
//积木广告,在基础库1.15.0 版本开始支持
/**
*
* @param {*} obj
* size:积木广告的个数,需要传入数字,默认为1
* orientation:积木广告的方向,需要传入0或者1:,0:表示横向,1:表示竖向,默认为横向
*/
createBlockAd: function (obj) {
let SDKVersion = this.systemInfo.SDKVersion;
let result = CCComFun.compareVersion(SDKVersion, "1.15.0"); //小于1.15.0的版本不显示 广告盒子
if (!result) return console.log("该基础库不支持积木广告");
if (!this.netState || !this.blockId) return console.log("没有网络或者blockId不存在");
// let isHide = obj.isHide;
// isHide ? this.setShowBlock(false) : this.setShowBlock(true);
// this.commonSize = cc.view.getFrameSize();
this.isShowBlock = true;
let id = this.blockId;
//固定创建广告宽度:300(最小)
// this.totalSize = cc.view.getVisibleSize(); //设计分辨率:720 1560
let pos = this.blockNode.parent.convertToWorldSpaceAR(this.blockNode);
this.bh = (pos.y / this.totalSize.height) * this.systemInfo.windowHeight;
let ratioY = 1 - (pos.y / this.totalSize.height); //获取当前展示的位置中心点的x,y坐标比例
let ratioX = pos.x / this.totalSize.width;
this.ratioX = ratioX;
this.ratioY = ratioY;
// console.log("积木广告:this.commonSize:",this.commonSize, " this.bh:",this.bh ," cha:",this.commonSize.height - this.bh)
let size = obj.size ? obj.size : 1;
let orientation = obj.orientation ? "vertical" : "landscape";
// console.log("积木广告:size:", size, " orientation:", orientation)
let width = this.totalSize.width;
this.pixelRatio = this.systemInfo.pixelRatio;
let top = this.commonSize.height * this.pixelRatio * ratioY;
let left = this.commonSize.width * this.pixelRatio * ratioX;
let system = this.systemInfo.system;
if (system.indexOf("iOS") !== -1) { //ios
this.pixelRatio = 1;
top = this.commonSize.height - this.bh;
width = this.commonSize.width;
//因为当前版本在IOS上不支持动态改变积木广告的高度
//故在创建时根据展示的积木广告数量直接设定广告高度
}
let oneBox_h = 78;
let oneBox_w = 60;
switch (orientation) {
case "vertical":
top -= ((oneBox_h * size) / 2 * this.pixelRatio);
left -= (oneBox_w / 2 * this.pixelRatio);
break;
case "landscape":
top -= (oneBox_h / 2 * this.pixelRatio);
left -= ((oneBox_w * this.pixelRatio * size) / 2);
break;
}
this.designTop = top;
// console.log("设计的高度是",this.designTop);
if (system.indexOf("iOS") !== -1) { //ios
left = 100;
} else {
let result1 = CCComFun.compareVersion(SDKVersion, "1.16.0");
if (result1) {
left = 100;
top = this.commonSize.height - this.bh;
this.pixelRatio = 1;
}
}
this.destroyBlock();
this.setShowBlock(true);
let blockAd = qq.createBlockAd({
adUnitId: id,
style: {
left: left,
top: top,
},
size: size,
orientation: orientation
})
this.resize = false;
this.blockAdOnResize(blockAd);
this.loadFun = () => {
// console.log("积木广告-加载成功");
this.blockAd = blockAd;
if (this.isShowBlock) {
let adshow = blockAd && blockAd.show()
adshow && adshow.then(() => {
if (!this.isShowBlock) {
this.hideBlock();
}
}).catch()
} else {
this.hideBlock();
}
}
this.errFun = (res) => {
console.log('blockAd-onError:', res)
}
if (blockAd) {
this.loadFun && blockAd.offLoad(this.loadFun)
blockAd.onLoad(this.loadFun);
this.errFun && blockAd.offError(this.errFun)
blockAd.onError(this.errFun);
}
},
destroyBanner: function () {
if (!this.bannerAd) return;
this.bannerAd.hide();
this.bannerAd.destroy();
this.bannerAd = null;
},
setShowBlock: function (isBool) {
this.isShowBlock = isBool;
},
//展示积木广告
showBlock: function () {
this.setShowBlock(true);
if (!this.blockAd) return;
this.blockAd.show();
},
//隐藏积木广告
hideBlock: function () {
this.setShowBlock(false);
if (!this.blockAd) return;
this.blockAd.hide();
},
//销毁积木广告
destroyBlock: function () {
this.setShowBlock(false);
if (!this.blockAd) return;
this.blockAd.hide();
this.blockAd.destroy();
this.blockAd = null;
},
blockAdOnResize: function (blockAd) {
let system = this.systemInfo.system;
let result = false;
if (system.indexOf("iOS") !== -1) {//ios
result = true;
} else {
let SDKVersion = this.systemInfo.SDKVersion;
let result1 = CCComFun.compareVersion(SDKVersion, "1.16.0");
if (result1) {
result = true;
}
}
if (result) {
blockAd.offResize();
blockAd.onResize(res => {
// console.log("blockAd-onResize-width:",res.width," height:",res.height)
let value = this.commonSize.width * this.pixelRatio * this.ratioX;
let Y = this.commonSize.height * this.pixelRatio * this.ratioY;
blockAd.style.left = Math.ceil(value - res.width / 2);
blockAd.style.top = Math.floor(Y) - Math.floor(res.height / 2);
})
}
},
};
module.exports = AdsFuncWD;
AdsWD.initBannerNode(this.adNode_info);
AdsWD.createBanner('d8635b71e0b0381b7035244d48181199');
let cnt = 1;
for (let i = 1; i <= adTime; i++) {
setTimeout(() => {
AdsWD.initBannerNode(this.adNode_info);
AdsWD.createBanner('d8635b71e0b0381b7035244d48181199');
cnt++;
console.log('cnt', cnt);
if (cnt == 7) {
// AdsWD.hideBanner();
AdsWD.destroyBanner();
}
// }, this.bannerRotation[0] * 1000 * i);
}, 5000 * i);
}