qm_tj.dart 4.91 KB
Newer Older
李增强's avatar
李增强 committed
1 2 3 4 5 6 7 8 9 10 11 12
library qm_tj;

import 'dart:convert';

import 'src/db.dart';
import 'src/http.dart';
import 'package:crypto/crypto.dart' as crypto;

class QMTJ {
  static DB _db;
  static String _appStartUpId;

李增强's avatar
m  
李增强 committed
13 14 15 16 17 18
  static bool isDebug = false;

  static void debug() {
    isDebug = true;
  }

李增强's avatar
李增强 committed
19 20 21 22 23 24 25 26 27 28 29 30
  static void init() async {
    _appStartUpId = _md5(_getDateNow().toString());
    _db = new DB();
    await _db.init();
  }

  /// APP 启动
  static void appStart({int source, Map<String, dynamic> extras}) async {
    int type = 1;
    int disappearTime = 0;
    int appearTime = _getDateNow();
    String id = _appStartUpId;
李增强's avatar
m  
李增强 committed
31
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
32 33 34 35 36 37
      "id": id,
      "type": type,
      "source": source,
      "disappear_time": disappearTime,
      "appear_time": appearTime,
      "extras": json.encode(extras),
李增强's avatar
m  
李增强 committed
38 39 40
    };
    log("appStart", map);
    await _db.db.insert("app_tj", map);
李增强's avatar
李增强 committed
41 42 43 44 45 46 47 48 49 50 51
  }

  /// APP 可见
  static void appAppear({int source, Map<String, dynamic> extras}) async {
    // APP进入前台,重新生成启动id
    _appStartUpId = _md5(_getDateNow().toString());

    int type = 2;
    int disappearTime = 0;
    int appearTime = _getDateNow();
    String id = _appStartUpId;
李增强's avatar
m  
李增强 committed
52
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
53 54 55 56 57 58
      "id": id,
      "type": type,
      "source": source,
      "disappear_time": disappearTime,
      "appear_time": appearTime,
      "extras": json.encode(extras),
李增强's avatar
m  
李增强 committed
59 60 61
    };
    log("appAppear", map);
    await _db.db.insert("app_tj", map);
李增强's avatar
李增强 committed
62 63 64 65
  }

  /// APP 不可见
  static void appDisappear() {
李增强's avatar
m  
李增强 committed
66 67
    int disappearTime = _getDateNow();
    _db.db.update("app_tj", {"disappear_time": disappearTime},
李增强's avatar
m  
李增强 committed
68
        where: "id = ? and disappear_time = 0", whereArgs: [_appStartUpId]);
李增强's avatar
m  
李增强 committed
69 70 71 72 73
    Map<String, dynamic> map = {
      "id": _appStartUpId,
      "disappear_time": disappearTime,
    };
    log("appDisappear", map);
李增强's avatar
李增强 committed
74 75 76 77 78 79 80 81 82 83 84 85
  }

  /// PAGE 可见
  static void pageAppear(
      {int source,
      String pageId,
      String lastPageId,
      Map<String, dynamic> extras}) async {
    String extrasStr = json.encode(extras);
    String id = _md5("${pageId}_${lastPageId}_$extrasStr");
    int disappearTime = 0;
    int appearTime = _getDateNow();
李增强's avatar
m  
李增强 committed
86
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
87 88 89 90 91 92 93
      "id": id,
      "source": source,
      "page_id": pageId,
      "last_page_id": lastPageId,
      "appear_time": appearTime,
      "disappear_time": disappearTime,
      "extras": extrasStr,
李增强's avatar
m  
李增强 committed
94 95 96
    };
    log("pageAppear", map);
    await _db.db.insert("page_tj", map);
李增强's avatar
李增强 committed
97 98 99 100 101 102 103
  }

  /// PAGE 不可见
  static void pageDisappear(
      {String pageId, String lastPageId, Map<String, dynamic> extras}) async {
    String extrasStr = json.encode(extras);
    String id = _md5("${pageId}_${lastPageId}_$extrasStr");
李增强's avatar
m  
李增强 committed
104
    int disappearTime = _getDateNow();
李增强's avatar
李增强 committed
105 106 107
    await _db.db.update(
        "page_tj",
        {
李增强's avatar
m  
李增强 committed
108
          "disappear_time": disappearTime,
李增强's avatar
李增强 committed
109
        },
李增强's avatar
m  
李增强 committed
110
        where: "id = ?  and disappear_time = 0",
李增强's avatar
李增强 committed
111
        whereArgs: [id]);
李增强's avatar
m  
李增强 committed
112 113 114 115 116 117 118 119
    Map<String, dynamic> map = {
      "id": id,
      "page_id": pageId,
      "last_page_id": lastPageId,
      "disappear_time": disappearTime,
      "extras": extrasStr,
    };
    log("pageDisappear", map);
李增强's avatar
李增强 committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133
  }

  /// 点击
  /// [pageId] 页面id
  /// [clickId] 点击id
  /// [extras] 点击参数
  static void tap(
      {int source,
      String pageId,
      String clickId,
      Map<String, dynamic> extras}) async {
    int clickTime = _getDateNow();
    String extrasStr = json.encode(extras);
    String id = _md5("${pageId}_${clickId}_${extrasStr}_${clickTime}");
李增强's avatar
m  
李增强 committed
134
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
135 136 137 138 139 140
      "id": id,
      "source": source,
      "page_id": pageId,
      "click_id": clickId,
      "click_time": clickTime,
      "extras": extrasStr,
李增强's avatar
m  
李增强 committed
141 142 143
    };
    log("tap", map);
    await _db.db.insert("tap_tj", map);
李增强's avatar
李增强 committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157
  }

  /// 曝光
  /// [pageId] 页面id
  /// [exposureId] 曝光id
  /// [extras] 曝光参数
  static void exposure(
      {int source,
      String pageId,
      String exposureId,
      Map<String, dynamic> extras}) async {
    int exposureTime = _getDateNow();
    String extrasStr = json.encode(extras);
    String id = _md5("${pageId}_${exposureId}_${extrasStr}_${exposureTime}");
李增强's avatar
m  
李增强 committed
158
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
159 160 161 162 163 164
      "id": id,
      "source": source,
      "page_id": pageId,
      "exposure_id": exposureId,
      "exposure_time": exposureTime,
      "extras": extrasStr,
李增强's avatar
m  
李增强 committed
165 166 167
    };
    log("exposure", map);
    await _db.db.insert("exposure_tj", map);
李增强's avatar
李增强 committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181
  }

  /// 获取当前时间戳,单位:秒
  static int _getDateNow() {
    int now = (DateTime.now().millisecondsSinceEpoch / 1000).floor();
    return now;
  }

  // md5编码
  static String _md5(String data) {
    var bytes = utf8.encode(data);
    var digest = crypto.md5.convert(bytes);
    return digest.toString();
  }
李增强's avatar
m  
李增强 committed
182 183 184 185 186 187 188 189 190

  static void log(String type, Map<String, dynamic> map) {
    if (!isDebug) {
      return;
    }
    print("$type=======================s===========================$type");
    print(map);
    print("$type=======================e===========================$type");
  }
李增强's avatar
李增强 committed
191
}