import lib from './library' import md5 from './md5' let _interval = 30000 let _yyuid = 0 let gh_id = '' let M = { launchOptions: function (name) { let param = wx.getLaunchOptionsSync() // let _md5 = param['query']['_md5'] _yyuid = param['query']['_yyuid'] if(param['query']['scene']) { gh_id = param['query']['scene'].startsWith('gh') ? param['query']['scene'] : '' } param['query'] = JSON.stringify(param['query']) param['referrerInfo'] = JSON.stringify(param['referrerInfo']) param['name'] = name let url = `${lib.host}sdk/launchOptions` lib.post(url, param, function (objResult) { if (objResult.result) { console.debug(url + ':' + objResult.msg); } else { // lib.showErrorTip(objResult.msg); console.error(objResult.msg); } }); }, gameAction: function (name, action) { if (!_yyuid) { console.debug('没有yyuid,无需上报'); return; } let param = { name, _yyuid, gh_id } let url = `${lib.host}sdk/${action}` param['_sign'] = M.getSign(param) lib.get(url, param, function (objResult) { if (objResult.result) { console.debug(url + ':' + objResult.msg); } else { // lib.showErrorTip(objResult.msg); console.error(objResult.msg); } }) }, getSign(param) { let str = '' for (let key in param) { str += `&${key}=${param[key]}` } str = str.substr(1); str = encodeURI(str) let sign = md5.hexMD5(str) // console.log(`str:${str}, sign:${sign}`) return sign }, reportDw() { } } export default class GameLife { constructor(name) { this.name = name this.gameEchoHandler = 0 } initEvent() { let self = this if (lib.wx) { // 上报启动参数 M.launchOptions(self.name); // 防止重复绑定,先解绑 wx.offShow(_show) wx.offHide(_hide) // 绑定事件 wx.onShow(_show) wx.onHide(_hide) } /** * 开始小游戏心跳响应 */ function _startEcho() { _stopEcho() if (!_yyuid) { console.debug('没有yyuid,无需上报'); return; } // 没30秒心跳一次 self.gameEchoHandler = setInterval(function () { M.gameAction(self.name, 'gameEcho') }, _interval) } /** * 停止小游戏心跳响应 */ function _stopEcho() { if (self.gameEchoHandler) { clearTimeout(self.gameEchoHandler) self.gameEchoHandler = 0 } } /** * 监听小游戏回到前台的事件 */ function _show() { M.gameAction(self.name, 'gameStart') _startEcho() } /** * 监听小游戏隐藏到后台事件。锁屏、按 HOME 键退到桌面、显示在聊天顶部等操作会触发此事件。 */ function _hide() { M.gameAction(self.name, 'gameEnd') _stopEcho() } } }