Global.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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: 114, //游戏版本号发版时需要核对
  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. appletAward: false,
  45. //判断是否使用本地数据
  46. localData: false,
  47. //已签到次数
  48. signCount: 0,
  49. //分享出去的群id
  50. shareTicket: '',
  51. //通用网络请求提示框
  52. commonAlert: AlertManager,
  53. //是否点击过分享
  54. clickShare: false,
  55. gameShareType: WechatShareType.None,
  56. //当前设备屏幕尺寸
  57. winSize: {height: 0, width: 0}
  58. };
  59. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  60. wx.onShow(({scene, query, shareTicket }) => {
  61. if (typeof wx.getUpdateManager === 'function') {
  62. const updateManager = wx.getUpdateManager();
  63. updateManager.onCheckForUpdate(function (res) {
  64. // 请求完新版本信息的回调
  65. console.log('hasUpdate: ' + JSON.stringify(res.hasUpdate));
  66. })
  67. updateManager.onUpdateReady(function () {
  68. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  69. updateManager.applyUpdate();
  70. })
  71. updateManager.onUpdateFailed(function () {
  72. // 新的版本下载失败
  73. })
  74. }
  75. //进入游戏的场景值
  76. console.log('scene ' + scene + '==============================game ===============================');
  77. if (scene == 1104) {
  78. Global.isMineEnter = true; //从我的小程序进入游戏
  79. } else {
  80. Global.isMineEnter = false; //从我的小程序进入游戏
  81. }
  82. console.log('Global onShow' + JSON.stringify(query) + '======================================');
  83. if (query != undefined && query != null) {
  84. if (typeof query == "string") {
  85. query = JSON.parse(query);
  86. }
  87. Global.shareType = query.shareType;
  88. if (Global.shareType == ShareAction.SHOW_GROUP_RANK) {
  89. if (shareTicket != undefined && shareTicket != null) {
  90. Global.shareTicket = shareTicket;
  91. GameEvent.fire(GameNotificationKey.GameShowGroupRank);//处理通过点击分享链接进入游戏的查看群排行榜
  92. } else {
  93. Global.shareTicket = '';
  94. }
  95. }
  96. if (query.uid != undefined && Global.shareType == ShareAction.INVITE_FRIEND) {
  97. Global.shareUid = query.uid;
  98. GameEvent.fire(GameNotificationKey.ProcessShareAction);//处理通过点击分享链接进入游戏的各种操作,加好友.....
  99. }
  100. }
  101. // console.log(query.from, shareTicket);
  102. //// 如果是从后台到前台
  103. if (Global.isOnHide) {
  104. Global.isOnHide = false;
  105. //客户端优先刷新技能使用情况
  106. GameEvent.fire(GameNotificationKey.GameSkillOnHide);
  107. /// 发通知更新跟定时器相关的数据
  108. GameEvent.fire(GameNotificationKey.GameShowNotificationKey);
  109. if (Global.clickShare) {
  110. GameEvent.fire(GameNotificationKey.ShowShareAction, Global.gameShareType);
  111. Global.clickShare = false;
  112. Global.gameShareType = WechatShareType.None;
  113. SkillApi.report(2, (responseData) => {
  114. },(error) => {
  115. });
  116. }
  117. }
  118. if (GameModule.audioMng) {
  119. GameModule.audioMng.playBgm();
  120. }
  121. });
  122. wx.onHide(() => {
  123. console.log('Global onHide ===================================');
  124. Global.isOnHide = true;
  125. if (GameModule.userInfo) {
  126. GameModule.userInfo.doReport();
  127. }
  128. // /// 关闭socket 如果socket是打开的状态 其它的状态就不管 打开之后直接重连 && Global._wxSocket.readyState == 'open'
  129. // if (Global._wxSocket != undefined && Global._wxSocket != null ) {
  130. // Global._wxSocket.close();
  131. // }
  132. cc.sys.localStorage.setItem("onHideTimestamp", Date.parse(new Date()));
  133. HomeApi.exitGame();
  134. if (GameModule.audioMng) {
  135. GameModule.audioMng.stopAll();
  136. }
  137. });
  138. wx.showShareMenu({
  139. withShareTicket: true,
  140. success: function (res) {
  141. },
  142. fail: function (res) {
  143. },
  144. complete: function (res) {
  145. }
  146. });
  147. wx.onShareAppMessage(function (res) {
  148. // 用户点击了“转发”按钮
  149. if (res.from === 'button') {
  150. // 来自页面内转发按钮
  151. } else if (res.from === 'menu') {
  152. }
  153. if (Global.isCheck) {
  154. var uid = -1;
  155. if (Global.user != undefined && Global.user.uid != undefined) {
  156. uid = Global.user.uid;
  157. }
  158. return {
  159. title: '猜猜他是谁?',
  160. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_3.png',
  161. query: 'uid=' + uid + '&shareType=' + ShareAction.INVITE_FRIEND,
  162. success: function (res) {
  163. console.log('分享成功');
  164. // 分享成功上报
  165. SkillApi.report(2, (responseData) => {
  166. console.log('上报分享成功');
  167. },(error) => {
  168. });
  169. },
  170. fail: function (res) {
  171. // 转发失败
  172. console.log('取消或分享失败');
  173. },
  174. complete: function () {
  175. }
  176. }
  177. } else {
  178. let randomIndex = parseInt(Math.random()*(WeChat.shareArray.length),10);
  179. let shareObjct = WeChat.shareArray[randomIndex];
  180. var uid = -1;
  181. if (Global.user != undefined && Global.user.uid != undefined) {
  182. uid = Global.user.uid;
  183. }
  184. return {
  185. title: shareObjct.title,
  186. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon,
  187. query: 'uid=' + uid + '&shareType=' + ShareAction.INVITE_FRIEND,
  188. success: function (res) {
  189. console.log('分享成功');
  190. // 分享成功上报
  191. SkillApi.report(2, (responseData) => {
  192. console.log('上报分享成功');
  193. },(error) => {
  194. });
  195. },
  196. fail: function (res) {
  197. // 转发失败
  198. console.log('取消或分享失败');
  199. },
  200. complete: function () {
  201. }
  202. }
  203. }
  204. });
  205. }