GameLife.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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['openid'] = userInfo['openId']
  12. param['appid'] = userInfo['watermark'] && userInfo['watermark']['appid']
  13. param['query'] = JSON.stringify(param['query'])
  14. param['referrerInfo'] = JSON.stringify(param['referrerInfo'])
  15. param['name'] = name
  16. let url = `${lib.host}sdk/launchOptions`
  17. lib.post(url, param, function(objResult) {
  18. if (objResult.result) {
  19. console.debug(url + ':' + objResult.msg);
  20. } else {
  21. // lib.showErrorTip(objResult.msg);
  22. console.error(objResult.msg);
  23. }
  24. });
  25. },
  26. gameAction : function(name, action) {
  27. if (!_yyuid) {
  28. console.debug('没有yyuid,无需上报');
  29. return;
  30. }
  31. let param = {name, _yyuid}
  32. let url = `${lib.host}sdk/${action}`
  33. param['_sign'] = M.getSign(param)
  34. lib.get(url, param, function(objResult) {
  35. if (objResult.result) {
  36. console.debug(url + ':' + objResult.msg);
  37. } else {
  38. // lib.showErrorTip(objResult.msg);
  39. console.error(objResult.msg);
  40. }
  41. })
  42. },
  43. getSign(param) {
  44. let str = ''
  45. for (let key in param) {
  46. str += `&${key}=${param[key]}`
  47. }
  48. str = str.substr(1);
  49. str = encodeURI(str)
  50. let sign = md5.hexMD5(str)
  51. // console.log(`str:${str}, sign:${sign}`)
  52. return sign
  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. wx.getUserInfo({
  65. success: function(res) {
  66. userInfo = res.userInfo
  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. * 开始小游戏心跳响应
  80. */
  81. function _startEcho() {
  82. _stopEcho()
  83. if (!_yyuid) {
  84. console.debug('没有yyuid,无需上报');
  85. return;
  86. }
  87. // 没30秒心跳一次
  88. self.gameEchoHandler = setInterval(function() {
  89. M.gameAction(self.name, 'gameEcho')
  90. }, _interval)
  91. }
  92. /**
  93. * 停止小游戏心跳响应
  94. */
  95. function _stopEcho() {
  96. if (self.gameEchoHandler) {
  97. clearTimeout(self.gameEchoHandler)
  98. self.gameEchoHandler = 0
  99. }
  100. }
  101. /**
  102. * 监听小游戏回到前台的事件
  103. */
  104. function _show() {
  105. M.gameAction(self.name, 'gameStart')
  106. _startEcho()
  107. }
  108. /**
  109. * 监听小游戏隐藏到后台事件。锁屏、按 HOME 键退到桌面、显示在聊天顶部等操作会触发此事件。
  110. */
  111. function _hide() {
  112. M.gameAction(self.name, 'gameEnd')
  113. _stopEcho()
  114. }
  115. }
  116. }