123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- 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) {
- if (GameModule.audioMng) {
- GameModule.audioMng.playBgm();
- }
- GameGlobal.isOnHide = false;
- //客户端优先刷新技能使用情况
- GameEvent.fire(GameNotificationKey.GameSkillOnHide);
- /// 发通知更新跟定时器相关的数据
- GameEvent.fire(GameNotificationKey.GameShowNotificationKey);
- console.log('游戏 onshow ========= ' + GameGlobal.clickShare);
- if (GameGlobal.clickShare) {
- let onHideTime = cc.sys.localStorage.getItem('onHideTimestamp');
- let curTime = new Date().getTime();
- let difference = curTime - onHideTime;
- var isShareOk = false;
-
- if (window.tt != undefined) {
- isShareOk = true;
- } else {
- //判断是否忽略分享的状态
- 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 {
- if (window.tt != undefined) {
- } else {
- AlertManager.showShareFailAlert();
- }
- }
- }
- }
- }
- static handelOnHide() {
- console.log('游戏 onhide =========== ');
- 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;
|