GameLife.js 3.3 KB

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