123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- /**
- * 平台管理器
- *
- * @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')
- }
- }
- }
|