const BuildingManager = require("../utils/BuildingManager"); const ShareAction = require('../utils/ShareAction'); const HomeApi = require("../net/HomeApi"); const SkillApi = require("../net/SkillApi"); const WeChat = require("../net/WeChat"); const AlertManager = require('../utils/AlertManager'); const {GameNotificationKey, WechatShareType } = require('../utils/GameEnum'); const GameModule = require("../utils/GameModule"); /// 处理 class Platform { static initPlatform() { if (window.tt != undefined) { this.handelTTLifeCyle(); }else if (CC_WECHATGAME) { this.handelWxLifeCyle(); } else if (CC_QQPLAY) { this.handelQQLifeCycle(); } } static handelQQLifeCycle() { BK.onMaximize( () => { BK.Script.log('游戏最小化'); Platform.handelOnHide(); }) BK.onGameClose(() => { BK.Script.log('关闭游戏'); Platform.handelOnHide(); } ) BK.onEnterBackground( () => { BK.Script.log('游戏退出到后台'); Platform.handelOnHide(); }) BK.onEnterForeground( () => { BK.Script.log('游戏回到前台'); this.handelOnShow(); }) // BK.onGameShare( () => { // BK.Script.log('分享事件,点击分享按钮触发'); // let shareInfo = { // summary: '猜猜他是谁?', // picUrl: 'https://pub.dwstatic.com/wxgame/taptapstar_qq/share/qq_share.png', // extendInfo: '' // }; // return shareInfo; // }) new BK.Game({ onShare() { BK.Script.log('分享事件,点击分享按钮触发'); let shareInfo = { summary: '猜猜他是谁?', picUrl: 'https://pub.dwstatic.com/wxgame/taptapstar_qq/share/qq_share.png', extendInfo: '' }; return shareInfo; } }); // BK.onGameShareComplete( () => { // BK.Script.log('分享结果,分享事件完成'); // }) // BK.Game.onNetworkChange( () => { // console.log('网络情况改变'); // }) // BK.game.onException( (app) => { // console.log('游戏报错'); // BK.Script.log(1, 0, "msg = " + app.errorMessage() + ", stack = " + app.errorStacktrace()); // }) } static handelTTLifeCyle() { tt.onShow((query) => { // if (typeof wx.getUpdateManager === 'function') { // const updateManager = wx.getUpdateManager(); // // updateManager.onCheckForUpdate(function (res) { // // 请求完新版本信息的回调 // console.log('hasUpdate: ' + JSON.stringify(res.hasUpdate)); // }) // // updateManager.onUpdateReady(function () { // // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 // updateManager.applyUpdate(); // }) // // updateManager.onUpdateFailed(function () { // // 新的版本下载失败 // }) // } //进入游戏的场景值 // console.log('scene ' + scene + '==============================game ==============================='); // if (scene == 1104) { // GameGlobal.isMineEnter = true; //从我的小程序进入游戏 // } else { // GameGlobal.isMineEnter = false; //从我的小程序进入游戏 // } console.log('Global onShow' + JSON.stringify(query) + '======================================'); if (query != undefined && query != null) { if (typeof query == "string") { query = JSON.parse(query); } GameGlobal.shareType = query.shareType; // if (GameGlobal.shareType == ShareAction.SHOW_GROUP_RANK) { // if (shareTicket != undefined && shareTicket != null) { // GameGlobal.shareTicket = shareTicket; // GameEvent.fire(GameNotificationKey.GameShowGroupRank);//处理通过点击分享链接进入游戏的查看群排行榜 // } else { // GameGlobal.shareTicket = ''; // } // } if (query.uid != undefined && GameGlobal.shareType == ShareAction.INVITE_FRIEND) { GameGlobal.shareUid = query.uid; GameEvent.fire(GameNotificationKey.ProcessShareAction);//处理通过点击分享链接进入游戏的各种操作,加好友..... } } //// 如果是从后台到前台 Platform.handelOnShow(); }); tt.onHide(() => { console.log('Global onHide ==================================='); GameGlobal.isOnHide = true; if (GameModule.userInfo) { GameModule.userInfo.doReport(); } // /// 关闭socket 如果socket是打开的状态 其它的状态就不管 打开之后直接重连 && Global._wxSocket.readyState == 'open' // if (Global._wxSocket != undefined && Global._wxSocket != null ) { // Global._wxSocket.close(); // } cc.sys.localStorage.setItem("onHideTimestamp", Date.parse(new Date())); HomeApi.exitGame(); if (GameModule.audioMng) { GameModule.audioMng.stopAll(); } }); tt.showShareMenu({ withShareTicket: true, success: function (res) { }, fail: function (res) { }, complete: function (res) { } }); tt.onShareAppMessage(function () { if (GameGlobal.isCheck) { var uid = -1; if (GameGlobal.user != undefined && GameGlobal.user.uid != undefined) { uid = GameGlobal.user.uid; } return { title: '猜猜他是谁?', imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_3.png', query: 'uid=' + uid + '&shareType=' + ShareAction.INVITE_FRIEND, success: function (res) { console.log('onShareAppMessage 分享成功'); // 分享成功上报 SkillApi.report(2, (responseData) => { console.log('上报分享成功'); },(error) => { }); }, fail: function (res) { // 转发失败 console.log('onShareAppMessage 取消或分享失败'); }, complete: function () { } } } else { let randomIndex = parseInt(Math.random()*(WeChat.shareArray.length),10); let shareObjct = WeChat.shareArray[randomIndex]; var uid = -1; if (GameGlobal.user != undefined && GameGlobal.user.uid != undefined) { uid = GameGlobal.user.uid; } return { title: shareObjct.title, imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon, query: 'uid=' + uid + '&shareType=' + ShareAction.INVITE_FRIEND, success: function (res) { console.log('onShareAppMessage 分享成功'); // 分享成功上报 SkillApi.report(2, (responseData) => { console.log('上报分享成功'); },(error) => { }); }, fail: function (res) { // 转发失败 console.log('onShareAppMessage 取消或分享失败'); }, complete: function () { }, }; } }); } static handelWxLifeCyle() { wx.onShow(({scene, query, shareTicket }) => { if (typeof wx.getUpdateManager === 'function') { const updateManager = wx.getUpdateManager(); updateManager.onCheckForUpdate(function (res) { // 请求完新版本信息的回调 console.log('hasUpdate: ' + JSON.stringify(res.hasUpdate)); }) updateManager.onUpdateReady(function () { // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 updateManager.applyUpdate(); }) updateManager.onUpdateFailed(function () { // 新的版本下载失败 }) } //进入游戏的场景值 console.log('scene ' + scene + '==============================game ==============================='); if (scene == 1104) { GameGlobal.isMineEnter = true; //从我的小程序进入游戏 } else { GameGlobal.isMineEnter = false; //从我的小程序进入游戏 } console.log('Global onShow' + JSON.stringify(query) + '======================================'); if (query != undefined && query != null) { if (typeof query == "string") { query = JSON.parse(query); } GameGlobal.shareType = query.shareType; if (GameGlobal.shareType == ShareAction.SHOW_GROUP_RANK) { if (shareTicket != undefined && shareTicket != null) { GameGlobal.shareTicket = shareTicket; GameEvent.fire(GameNotificationKey.GameShowGroupRank);//处理通过点击分享链接进入游戏的查看群排行榜 } else { GameGlobal.shareTicket = ''; } } if (query.uid != undefined && GameGlobal.shareType == ShareAction.INVITE_FRIEND) { GameGlobal.shareUid = query.uid; GameEvent.fire(GameNotificationKey.ProcessShareAction);//处理通过点击分享链接进入游戏的各种操作,加好友..... } } //// 如果是从后台到前台 Platform.handelOnShow(); }); wx.onHide(() => { console.log('Global onHide ==================================='); GameGlobal.isOnHide = true; if (GameModule.userInfo) { GameModule.userInfo.doReport(); } // /// 关闭socket 如果socket是打开的状态 其它的状态就不管 打开之后直接重连 && Global._wxSocket.readyState == 'open' // if (Global._wxSocket != undefined && Global._wxSocket != null ) { // Global._wxSocket.close(); // } cc.sys.localStorage.setItem("onHideTimestamp", Date.parse(new Date())); HomeApi.exitGame(); if (GameModule.audioMng) { GameModule.audioMng.stopAll(); } }); wx.showShareMenu({ withShareTicket: true, success: function (res) { }, fail: function (res) { }, complete: function (res) { } }); wx.onShareAppMessage(function (res) { // 用户点击了“转发”按钮 if (res.from === 'button') { // 来自页面内转发按钮 } else if (res.from === 'menu') { } if (GameGlobal.isCheck) { var uid = -1; if (GameGlobal.user != undefined && GameGlobal.user.uid != undefined) { uid = GameGlobal.user.uid; } return { title: '猜猜他是谁?', imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_3.png', query: 'uid=' + uid + '&shareType=' + ShareAction.INVITE_FRIEND, success: function (res) { console.log('分享成功'); // 分享成功上报 SkillApi.report(2, (responseData) => { console.log('上报分享成功'); },(error) => { }); }, fail: function (res) { // 转发失败 console.log('取消或分享失败'); }, complete: function () { } } } else { let randomIndex = parseInt(Math.random()*(WeChat.shareArray.length),10); let shareObjct = WeChat.shareArray[randomIndex]; var uid = -1; if (GameGlobal.user != undefined && GameGlobal.user.uid != undefined) { uid = GameGlobal.user.uid; } return { title: shareObjct.title, imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon, query: 'uid=' + uid + '&shareType=' + ShareAction.INVITE_FRIEND, success: function (res) { console.log('分享成功'); // 分享成功上报 SkillApi.report(2, (responseData) => { console.log('上报分享成功'); },(error) => { }); }, fail: function (res) { // 转发失败 console.log('取消或分享失败'); }, complete: function () { }, }; } }); } static handelOnShow() { if (GameGlobal.isOnHide) { GameGlobal.isOnHide = false; //客户端优先刷新技能使用情况 GameEvent.fire(GameNotificationKey.GameSkillOnHide); /// 发通知更新跟定时器相关的数据 GameEvent.fire(GameNotificationKey.GameShowNotificationKey); if (GameGlobal.clickShare) { let onHideTime = cc.sys.localStorage.getItem('onHideTimestamp'); let curTime = new Date().getTime(); let difference = curTime - onHideTime; var isShareOk = false; //判断是否忽略分享的状态 if (GameGlobal.isIgnoreShareStatus) { isShareOk = true; } else { //在分享界面停留了3秒当作分享成功,如果少于3秒即有10%概率当作成功 if (difference > 3000) { isShareOk = true; } else { let randomOk = Math.random() <= 0.1 ? true : false; if (randomOk) { isShareOk = true; } } } GameEvent.fire(GameNotificationKey.ShowShareAction, GameGlobal.gameShareType, isShareOk); GameGlobal.clickShare = false; GameGlobal.gameShareType = WechatShareType.None; GameGlobal.isIgnoreShareStatus = false; if (isShareOk) { SkillApi.report(2, (responseData) => { },(error) => { }); } else { AlertManager.showShareFailAlert(); } } } } static handelOnHide() { GameGlobal.isOnHide = true; if (GameModule.userInfo) { GameModule.userInfo.doReport(); } /// 关闭socket if (GameGlobal._wxSocket != undefined && GameGlobal._wxSocket != null) { GameGlobal._wxSocket.close(); } cc.sys.localStorage.setItem("onHideTimestamp", Date.parse(new Date())); HomeApi.exitGame(); if (GameModule.audioMng) { GameModule.audioMng.stopAll(); } } } module.exports = Platform;