Platform.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. const BuildingManager = require("../utils/BuildingManager");
  2. const ShareAction = require('../utils/ShareAction');
  3. const HomeApi = require("../net/HomeApi");
  4. const SkillApi = require("../net/SkillApi");
  5. const WeChat = require("../net/WeChat");
  6. const AlertManager = require('../utils/AlertManager');
  7. const {GameNotificationKey, WechatShareType } = require('../utils/GameEnum');
  8. const GameModule = require("../utils/GameModule");
  9. /// 处理
  10. class Platform {
  11. static initPlatform() {
  12. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  13. this.handelWxLifeCyle();
  14. } else if (CC_QQPLAY) {
  15. this.handelQQLifeCycle();
  16. }
  17. }
  18. static handelQQLifeCycle() {
  19. BK.onMaximize( () => {
  20. BK.Script.log('游戏最小化');
  21. Platform.handelOnHide();
  22. })
  23. BK.onGameClose(() => {
  24. BK.Script.log('关闭游戏');
  25. Platform.handelOnHide();
  26. } )
  27. BK.onEnterBackground( () => {
  28. BK.Script.log('游戏退出到后台');
  29. Platform.handelOnHide();
  30. })
  31. BK.onEnterForeground( () => {
  32. BK.Script.log('游戏回到前台');
  33. this.handelOnShow();
  34. })
  35. // BK.onGameShare( () => {
  36. // BK.Script.log('分享事件,点击分享按钮触发');
  37. // let shareInfo = {
  38. // summary: '猜猜他是谁?',
  39. // picUrl: 'https://pub.dwstatic.com/wxgame/taptapstar_qq/share/qq_share.png',
  40. // extendInfo: ''
  41. // };
  42. // return shareInfo;
  43. // })
  44. new BK.Game({
  45. onShare() {
  46. BK.Script.log('分享事件,点击分享按钮触发');
  47. let shareInfo = {
  48. summary: '猜猜他是谁?',
  49. picUrl: 'https://pub.dwstatic.com/wxgame/taptapstar_qq/share/qq_share.png',
  50. extendInfo: ''
  51. };
  52. return shareInfo;
  53. }
  54. });
  55. // BK.onGameShareComplete( () => {
  56. // BK.Script.log('分享结果,分享事件完成');
  57. // })
  58. // BK.Game.onNetworkChange( () => {
  59. // console.log('网络情况改变');
  60. // })
  61. // BK.game.onException( (app) => {
  62. // console.log('游戏报错');
  63. // BK.Script.log(1, 0, "msg = " + app.errorMessage() + ", stack = " + app.errorStacktrace());
  64. // })
  65. }
  66. static handelWxLifeCyle() {
  67. wx.onShow(({scene, query, shareTicket }) => {
  68. if (typeof wx.getUpdateManager === 'function') {
  69. const updateManager = wx.getUpdateManager();
  70. updateManager.onCheckForUpdate(function (res) {
  71. // 请求完新版本信息的回调
  72. console.log('hasUpdate: ' + JSON.stringify(res.hasUpdate));
  73. })
  74. updateManager.onUpdateReady(function () {
  75. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  76. updateManager.applyUpdate();
  77. })
  78. updateManager.onUpdateFailed(function () {
  79. // 新的版本下载失败
  80. })
  81. }
  82. //进入游戏的场景值
  83. console.log('scene ' + scene + '==============================game ===============================');
  84. if (scene == 1089) {
  85. GameGlobal.isMineEnter = true; //从我的小程序进入游戏
  86. }
  87. console.log('Global onShow' + JSON.stringify(query) + '======================================');
  88. if (query != undefined && query != null) {
  89. GameGlobal.shareType = query.shareType;
  90. if (GameGlobal.shareType == ShareAction.SHOW_GROUP_RANK) {
  91. if (shareTicket != undefined && shareTicket != null) {
  92. GameGlobal.shareTicket = shareTicket;
  93. GameEvent.fire(GameNotificationKey.GameShowGroupRank);//处理通过点击分享链接进入游戏的查看群排行榜
  94. } else {
  95. GameGlobal.shareTicket = '';
  96. }
  97. }
  98. }
  99. // console.log(query.from, shareTicket);
  100. //// 如果是从后台到前台
  101. Platform.handelOnShow();
  102. });
  103. wx.onHide(() => {
  104. console.log('Global onHide ===================================');
  105. Platform.handelOnHide();
  106. });
  107. wx.showShareMenu({
  108. withShareTicket: true,
  109. success: function (res) {
  110. },
  111. fail: function (res) {
  112. },
  113. complete: function (res) {
  114. }
  115. });
  116. wx.onShareAppMessage(function (res) {
  117. // 用户点击了“转发”按钮
  118. if (res.from === 'button') {
  119. // 来自页面内转发按钮
  120. } else if (res.from === 'menu') {
  121. }
  122. if (GameGlobal.isCheck) {
  123. return {
  124. title: '猜猜他是谁?',
  125. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_3.png',
  126. query: 'shareType=' + ShareAction.NONE,
  127. success: function (res) {
  128. console.log('分享成功');
  129. // 分享成功上报
  130. SkillApi.report(2, (responseData) => {
  131. console.log('上报分享成功');
  132. },(error) => {
  133. });
  134. },
  135. fail: function (res) {
  136. // 转发失败
  137. console.log('取消或分享失败');
  138. },
  139. complete: function () {
  140. }
  141. }
  142. } else {
  143. let randomIndex = parseInt(Math.random()*(WeChat.shareArray.length),10);
  144. let shareObjct = WeChat.shareArray[randomIndex];
  145. return {
  146. title: shareObjct.title,
  147. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon,
  148. query: 'shareType=' + ShareAction.NONE,
  149. success: function (res) {
  150. console.log('分享成功');
  151. // 分享成功上报
  152. SkillApi.report(2, (responseData) => {
  153. console.log('上报分享成功');
  154. },(error) => {
  155. });
  156. },
  157. fail: function (res) {
  158. // 转发失败
  159. console.log('取消或分享失败');
  160. },
  161. complete: function () {
  162. }
  163. }
  164. }
  165. });
  166. }
  167. static handelOnShow() {
  168. if (GameGlobal.isOnHide) {
  169. GameGlobal.isOnHide = false;
  170. //客户端优先刷新技能使用情况
  171. GameEvent.fire(GameNotificationKey.GameSkillOnHide);
  172. /// 发通知更新跟定时器相关的数据
  173. GameEvent.fire(GameNotificationKey.GameShowNotificationKey);
  174. if (GameGlobal.clickShare) {
  175. GameEvent.fire(GameNotificationKey.ShowShareAction, GameGlobal.gameShareType);
  176. GameGlobal.clickShare = false;
  177. GameGlobal.gameShareType = WechatShareType.None;
  178. SkillApi.report(2, (responseData) => {
  179. },(error) => {
  180. });
  181. }
  182. }
  183. if (GameModule.audioMng) {
  184. GameModule.audioMng.playBgm();
  185. }
  186. }
  187. static handelOnHide() {
  188. GameGlobal.isOnHide = true;
  189. if (GameModule.userInfo) {
  190. GameModule.userInfo.doReport();
  191. }
  192. /// 关闭socket
  193. if (GameGlobal._wxSocket != undefined && GameGlobal._wxSocket != null) {
  194. GameGlobal._wxSocket.close();
  195. }
  196. cc.sys.localStorage.setItem("onHideTimestamp", Date.parse(new Date()));
  197. HomeApi.exitGame();
  198. if (GameModule.audioMng) {
  199. GameModule.audioMng.stopAll();
  200. }
  201. }
  202. }
  203. module.exports = Platform;