Global.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. const BuildingManager = require("../utils/BuildingManager");
  2. const ShareAction = require('./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. window.Global = {
  10. // 建筑管理
  11. BuildingManager: BuildingManager.instance,
  12. debug: true,
  13. ver: 102, //游戏版本号发版时需要核对
  14. isCheck: false,
  15. isMineEnter: false,
  16. /**
  17. * 点击别人分享的链接需要进行的操作 查看群排行
  18. */
  19. shareType: ShareAction.NONE,
  20. /**
  21. * 点击别人分享的链接获取到这个用户的uid,默认-1
  22. */
  23. shareUid: -1,
  24. //SystemInfo
  25. os: 1,//1 android,2 ios
  26. channel: CC_WECHATGAME ? "weixin" : "LuciferChannel",
  27. user: null,
  28. /// 游戏是否退出到后台
  29. isOnHide: false,
  30. homeUpdate: true,
  31. wechatScoreKey: 'buildingLevel',
  32. // 开发中的城市Id
  33. devCityId: 1,
  34. buildRes: null,
  35. prefabsRes: null,
  36. starAvatarRes: null,
  37. starSpineRes: null,
  38. needLogin: false,
  39. /// 最后一个缩短技能cd的技能等级
  40. rcdSkillLevel: 0,
  41. //是否已领取当天签到奖励
  42. isSignAward: false,
  43. //已签到次数
  44. signCount: 0,
  45. //分享出去的群id
  46. shareTicket: '',
  47. //通用网络请求提示框
  48. commonAlert: AlertManager,
  49. //是否点击过分享
  50. clickShare: false,
  51. gameShareType: WechatShareType.None,
  52. //当前设备屏幕尺寸
  53. winSize: {height: 0, width: 0}
  54. };
  55. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  56. wx.onShow(({scene, query, shareTicket }) => {
  57. if (typeof wx.getUpdateManager === 'function') {
  58. const updateManager = wx.getUpdateManager();
  59. updateManager.onCheckForUpdate(function (res) {
  60. // 请求完新版本信息的回调
  61. console.log('hasUpdate: ' + JSON.stringify(res.hasUpdate));
  62. })
  63. updateManager.onUpdateReady(function () {
  64. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  65. updateManager.applyUpdate();
  66. })
  67. updateManager.onUpdateFailed(function () {
  68. // 新的版本下载失败
  69. })
  70. }
  71. //进入游戏的场景值
  72. console.log('scene ' + scene + '==============================game ===============================');
  73. if (scene == 1089) {
  74. Global.isMineEnter = true; //从我的小程序进入游戏
  75. }
  76. console.log('Global onShow' + JSON.stringify(query) + '======================================');
  77. if (query != undefined && query != null) {
  78. Global.shareType = query.shareType;
  79. if (Global.shareType == ShareAction.SHOW_GROUP_RANK) {
  80. if (shareTicket != undefined && shareTicket != null) {
  81. Global.shareTicket = shareTicket;
  82. GameEvent.fire(GameNotificationKey.GameShowGroupRank);//处理通过点击分享链接进入游戏的查看群排行榜
  83. } else {
  84. Global.shareTicket = '';
  85. }
  86. }
  87. }
  88. // debugger;
  89. // console.log(query.from, shareTicket);
  90. //// 如果是从后台到前台
  91. if (Global.isOnHide) {
  92. Global.isOnHide = false;
  93. //客户端优先刷新技能使用情况
  94. GameEvent.fire(GameNotificationKey.GameSkillOnHide);
  95. /// 发通知更新跟定时器相关的数据
  96. GameEvent.fire(GameNotificationKey.GameShowNotificationKey);
  97. if (Global.clickShare) {
  98. GameEvent.fire(GameNotificationKey.ShowShareAction, Global.gameShareType);
  99. Global.clickShare = false;
  100. Global.gameShareType = WechatShareType.None;
  101. SkillApi.report(2, (responseData) => {
  102. },(error) => {
  103. });
  104. }
  105. }
  106. if (GameModule.audioMng) {
  107. GameModule.audioMng.playBgm();
  108. }
  109. });
  110. wx.onHide(() => {
  111. console.log('Global onHide ===================================');
  112. Global.isOnHide = true;
  113. /// 关闭socket
  114. if (Global._wxSocket != undefined && Global._wxSocket != null) {
  115. Global._wxSocket.close();
  116. }
  117. cc.sys.localStorage.setItem("onHideTimestamp", Date.parse(new Date()));
  118. HomeApi.exitGame();
  119. if (GameModule.audioMng) {
  120. GameModule.audioMng.stopAll();
  121. }
  122. });
  123. wx.showShareMenu({
  124. withShareTicket: true,
  125. success: function (res) {
  126. },
  127. fail: function (res) {
  128. },
  129. complete: function (res) {
  130. }
  131. });
  132. wx.onShareAppMessage(function (res) {
  133. // 用户点击了“转发”按钮
  134. if (res.from === 'button') {
  135. // 来自页面内转发按钮
  136. } else if (res.from === 'menu') {
  137. }
  138. if (Global.isCheck) {
  139. return {
  140. title: '猜猜他是谁?',
  141. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_3.png',
  142. query: 'shareType=' + ShareAction.NONE,
  143. success: function (res) {
  144. console.log('分享成功');
  145. // 分享成功上报
  146. SkillApi.report(2, (responseData) => {
  147. console.log('上报分享成功');
  148. },(error) => {
  149. });
  150. },
  151. fail: function (res) {
  152. // 转发失败
  153. console.log('取消或分享失败');
  154. },
  155. complete: function () {
  156. }
  157. }
  158. } else {
  159. let randomIndex = parseInt(Math.random()*(WeChat.shareArray.length),10);
  160. let shareObjct = WeChat.shareArray[randomIndex];
  161. return {
  162. title: shareObjct.title,
  163. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon,
  164. query: 'shareType=' + ShareAction.NONE,
  165. success: function (res) {
  166. console.log('分享成功');
  167. // 分享成功上报
  168. SkillApi.report(2, (responseData) => {
  169. console.log('上报分享成功');
  170. },(error) => {
  171. });
  172. },
  173. fail: function (res) {
  174. // 转发失败
  175. console.log('取消或分享失败');
  176. },
  177. complete: function () {
  178. }
  179. }
  180. }
  181. });
  182. }