GameLife.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import lib from './library'
  2. import md5 from './md5'
  3. let _interval = 30000
  4. let _yyuid = 0
  5. let gh_id = ''
  6. let key = 'KeB357eb6b9G719R'
  7. let M = {
  8. launchOptions: function (name) {
  9. let param = wx.getLaunchOptionsSync()
  10. let verifyMd5 = M.getSign(param.query,key)
  11. // let _md5 = param['query']['_md5']
  12. _yyuid = param['query']['_yyuid']
  13. if(param['query']['scene']) {
  14. gh_id = param['query']['scene'].startsWith('gh') ? param['query']['scene'] : ''
  15. }
  16. param['query'] = JSON.stringify(param['query'])
  17. param['referrerInfo'] = JSON.stringify(param['referrerInfo'])
  18. param['name'] = name
  19. let url = `${lib.host}sdk/launchOptions?verify=${verifyMd5}`
  20. lib.post(url, param, function (objResult) {
  21. if (objResult.result) {
  22. console.debug(url + ':' + objResult.msg);
  23. } else {
  24. // lib.showErrorTip(objResult.msg);
  25. console.error(objResult.msg);
  26. }
  27. });
  28. },
  29. gameAction: function (name, action) {
  30. if (!_yyuid) {
  31. console.debug('没有yyuid,无需上报');
  32. return;
  33. }
  34. let param = { name, _yyuid, gh_id }
  35. let url = `${lib.host}sdk/${action}`
  36. param['_sign'] = M.getSign(param)
  37. lib.get(url, param, function (objResult) {
  38. if (objResult.result) {
  39. console.debug(url + ':' + objResult.msg);
  40. } else {
  41. // lib.showErrorTip(objResult.msg);
  42. console.error(objResult.msg);
  43. }
  44. })
  45. },
  46. getSign(param,key) {
  47. let str = ''
  48. for (let key in param) {
  49. str += `&${key}=${param[key]}`
  50. }
  51. str = str.substr(1);
  52. str = str ? str : new Date().getTime()
  53. str = encodeURI(str)
  54. let sign = key ? md5.hexMD5(key + str) : md5.hexMD5(str)
  55. // console.log(`str:${str}, sign:${sign}`)
  56. return sign
  57. },
  58. }
  59. export default class GameLife {
  60. constructor(name) {
  61. this.name = name
  62. this.gameEchoHandler = 0
  63. }
  64. initEvent() {
  65. let self = this
  66. if (lib.wx) {
  67. // 上报启动参数
  68. M.launchOptions(self.name);
  69. // 防止重复绑定,先解绑
  70. wx.offShow(_show)
  71. wx.offHide(_hide)
  72. // 绑定事件
  73. wx.onShow(_show)
  74. wx.onHide(_hide)
  75. }
  76. /**
  77. * 开始小游戏心跳响应
  78. */
  79. function _startEcho() {
  80. _stopEcho()
  81. if (!_yyuid) {
  82. console.debug('没有yyuid,无需上报');
  83. return;
  84. }
  85. // 没30秒心跳一次
  86. self.gameEchoHandler = setInterval(function () {
  87. M.gameAction(self.name, 'gameEcho')
  88. }, _interval)
  89. }
  90. /**
  91. * 停止小游戏心跳响应
  92. */
  93. function _stopEcho() {
  94. if (self.gameEchoHandler) {
  95. clearTimeout(self.gameEchoHandler)
  96. self.gameEchoHandler = 0
  97. }
  98. }
  99. /**
  100. * 监听小游戏回到前台的事件
  101. */
  102. function _show() {
  103. M.gameAction(self.name, 'gameStart')
  104. _startEcho()
  105. }
  106. /**
  107. * 监听小游戏隐藏到后台事件。锁屏、按 HOME 键退到桌面、显示在聊天顶部等操作会触发此事件。
  108. */
  109. function _hide() {
  110. M.gameAction(self.name, 'gameEnd')
  111. _stopEcho()
  112. }
  113. }
  114. }