export default class xmSdk {
static AdId = '12345';//广告id
static bannerAd = null;
static videoInstance = null;
/**
* 获取对象
*/
static xmObj: any = Laya.Browser.window.qg;
// static isDevtools: Boolean = false;
//播放banner广告
static playBannerAd: Function = (complete?: Function, close?: Function): void => {
let xm = xmSdk.xmObj;
// console.log('创建bannerad', xm);
if (xm != undefined) {
let width = Laya.Browser.clientWidth;
let height = Laya.Browser.clientHeight;
var targetBannerAdWidth = 128;
if (xmSdk.bannerAd) {
xmSdk.bannerAd.hide();
// xmSdk.bannerAd.destory();
}
// console.log('创建bannerad')
// 创建一个居于屏幕底部正中的广告
xmSdk.bannerAd = xm.createBannerAd({
adUnitId: xmSdk.AdId,
style: {
width: targetBannerAdWidth,
top: height - (targetBannerAdWidth / 16) * 9, // 根据系统约定尺寸计算出广告高度
},
});
// console.log('创建bannerad完成',xmSdk.bannerAd)
xmSdk.bannerAd.onResize((size) => {
// console.log(size.width, size.height);
xmSdk.bannerAd.style.top = height - size.height;
xmSdk.bannerAd.style.left = (width - size.width) / 2;
});
// console.log('bannerad重设大小完成',xmSdk.bannerAd.style.top,xmSdk.bannerAd.style.left)
xmSdk.bannerAd.onLoad(function () {
console.log('load success')
});
xmSdk.bannerAd.onError(data => {
console.log(`Banner Error: errorMsg: ${data.msg}, erroCode: ${data.code}`)
});
xmSdk.bannerAd.show();
// console.log('显示成功')
} else {
if (complete) {
complete();
}
if (close) {
close();
}
}
}
static hideBannerAd() {
if (this.bannerAd) {
this.bannerAd.hide();
}
}
/**
* 播放激励视频广告
*/
static playVideoAd: any = (complete?: any, close?: any): void => {
let xm = xmSdk.xmObj;
if (xm != undefined) {
xmSdk.videoInstance = xm.createRewardedVideoAd({
adUnitId: xmSdk.AdId,
});
// 显示激励广告
xmSdk.videoInstance.show()
.then(() => {
console.log("激励广告显示成功");
//播放激励广告视频
}).catch((err) => {
console.log("激励广告组件出现问题", err);
if (err.errCode != 1004) {
// 这里需要调用组件提示
// code here
}
if (close) {
close();
}
});
xmSdk.videoInstance.onClose((res) => {
if (res.isEnded) {
// 给予奖励
if (complete) {
complete();
complete = null;
}
} else {
if (close) {
close();
}
}
xmSdk.clearTheVideoAd();
xmSdk.videoInstance = null;
});
} else {
//播放广告视频
if (complete) {
complete();
}
if (close) {
close();
}
}
}
public static clearTheVideoAd() {
if (xmSdk.videoInstance != null) {
xmSdk.videoInstance.offLoad(() => { });
xmSdk.videoInstance.offClose(() => { });
xmSdk.videoInstance.close();
xmSdk.videoInstance = null;
}
}
/**
* 播放插屏广告
* @param complete
*/
static playPageAd: Function = (complete?: Function, error?: Function): void => {
console.log("调用playPageAd()展示插屏广告");
let xm = xmSdk.xmObj;
if (xm != undefined) {
let interstitialAd = xm.createInterstitialAd({
adUnitId: xmSdk.AdId,
});
interstitialAd.show().then(() => {
console.log("插屏广告显示成功");
//播放激励广告视频
}).catch((err) => {
console.log("插屏广告组件出现问题", err);
if (err.errCode != 1004) {//这里的errCode是其他模板里的,自行修改吧
// 这里需要调用组件提示
// code here
}
if (close) {
close();
}
});
}
}
/**
*
* @param callback
*/
static onHide(callback?: any) {
this.xmObj.onHide(() => {
if (callback) callback();
});
}
static login() {
this.xmObj.login({
success(res) {
console.log(`login 调用成功${res.code} ${res.anonymousCode}`);
},
fail(res) {
console.log(`login 调用失败`);
},
});
}
}