/** * 平台管理器 * * @format */ import {Data, Mgr} from '../GameControl' import {WxUtils} from '../utils/WxUtils' import {CHANNEL, LANGUAGE_TYPE} from '../enums/Enum' import {ConstValue} from '../data/ConstValue' import {AdvertisementConfig} from '../config/AdvertisementConfig' export class PlatformManager { public isBackGround = false /** * 平台管理器各个平台工具初始化 */ public init() { this.checkUpdate() if (cc.sys.platform === cc.sys.WECHAT_GAME) { WxUtils.init() } cc.game.on(cc.game.EVENT_HIDE, () => { if (!this.isBackGround) { this.isBackGround = true } Mgr.audio.onViewHide() Mgr.net.stopKeepAlive() }) cc.game.on(cc.game.EVENT_SHOW, () => { if (this.isBackGround) { this.isBackGround = false } Mgr.audio.onViewShow() if (Mgr.net.webSocket && Mgr.net.webSocket.readyState === WebSocket.OPEN) Mgr.net.sendKeepAlive() }) cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this) Data.main.safeTop = this.getSafeTop() Data.main.channel = this.getCurChannelName() if (this.getCurChannelName() == CHANNEL.googleplay) { Object.keys(AdvertisementConfig).forEach(v => { if (parseInt(v) < 99902) this.googleLoadMobAD(v) }) } } public reset() { cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this) } /** * 是否是微信小游戏环境 */ public isWeChat() { // @ts-ignore let _global = typeof window === 'undefined' ? global : window // @ts-ignore return cc.sys.platform == cc.sys.WECHAT_GAME && !cc.sys.isBrowser && typeof _global.qq === 'undefined' } /** * 是否是微信H5环境 */ public isWeChatH5() { return cc.sys.isBrowser && navigator.userAgent.includes('MicroMessenger') } /** * 是否是Android本地环境 */ public isAndroid() { return cc.sys.os === cc.sys.OS_ANDROID && cc.sys.isNative } /** * 是否是IOS本地环境 */ public isIOS() { return cc.sys.os === cc.sys.OS_IOS && cc.sys.isNative } /** * 是否是微信小游戏运行在android环境下 */ public isWeChatAndroid() { return this.isWeChat() && cc.sys.os === cc.sys.OS_ANDROID } /** * 是否是微信小游戏运行在IOS环境下 */ public isWeChatIOS() { return this.isWeChat() && cc.sys.os === cc.sys.OS_IOS } /** * 是否是Android本地环境 */ public isWindows() { return cc.sys.os === cc.sys.OS_WINDOWS && cc.sys.isNative } /** * 是否在开发环境下 */ public isWindowsDev() { return CC_DEV } /** * 判断是否在普通的html5环境下 */ public isH5() { return cc.sys.isBrowser && !navigator.userAgent.includes('MicroMessenger') } /** * 获取(安全的显示高度)刘海屏高度 */ public getSafeTop() { if (this.isWeChat()) { // @ts-ignore const systemInfo = wx.getSystemInfoSync() // @ts-ignore let marginTop = systemInfo.safeArea.top console.log('systemInfo', systemInfo) if (marginTop > 20) { if (cc.sys.os === cc.sys.OS_IOS) { marginTop -= 10 // 适配全面屏iphone } } else if (marginTop == 20) { marginTop = 0 // 适配老版iphone 和全部android手机 } const paddingTop = cc.view.getVisibleSize().height * (marginTop / systemInfo.screenHeight) return paddingTop } else if (this.isIOS()) { return cc.view.getVisibleSize().height - cc.sys.getSafeAreaRect().height // IOS原生 cocos已封装 } else if (this.isAndroid()) { let height = jsb.reflection.callStaticMethod( 'org/cocos2dx/javascript/service/GameSDK', 'getNotchHeight', '()I', ) //console.log('getSafeTop--android-->', height) return height } else if (this.isWindowsDev()) { return 0 } else if (this.isH5()) { return cc.view.getVisibleSize().height - cc.sys.getSafeAreaRect().height } else if (this.isWeChatH5()) { return cc.view.getVisibleSize().height - cc.sys.getSafeAreaRect().height } else { return 0 } } public gc() { console.log('gc') if (this.isWeChat()) { // @ts-ignore wx['triggerGC'] && wx['triggerGC']() } else if (this.isIOS() || this.isAndroid()) { if (CC_JSB) { cc.sys.garbageCollect() } } } /** * 检测用户有没有授权 * @param success * @param fail */ public checkUserIsAuthorize(success: Function, fail: Function) { if (this.isWeChat()) { WxUtils.checkUserIsAuthorize(success, fail) } } public createAuthorizeButton(success, fail, node: cc.Node) { let btn = null if (this.isWeChat()) { btn = WxUtils.createAuthorizeButton(success, fail, node) } return btn } //复制功能 public copyToClipboard(text: string) { if (this.isWindowsDev()) { navigator.clipboard?.writeText(text).then(() => { Mgr.ui.tip(LANGUAGE_TYPE.copySuccess) }) } else if (cc.sys.os === cc.sys.OS_ANDROID) { Mgr.ui.tip(LANGUAGE_TYPE.copySuccess) jsb.reflection.callStaticMethod( 'org/cocos2dx/javascript/service/GameSDK', 'copyTextToClipboard', '(Ljava/lang/String;)V', text, ) } } public launchBack() { if (this.isWeChat()) { WxUtils.launchBack() } } public onShow(callBack?: Function) { if (this.isWeChat()) { WxUtils.onShow(callBack) } } public checkUpdate() { if (this.isWeChat()) { WxUtils.checkUpdate() } } public playVideoAD(adid: number | string, onsuccess: Function, onerr?: Function) { adid = adid.toString() if (this.isWeChat()) { WxUtils.showRewardAd(adid, onsuccess, onerr) } else if (this.isWindowsDev()) { onsuccess && onsuccess() } else if (this.isAndroid()) { if (ConstValue.DEBUG) { onsuccess && onsuccess() } else { window['SDKUtils'].adSuccessCB = onsuccess window['SDKUtils'].adFailCB = errCode => { if (errCode == 1001) { Mgr.ui.tip(LANGUAGE_TYPE.adNotInit) } else if (errCode == 1002) { Mgr.ui.tip(LANGUAGE_TYPE.adLoading) } onerr && onerr(errCode) } this.googleMobAD(adid) } } } public getCurChannelName() { let ret = CHANNEL.none if (Mgr.platform.isWeChat()) { } else if (Mgr.platform.isH5()) { } else if (Mgr.platform.isWeChatH5()) { } else if (Mgr.platform.isAndroid()) { ret = jsb.reflection.callStaticMethod( 'org/cocos2dx/javascript/service/GameSDK', 'getChannel', '()Ljava/lang/String;', ) } return ret } public onKeyDown(event) { switch (event.keyCode) { case cc.macro.KEY.back: if (this.isAndroid()) { let className = 'org/cocos2dx/javascript/SDKUtils' let methodName = 'gotoBackPressed' let methodSignature = '()V' jsb.reflection.callStaticMethod(className, methodName, methodSignature) } break } } public logout() { if (this.isAndroid()) { //0测试1游客2facebook3google let funcNameArr = ['', '', 'facebookLogout', 'googleLogout'] let funcName = funcNameArr[Data.main.loginType] if (funcName) jsb.reflection.callStaticMethod('org/cocos2dx/javascript/service/GameSDK', funcName, '()V') } } public facebookLogin() { if (this.isAndroid()) { jsb.reflection.callStaticMethod('org/cocos2dx/javascript/service/GameSDK', 'facebookLogin', '()V') } } public googleLogin() { if (this.isAndroid()) { jsb.reflection.callStaticMethod( 'org/cocos2dx/javascript/service/GameSDK', 'googleLogin', '(Ljava/lang/String;)V', '1053688457546-tfl1l8l5mkc04v0qng2ogn2jkopafh49.apps.googleusercontent.com', ) } } public facebookAD(AdID) { AdID = 'YOUR_PLACEMENT_ID' if (this.isAndroid()) { jsb.reflection.callStaticMethod( 'org/cocos2dx/javascript/service/GameSDK', 'facebookAd', '(Ljava/lang/String;)V', AdID, ) } } public googleMobAD(AdID) { let googleAdID = AdvertisementConfig[AdID].google if (this.isAndroid()) { jsb.reflection.callStaticMethod( 'org/cocos2dx/javascript/service/GameSDK', 'googleMobAD', '(Ljava/lang/String;)V', googleAdID, ) } } public googleLoadMobAD(AdID) { let googleAdID = AdvertisementConfig[AdID].google if (this.isAndroid()) { jsb.reflection.callStaticMethod( 'org/cocos2dx/javascript/service/GameSDK', 'googleReadyRewardedVideoAd', '(Ljava/lang/String;)V', googleAdID, ) } } public getUniqueDeviceId() { if (this.isAndroid()) { return jsb.reflection.callStaticMethod( 'org/cocos2dx/javascript/service/GameSDK', 'getUniqueDeviceId', '()Ljava/lang/String;', ) } } public buyItem(productId: string, orderId: string) { if (this.isAndroid()) { if (Data.main.channel == CHANNEL.googleplay) { jsb.reflection.callStaticMethod( 'org/cocos2dx/javascript/service/GameSDK', 'googlePay', '(Ljava/lang/String;Ljava/lang/String;)V', productId, orderId, ) } } } public hideSplash() { if (this.isAndroid()) { return jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'hideSplash', '()V') } } }