GameLife.js 3.4 KB

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