import { _decorator, Component, sys, utils } from "cc"; import Utils from "../Common/Utils"; import { WXSDK } from "./wxSDK"; import { ByteDanceSDK } from "./bdSDK"; import { WebSDK } from "./webSDK"; import { KSSDK } from "./ksSDK"; const { ccclass, property } = _decorator; /**运行渠道 */ export enum E_SDK_Channel { /**微信 */ WX, /**字节跳动 (抖音、头条) */ BD, /**网页 */ WEB, /**快手 */ KS, } /**玩家SDK信息 */ export interface I_SDK_UserInfo { /**昵称 */ nickName: string; /**头像url */ avatarUrl: string; } /**玩家SDK信息回调 */ export interface I_SDK_UserInfo_Callback { (info: I_SDK_UserInfo): void; } //渠道管理器 @ccclass('SDKManager') export class SDKManager { //extends Component private static _I: SDKManager = null; public static get I(): SDKManager { if (!SDKManager._I) { SDKManager._I = new SDKManager(); SDKManager._I.init(); } return SDKManager._I; } private init(){ } initEmpty(){ } private static sdk_channel:E_SDK_Channel = E_SDK_Channel.WEB //当前SDK渠道 private static video_reward_map:any private static share_reward_map:any private static sdk:any //初始化SDK static init(cb: Function = null) { this.video_reward_map = {} this.share_reward_map = {} console.log("平台值 wx:",window["wx"], typeof window["wx"]) console.log("平台值 ks:",window["ks"], typeof window["ks"]) console.log("平台值 tt:",window["tt"], typeof window["tt"]) console.log("当前平台:",sys.platform) if (typeof window["ks"] !== 'undefined') { this.sdk_channel = E_SDK_Channel.KS this.sdk = new KSSDK() //快手 }else if (sys.platform === sys.Platform.WECHAT_GAME && typeof window["wx"] !== 'undefined') { this.sdk_channel = E_SDK_Channel.WX this.sdk = new WXSDK() //微信 } else if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME && typeof window["tt"] !== 'undefined') { this.sdk_channel = E_SDK_Channel.BD this.sdk = new ByteDanceSDK() //抖音(头条) } else { this.sdk_channel = E_SDK_Channel.WEB this.sdk = new WebSDK() } // if (sys.platform === sys.Platform.WECHAT_GAME) { // this.sdk = new WXSDK() // } else if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME) { // this.sdk = new ByteDanceSDK() // } else { // this.sdk = new WebSDK() // } this.sdk.init(cb) } /**是否是微信平台 */ static isWenxin() { return this.sdk_channel === E_SDK_Channel.WX } /**是否是抖音平台 */ static isByteDance() { return this.sdk_channel === E_SDK_Channel.BD } /**是否是快手平台 */ static isKuaishou() { return this.sdk_channel === E_SDK_Channel.KS } /**是否是web平台 */ static isWeb() { return this.sdk_channel === E_SDK_Channel.WEB } //SDK登录,并返回code static login(cb: Function = null) { this.sdk.login(cb) } //检查玩家权限 //autoKey 授权类型 : userInfo=用户信息 static checkAutoSetting(autoKey:string, cb:Function = null) { this.sdk.checkAutoSetting(autoKey, cb) } //获取用户信息 static getUserInfo(cb:Function = null) { this.sdk.getUserInfo(cb) } //注册视频广告回调 static register_video_reward(uuid:string, func:Function) { let video_reward_map = this.video_reward_map || {}; video_reward_map[uuid] = func this.video_reward_map = video_reward_map; } //注册分享回调 static register_share_reward(uuid:string, func:Function) { let share_reward_map = this.share_reward_map || {}; share_reward_map[uuid] = func this.share_reward_map = share_reward_map; } //执行视频广告回调 private static dispatch_video_reward(uuid:string, tag:number) { let video_reward_map = this.video_reward_map || {}; let register_func:Function = video_reward_map[uuid]; if (register_func) { register_func(tag) } } //执行分享回调 private static dispatch_share_reward(uuid:string, tag:number) { let share_reward_map = this.share_reward_map || {}; let register_func:Function = share_reward_map[uuid]; if (register_func) { register_func(tag) } } //视频广告 static show_reward_video_ad(uuid:string, tag: number) { this.sdk.show_video(()=>{ this.dispatch_video_reward(uuid,tag) }); } //插屏广告 static show_interstitial_ad() { this.sdk.show_splash(); } //展示BANNER static show_banner_ad() { this.sdk.show_banner(); } //隐藏BANNBER static hide_banner_ad() { this.sdk.hide_banner(); } //主动拉起分享 static show_share() { this.sdk.show_share(); } //分享得奖励 static show_reward_share(uuid:string, tag: number) { this.sdk.show_share(()=>{ this.dispatch_share_reward(uuid,tag) }); } //获取设备分辨率 static getWindowSize() { return this.sdk.getWindowSize(); } /**显示游戏圈 * @param leftRatio 左边距占屏幕宽度的比例 * @param topRatio 顶部边距占屏幕高度的比例 * @param widthRatio 游戏圈宽度占屏幕宽度的比例 * @param heightRatio 游戏圈高度占屏幕高度的比例 */ static show_gameClub(leftRatio, topRatio, widthRatio, heightRatio) { this.sdk.show_gameClub(leftRatio, topRatio, widthRatio, heightRatio); } //隐藏游戏圈 static hide_gameClub() { this.sdk.hide_gameClub(); } /**检查是否支持base64音频播放*/ static checkSupportBase64Audio() { return this.sdk.checkSupportBase64Audio(); } /**播放base64音频 */ static playBase64Audio(base64: string, loop: boolean = false) { this.sdk.playBase64Audio(base64, loop); } //记录GM日志 private static _gmLogString: string = ""; private static _gmLogCount: number = 0; //记录日志条数 static get GMLogString(): string { return SDKManager._gmLogString; } static ShowGMLog(log: string){ Utils.Log(log); if (SDKManager._gmLogCount > 100){ SDKManager.ClearGMLog(); } SDKManager._gmLogString += log + "\n"; SDKManager._gmLogCount++; } static ClearGMLog(){ SDKManager._gmLogString = ""; SDKManager._gmLogCount = 0; } /**是否是微信小程序 */ static get isWeChatGame(): boolean { Utils.Log(`platform = ${sys.platform}`); return sys.platform == sys.Platform.WECHAT_GAME; } }