Platform.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 (window.tt != undefined) {
  13. this.handelTTLifeCyle();
  14. }else if (CC_WECHATGAME) {
  15. this.handelWxLifeCyle();
  16. } else if (CC_QQPLAY) {
  17. this.handelQQLifeCycle();
  18. }
  19. }
  20. static handelQQLifeCycle() {
  21. BK.onMaximize( () => {
  22. BK.Script.log('游戏最小化');
  23. Platform.handelOnHide();
  24. })
  25. BK.onGameClose(() => {
  26. BK.Script.log('关闭游戏');
  27. Platform.handelOnHide();
  28. } )
  29. BK.onEnterBackground( () => {
  30. BK.Script.log('游戏退出到后台');
  31. Platform.handelOnHide();
  32. })
  33. BK.onEnterForeground( () => {
  34. BK.Script.log('游戏回到前台');
  35. this.handelOnShow();
  36. })
  37. // BK.onGameShare( () => {
  38. // BK.Script.log('分享事件,点击分享按钮触发');
  39. // let shareInfo = {
  40. // summary: '猜猜他是谁?',
  41. // picUrl: 'https://pub.dwstatic.com/wxgame/taptapstar_qq/share/qq_share.png',
  42. // extendInfo: ''
  43. // };
  44. // return shareInfo;
  45. // })
  46. new BK.Game({
  47. onShare() {
  48. BK.Script.log('分享事件,点击分享按钮触发');
  49. let shareInfo = {
  50. summary: '猜猜他是谁?',
  51. picUrl: 'https://pub.dwstatic.com/wxgame/taptapstar_qq/share/qq_share.png',
  52. extendInfo: ''
  53. };
  54. return shareInfo;
  55. }
  56. });
  57. // BK.onGameShareComplete( () => {
  58. // BK.Script.log('分享结果,分享事件完成');
  59. // })
  60. // BK.Game.onNetworkChange( () => {
  61. // console.log('网络情况改变');
  62. // })
  63. // BK.game.onException( (app) => {
  64. // console.log('游戏报错');
  65. // BK.Script.log(1, 0, "msg = " + app.errorMessage() + ", stack = " + app.errorStacktrace());
  66. // })
  67. }
  68. static handelTTLifeCyle() {
  69. tt.onShow((query) => {
  70. // if (typeof wx.getUpdateManager === 'function') {
  71. // const updateManager = wx.getUpdateManager();
  72. //
  73. // updateManager.onCheckForUpdate(function (res) {
  74. // // 请求完新版本信息的回调
  75. // console.log('hasUpdate: ' + JSON.stringify(res.hasUpdate));
  76. // })
  77. //
  78. // updateManager.onUpdateReady(function () {
  79. // // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  80. // updateManager.applyUpdate();
  81. // })
  82. //
  83. // updateManager.onUpdateFailed(function () {
  84. // // 新的版本下载失败
  85. // })
  86. // }
  87. //进入游戏的场景值
  88. // console.log('scene ' + scene + '==============================game ===============================');
  89. // if (scene == 1104) {
  90. // GameGlobal.isMineEnter = true; //从我的小程序进入游戏
  91. // } else {
  92. // GameGlobal.isMineEnter = false; //从我的小程序进入游戏
  93. // }
  94. console.log('Global onShow' + JSON.stringify(query) + '======================================');
  95. if (query != undefined && query != null) {
  96. if (typeof query == "string") {
  97. query = JSON.parse(query);
  98. }
  99. GameGlobal.shareType = query.shareType;
  100. // if (GameGlobal.shareType == ShareAction.SHOW_GROUP_RANK) {
  101. // if (shareTicket != undefined && shareTicket != null) {
  102. // GameGlobal.shareTicket = shareTicket;
  103. // GameEvent.fire(GameNotificationKey.GameShowGroupRank);//处理通过点击分享链接进入游戏的查看群排行榜
  104. // } else {
  105. // GameGlobal.shareTicket = '';
  106. // }
  107. // }
  108. if (query.uid != undefined && GameGlobal.shareType == ShareAction.INVITE_FRIEND) {
  109. GameGlobal.shareUid = query.uid;
  110. GameEvent.fire(GameNotificationKey.ProcessShareAction);//处理通过点击分享链接进入游戏的各种操作,加好友.....
  111. }
  112. }
  113. //// 如果是从后台到前台
  114. Platform.handelOnShow();
  115. });
  116. tt.onHide(() => {
  117. console.log('Global onHide ===================================');
  118. GameGlobal.isOnHide = true;
  119. if (GameModule.userInfo) {
  120. GameModule.userInfo.doReport();
  121. }
  122. // /// 关闭socket 如果socket是打开的状态 其它的状态就不管 打开之后直接重连 && Global._wxSocket.readyState == 'open'
  123. // if (Global._wxSocket != undefined && Global._wxSocket != null ) {
  124. // Global._wxSocket.close();
  125. // }
  126. cc.sys.localStorage.setItem("onHideTimestamp", Date.parse(new Date()));
  127. HomeApi.exitGame();
  128. if (GameModule.audioMng) {
  129. GameModule.audioMng.stopAll();
  130. }
  131. });
  132. tt.showShareMenu({
  133. withShareTicket: true,
  134. success: function (res) {
  135. },
  136. fail: function (res) {
  137. },
  138. complete: function (res) {
  139. }
  140. });
  141. tt.onShareAppMessage(function () {
  142. if (GameGlobal.isCheck) {
  143. var uid = -1;
  144. if (GameGlobal.user != undefined && GameGlobal.user.uid != undefined) {
  145. uid = GameGlobal.user.uid;
  146. }
  147. return {
  148. title: '猜猜他是谁?',
  149. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_3.png',
  150. query: 'uid=' + uid + '&shareType=' + ShareAction.INVITE_FRIEND,
  151. success: function (res) {
  152. console.log('onShareAppMessage 分享成功');
  153. // 分享成功上报
  154. SkillApi.report(2, (responseData) => {
  155. console.log('上报分享成功');
  156. },(error) => {
  157. });
  158. },
  159. fail: function (res) {
  160. // 转发失败
  161. console.log('onShareAppMessage 取消或分享失败');
  162. },
  163. complete: function () {
  164. }
  165. }
  166. } else {
  167. let randomIndex = parseInt(Math.random()*(WeChat.shareArray.length),10);
  168. let shareObjct = WeChat.shareArray[randomIndex];
  169. var uid = -1;
  170. if (GameGlobal.user != undefined && GameGlobal.user.uid != undefined) {
  171. uid = GameGlobal.user.uid;
  172. }
  173. return {
  174. title: shareObjct.title,
  175. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon,
  176. query: 'uid=' + uid + '&shareType=' + ShareAction.INVITE_FRIEND,
  177. success: function (res) {
  178. console.log('onShareAppMessage 分享成功');
  179. // 分享成功上报
  180. SkillApi.report(2, (responseData) => {
  181. console.log('上报分享成功');
  182. },(error) => {
  183. });
  184. },
  185. fail: function (res) {
  186. // 转发失败
  187. console.log('onShareAppMessage 取消或分享失败');
  188. },
  189. complete: function () {
  190. },
  191. };
  192. }
  193. });
  194. }
  195. static handelWxLifeCyle() {
  196. wx.onShow(({scene, query, shareTicket }) => {
  197. if (typeof wx.getUpdateManager === 'function') {
  198. const updateManager = wx.getUpdateManager();
  199. updateManager.onCheckForUpdate(function (res) {
  200. // 请求完新版本信息的回调
  201. console.log('hasUpdate: ' + JSON.stringify(res.hasUpdate));
  202. })
  203. updateManager.onUpdateReady(function () {
  204. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  205. updateManager.applyUpdate();
  206. })
  207. updateManager.onUpdateFailed(function () {
  208. // 新的版本下载失败
  209. })
  210. }
  211. //进入游戏的场景值
  212. console.log('scene ' + scene + '==============================game ===============================');
  213. if (scene == 1104) {
  214. GameGlobal.isMineEnter = true; //从我的小程序进入游戏
  215. } else {
  216. GameGlobal.isMineEnter = false; //从我的小程序进入游戏
  217. }
  218. console.log('Global onShow' + JSON.stringify(query) + '======================================');
  219. if (query != undefined && query != null) {
  220. if (typeof query == "string") {
  221. query = JSON.parse(query);
  222. }
  223. GameGlobal.shareType = query.shareType;
  224. if (GameGlobal.shareType == ShareAction.SHOW_GROUP_RANK) {
  225. if (shareTicket != undefined && shareTicket != null) {
  226. GameGlobal.shareTicket = shareTicket;
  227. GameEvent.fire(GameNotificationKey.GameShowGroupRank);//处理通过点击分享链接进入游戏的查看群排行榜
  228. } else {
  229. GameGlobal.shareTicket = '';
  230. }
  231. }
  232. if (query.uid != undefined && GameGlobal.shareType == ShareAction.INVITE_FRIEND) {
  233. GameGlobal.shareUid = query.uid;
  234. GameEvent.fire(GameNotificationKey.ProcessShareAction);//处理通过点击分享链接进入游戏的各种操作,加好友.....
  235. }
  236. }
  237. //// 如果是从后台到前台
  238. Platform.handelOnShow();
  239. });
  240. wx.onHide(() => {
  241. console.log('Global onHide ===================================');
  242. GameGlobal.isOnHide = true;
  243. if (GameModule.userInfo) {
  244. GameModule.userInfo.doReport();
  245. }
  246. // /// 关闭socket 如果socket是打开的状态 其它的状态就不管 打开之后直接重连 && Global._wxSocket.readyState == 'open'
  247. // if (Global._wxSocket != undefined && Global._wxSocket != null ) {
  248. // Global._wxSocket.close();
  249. // }
  250. cc.sys.localStorage.setItem("onHideTimestamp", Date.parse(new Date()));
  251. HomeApi.exitGame();
  252. if (GameModule.audioMng) {
  253. GameModule.audioMng.stopAll();
  254. }
  255. });
  256. wx.showShareMenu({
  257. withShareTicket: true,
  258. success: function (res) {
  259. },
  260. fail: function (res) {
  261. },
  262. complete: function (res) {
  263. }
  264. });
  265. wx.onShareAppMessage(function (res) {
  266. // 用户点击了“转发”按钮
  267. if (res.from === 'button') {
  268. // 来自页面内转发按钮
  269. } else if (res.from === 'menu') {
  270. }
  271. if (GameGlobal.isCheck) {
  272. var uid = -1;
  273. if (GameGlobal.user != undefined && GameGlobal.user.uid != undefined) {
  274. uid = GameGlobal.user.uid;
  275. }
  276. return {
  277. title: '猜猜他是谁?',
  278. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/share_3.png',
  279. query: 'uid=' + uid + '&shareType=' + ShareAction.INVITE_FRIEND,
  280. success: function (res) {
  281. console.log('分享成功');
  282. // 分享成功上报
  283. SkillApi.report(2, (responseData) => {
  284. console.log('上报分享成功');
  285. },(error) => {
  286. });
  287. },
  288. fail: function (res) {
  289. // 转发失败
  290. console.log('取消或分享失败');
  291. },
  292. complete: function () {
  293. }
  294. }
  295. } else {
  296. let randomIndex = parseInt(Math.random()*(WeChat.shareArray.length),10);
  297. let shareObjct = WeChat.shareArray[randomIndex];
  298. var uid = -1;
  299. if (GameGlobal.user != undefined && GameGlobal.user.uid != undefined) {
  300. uid = GameGlobal.user.uid;
  301. }
  302. return {
  303. title: shareObjct.title,
  304. imageUrl: 'https://pub.dwstatic.com/wxgame/taptapstar/share/' + shareObjct.icon,
  305. query: 'uid=' + uid + '&shareType=' + ShareAction.INVITE_FRIEND,
  306. success: function (res) {
  307. console.log('分享成功');
  308. // 分享成功上报
  309. SkillApi.report(2, (responseData) => {
  310. console.log('上报分享成功');
  311. },(error) => {
  312. });
  313. },
  314. fail: function (res) {
  315. // 转发失败
  316. console.log('取消或分享失败');
  317. },
  318. complete: function () {
  319. },
  320. };
  321. }
  322. });
  323. }
  324. static handelOnShow() {
  325. if (GameGlobal.isOnHide) {
  326. if (GameModule.audioMng) {
  327. GameModule.audioMng.playBgm();
  328. }
  329. GameGlobal.isOnHide = false;
  330. //客户端优先刷新技能使用情况
  331. GameEvent.fire(GameNotificationKey.GameSkillOnHide);
  332. /// 发通知更新跟定时器相关的数据
  333. GameEvent.fire(GameNotificationKey.GameShowNotificationKey);
  334. console.log('游戏 onshow ========= ' + GameGlobal.clickShare);
  335. if (GameGlobal.clickShare) {
  336. let onHideTime = cc.sys.localStorage.getItem('onHideTimestamp');
  337. let curTime = new Date().getTime();
  338. let difference = curTime - onHideTime;
  339. var isShareOk = false;
  340. if (window.tt != undefined) {
  341. isShareOk = true;
  342. } else {
  343. //判断是否忽略分享的状态
  344. if (GameGlobal.isIgnoreShareStatus) {
  345. isShareOk = true;
  346. } else {
  347. //在分享界面停留了3秒当作分享成功,如果少于3秒即有10%概率当作成功
  348. if (difference > 3000) {
  349. isShareOk = true;
  350. } else {
  351. let randomOk = Math.random() <= 0.1 ? true : false;
  352. if (randomOk) {
  353. isShareOk = true;
  354. }
  355. }
  356. }
  357. }
  358. GameEvent.fire(GameNotificationKey.ShowShareAction, GameGlobal.gameShareType, isShareOk);
  359. GameGlobal.clickShare = false;
  360. GameGlobal.gameShareType = WechatShareType.None;
  361. GameGlobal.isIgnoreShareStatus = false;
  362. if (isShareOk) {
  363. SkillApi.report(2, (responseData) => {
  364. },(error) => {
  365. });
  366. } else {
  367. if (window.tt != undefined) {
  368. } else {
  369. AlertManager.showShareFailAlert();
  370. }
  371. }
  372. }
  373. }
  374. }
  375. static handelOnHide() {
  376. console.log('游戏 onhide =========== ');
  377. GameGlobal.isOnHide = true;
  378. if (GameModule.userInfo) {
  379. GameModule.userInfo.doReport();
  380. }
  381. /// 关闭socket
  382. if (GameGlobal._wxSocket != undefined && GameGlobal._wxSocket != null) {
  383. GameGlobal._wxSocket.close();
  384. }
  385. cc.sys.localStorage.setItem("onHideTimestamp", Date.parse(new Date()));
  386. HomeApi.exitGame();
  387. if (GameModule.audioMng) {
  388. GameModule.audioMng.stopAll();
  389. }
  390. }
  391. }
  392. module.exports = Platform;