大侠
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

338 lines
11 KiB

import { sys } from "cc";
import MathUtils from "../../ExUtils/MathUtils";
import { EGlobalEvent } from "../../MesEvent/EGlobalEvent";
import MesMgr from "../../MesEvent/MesMgr";
import PlatformData from "../Data/PlatformData";
import { EPlatformType } from "../T/EPlatformType";
import { ShareInfo } from "../T/ShareInfo";
import WXPlatform from "./WXPlatform";
/**
* 头条平台实例
*/
export default class KSPlatform extends WXPlatform {
IsLeaderboardsEnable: boolean=false;
platform: EPlatformType = EPlatformType.KS;
protected _showVideoLoad: boolean = true;
protected openIDUrl:string = "https://game-api.lvzhouyouxi.com/getOpenId_line_ks";
isSliderBar = false;
Init(platformData: PlatformData,callBack:Function) {
this._base = window["ks"]
if (this._base == null) {
console.warn("平台初始化错误");
return;
}
this.platformData = platformData;
this.isAddShortcut = false;
// this._InitShareList();
this._InitLauchOption();
this._Login(callBack);
this._InitShareInfo();
this._InitSystemInfo();
this._CreateBannerAd();
this._CreateVideoAd();
this._CreateInterstitalAd();
this.checkShortcut((isOK)=>{this.isAddShortcut = isOK});
window["ks"].checkSliderBarIsAvailable({
success: (res) => {
console.log("检查侧边栏功能是否可用 success: ", res.available);
this.isSliderBar = res.available;
},
fail: (res) => {
console.log("check scene fail:", res);
//失败回调逻辑
}
});
this._base.onShow((res) => {
console.log("启动参数:", res.query);
console.log("来源信息:", res.refererInfo);
console.log("场景值:", res.scene);
console.log("启动场景字段:", res.from, ", ", res.location);
if (res.from == 'sidebar_new') {
console.log('从侧边栏启动')
window["cebianlan"] = true;
MesMgr.event(EGlobalEvent.E_CHECK_CEBIAN_LAN);
}
});
window["iplatform"] = this;
}
_CheckUpdate(){ }
_InitShareInfo(){}
protected _CreateVideoAd() {
if (this.platformData.rewardVideoId.length <= 0) {
console.warn("无有效的视频广告ID,取消加载");
return;
}
let CurRewardVideoId = this.platformData.rewardVideoId;//MathUtils.RandomFromArray(this.platformData.rewardVideoId)
console.log('vedio ad id', CurRewardVideoId)
if (!this._cacheVideoAD) {
console.log("当前策略为不缓存视频广告");
return;
}
let createRewardedVideoAd = this._base["createRewardedVideoAd"];
if (createRewardedVideoAd == null) {
console.warn("无createRewardedVideoAd方法,跳过初始化");
return;
}
if (!CurRewardVideoId) {
console.log("无有效的视频广告ID,取消加载");
return;
}
this._videoFailedCount = 0;
let videoObj = {};
videoObj["adUnitId"] = CurRewardVideoId;
this._rewardVideo = createRewardedVideoAd(videoObj);
if(!this._rewardVideo){
console.log("无有效的视频广告组件");
return;
}
this._rewardVideo.onError((res) => {
this._videoFailedCount++;
console.warn("视频广告加载失败", res);
if (this._videoFailedCount > 10) {
console.log("第", this._videoFailedCount, "次重新加载视频广告");
}
});
this._rewardVideo.onClose((res) => {
console.log("视频回调", res);
let isEnd = res["isEnded"] as boolean;
if (isEnd) {
if (this._rewardSuccessed) this._rewardSuccessed();
} else {
if (this._rewardSkipped) this._rewardSkipped();
}
});
this._isVideoLoaded = true;
}
protected _CreateBannerAd() {
if (this.platformData.bannerId) {
console.log("无有效的banner广告ID,取消加载");
return;
}
let curbannerId = this.platformData.bannerId;//MathUtils.RandomFromArray()
console.log('bannerId ad id', curbannerId);
if (!curbannerId) {
console.log("无有效的横幅广告ID,取消加载");
return;
}
let windowWidth = this._base.getSystemInfoSync().windowWidth;
let windowHeight = this._base.getSystemInfoSync().windowHeight;
let bannerObj = {};
bannerObj["adUnitId"] = curbannerId;
bannerObj["adIntervals"] = 30;
let styleObj = {};
styleObj["left"] = 0;
styleObj["top"] = 0;
styleObj["width"] = windowWidth;
bannerObj["style"] = styleObj;
this._bannerAd = this._base.createBannerAd(bannerObj);
this._isBannerLoaded = false;
if (this._bannerAd) {
this._bannerAd.onLoad(() => {
console.log("banner加载成功", this._bannerAd);
this._isBannerLoaded = true;
});
this._bannerAd.onError((res) => {
console.warn("banner广告加载失败", res);
this._bannerAd == null;
});
this._bannerAd.onResize((size) => {
this._bannerAd.style.top = windowHeight - size.height;
this._bannerAd.style.left = (windowWidth - size.width) / 2;
});
}
}
protected _CreateInterstitalAd() {
if (this.platformData.interstitialId.length < 1) {
console.log("无有效的插页广告ID,取消加载");
return;
}
let curinterstitialId = MathUtils.RandomFromArray(this.platformData.interstitialId)
if (!curinterstitialId) {
console.log("无有效的插页广告ID,取消加载");
return;
}
this._interstitalFailedCount = 0;
let intAdObj = {};
intAdObj["adUnitId"] = curinterstitialId;
this._intersitialAd = this._base.createInterstitialAd(intAdObj);
if (!this._intersitialAd){
console.log("无有效的插页广告组件");
return;
}
this._intersitialAd.onError((err) => {
this._interstitalFailedCount++;
console.warn("插页广告加载失败", err);
if (this._interstitalFailedCount > 10) {
console.log("第", this._interstitalFailedCount, "次重新加载插页广告");
// 失败自动加载广告
this._intersitialAd.load();
}
});
console.log("插页广告加载成功");
this._isInterstitialLoaded = true;
}
RecordEvent(eventId: string, param: object) {
let reportAnalytics = this._base["reportAnalytics"];
if (reportAnalytics) {
if (param == null) {
param = {};
}
reportAnalytics(eventId, param);
} else {
console.warn("reportAnalytics 方法不存在");
}
}
ShowBannerAd() {
if (!this.IsBannerAvaliable()) {
return;
}
this._bannerAd.show();
}
ShareAppMessage(shareInfo: ShareInfo, onSuccess: Function, onFailed: Function) {
console.log("分享消息", shareInfo);
let shareObj = this._WrapShareInfo(shareInfo);
shareObj["success"] = () => {
if (onSuccess) {
onSuccess();
}
}
shareObj["fail"] = () => {
if (onFailed) {
onFailed();
}
}
this._base.shareAppMessage(shareObj);
}
protected _WrapShareInfo(shareInfo: ShareInfo) {
let shareObj = {};
if (shareInfo.shareTitle) {
shareObj["title"] = shareInfo.shareTitle;
}
if (shareInfo.shareID) {
shareObj["templateId"] = shareInfo.shareID;
}
if (shareInfo.sharePath) {
shareObj["query"] = {};
let pathSplit = shareInfo.sharePath.split("?");
let params = pathSplit[1].split("&");
for (let getParam of params) {
let splitParam = getParam.split("=");
shareObj["query"][splitParam[0]] = splitParam[1];
}
}
return shareObj;
}
OpenGameBox(appIds: string[]) {
let openData = [];
for (let i = 0; i < appIds.length; ++i) {
openData.push({
appId: appIds[i]
});
}
this._base.showMoreGamesModal({
appLaunchOptions: openData
});
}
NavigateToApp(appid: string, path?: string, extra?: any): Promise<boolean> {
return new Promise((resolve, reject) => {
if (!this.isSupportJumpOther) {
reject(false);
console.log("当前平台不支持小游戏跳转", this);
} else {
this._base.showMoreGamesModal({
appLaunchOptions: [
{
appId: this.platformData.appId,
query: "foo=bar&baz=qux",
extraData: {}
}
],
success(res) {
resolve(true);
console.log("跳转小游戏成功", appid);
},
fail(err) {
reject(false);
console.log("跳转小游戏失败", appid);
}
});
}
});
}
GetUserCloudStorage() {
}
SetUserCloudStorage() {
}
get isCanShowRecommend(){
return false;
};
createShortcut(BackFun:(isOK:boolean)=>void) {
if (sys.os == sys.OS.IOS) {
this.base.addShortcut({
success: () => { },
fail: () => { }
})
BackFun && BackFun(true)
} else {
this.base.addShortcut({
success: () => {
BackFun && BackFun(true)
},
fail: () => {
BackFun && BackFun(false)
}
})
}
}
checkShortcut(BackFun:(isOK:boolean)=>void) {
if (sys.os == sys.OS.IOS) {
this.base.checkShortcut({
success: () => { },
fail: () => { }
})
ConsoleEx.Log("IOS不支持添加桌面图标")
BackFun && BackFun(true)
} else {
this.base.checkShortcut({
success: (res) => {
BackFun && BackFun(res.installed)
},
fail: () => {
BackFun && BackFun(false)
}
})
}
}
}