123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- import lib from './library'
- import md5 from './md5'
- let _interval = 30000
- let _yyuid = 0
- let userInfo = {}
- let M = {
- launchOptions: function (name) {
- let param = wx.getLaunchOptionsSync()
- // let _md5 = param['query']['_md5']
- _yyuid = param['query']['_yyuid']
-
- 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 }
- 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) {
- // 已经授权,可以直接调用 getUserInfo 获取头像昵称
- // 上报启动参数
- 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()
- }
- }
- }
|