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 (cc.sys.platform === cc.sys.WECHAT_GAME) { 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 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 == 1089) { GameGlobal.isMineEnter = true; //从我的小程序进入游戏 } console.log('Global onShow' + JSON.stringify(query) + '======================================'); if (query != undefined && query != null) { 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 = ''; } } } // console.log(query.from, shareTicket); //// 如果是从后台到前台 Platform.handelOnShow(); }); wx.onHide(() => { console.log('Global onHide ==================================='); Platform.handelOnHide(); }); 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) { return { title: '猜猜他是谁?', imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_3.png', query: 'shareType=' + ShareAction.NONE, 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]; return { title: shareObjct.title, imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon, query: 'shareType=' + ShareAction.NONE, 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) { GameEvent.fire(GameNotificationKey.ShowShareAction, GameGlobal.gameShareType); GameGlobal.clickShare = false; GameGlobal.gameShareType = WechatShareType.None; SkillApi.report(2, (responseData) => { },(error) => { }); } } if (GameModule.audioMng) { GameModule.audioMng.playBgm(); } } 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;