qm_tj.dart 5.48 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
  static Future<bool> ready;

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

  static void debug() {
    isDebug = true;
  }

李增强's avatar
m  
李增强 committed
21
  static init() {
李增强's avatar
李增强 committed
22
    _appStartUpId = _md5(_getDateNow().toString());
李增强's avatar
m  
李增强 committed
23 24
    ready = new Future<bool>(() async {
      _db = new DB();
李增强's avatar
m  
李增强 committed
25 26
      await _db.init();
      List<Map> list = await _db.db.query("page_tj");
李增强's avatar
m  
李增强 committed
27 28
      print("+++++++++++++++++++++++++++++${list.length}");
      list.removeAt(0);
李增强's avatar
m  
李增强 committed
29 30 31
      List<String> ids = list.map((e) => e['id']);
      List<Map> list2 = await _db.db
          .query("page_tj", where: "id in(?)", whereArgs: [ids.join(",")]);
李增强's avatar
m  
李增强 committed
32
      print("+++++++++++++++++++++++++++++${list2.length}");
李增强's avatar
m  
李增强 committed
33 34
      return true;
    });
李增强's avatar
李增强 committed
35 36 37 38 39 40 41 42
  }

  /// 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
43
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
44 45 46 47 48 49
      "id": id,
      "type": type,
      "source": source,
      "disappear_time": disappearTime,
      "appear_time": appearTime,
      "extras": json.encode(extras),
李增强's avatar
m  
李增强 committed
50
    };
李增强's avatar
m  
李增强 committed
51
    await ready;
李增强's avatar
m  
李增强 committed
52 53
    log("appStart", map);
    await _db.db.insert("app_tj", map);
李增强's avatar
李增强 committed
54 55 56 57 58 59 60 61 62 63 64
  }

  /// 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
65
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
66 67 68 69 70 71
      "id": id,
      "type": type,
      "source": source,
      "disappear_time": disappearTime,
      "appear_time": appearTime,
      "extras": json.encode(extras),
李增强's avatar
m  
李增强 committed
72
    };
李增强's avatar
m  
李增强 committed
73
    await ready;
李增强's avatar
m  
李增强 committed
74 75
    log("appAppear", map);
    await _db.db.insert("app_tj", map);
李增强's avatar
李增强 committed
76 77 78
  }

  /// APP 不可见
李增强's avatar
m  
李增强 committed
79
  static void appDisappear() async {
李增强's avatar
m  
李增强 committed
80
    int disappearTime = _getDateNow();
李增强's avatar
m  
李增强 committed
81
    Map<String, dynamic> map = {
李增强's avatar
m  
李增强 committed
82 83 84
      "id": _appStartUpId,
      "disappear_time": disappearTime,
    };
李增强's avatar
m  
李增强 committed
85
    await ready;
李增强's avatar
m  
李增强 committed
86
    log("appDisappear", map);
李增强's avatar
m  
李增强 committed
87 88
    _db.db.update("app_tj", {"disappear_time": disappearTime},
        where: "id = ? and disappear_time = 0", whereArgs: [_appStartUpId]);
李增强's avatar
李增强 committed
89 90 91 92 93 94 95 96 97 98 99 100
  }

  /// 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
101
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
102 103 104 105 106 107 108
      "id": id,
      "source": source,
      "page_id": pageId,
      "last_page_id": lastPageId,
      "appear_time": appearTime,
      "disappear_time": disappearTime,
      "extras": extrasStr,
李增强's avatar
m  
李增强 committed
109
    };
李增强's avatar
m  
李增强 committed
110
    await ready;
李增强's avatar
m  
李增强 committed
111
    log("pageAppear", map);
李增强's avatar
m  
李增强 committed
112
    await _db.db.insert("page_tj", map);
李增强's avatar
李增强 committed
113 114 115 116 117 118 119
  }

  /// 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
120
    int disappearTime = _getDateNow();
李增强's avatar
m  
李增强 committed
121 122 123 124 125 126 127
    Map<String, dynamic> map = {
      "id": id,
      "page_id": pageId,
      "last_page_id": lastPageId,
      "disappear_time": disappearTime,
      "extras": extrasStr,
    };
李增强's avatar
m  
李增强 committed
128 129
    await ready;
    log("pageDisappear", map);
李增强's avatar
李增强 committed
130 131 132
    await _db.db.update(
        "page_tj",
        {
李增强's avatar
m  
李增强 committed
133
          "disappear_time": disappearTime,
李增强's avatar
李增强 committed
134
        },
李增强's avatar
m  
李增强 committed
135
        where: "id = ?  and disappear_time = 0",
李增强's avatar
李增强 committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        whereArgs: [id]);
  }

  /// 点击
  /// [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
151
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
152 153 154 155 156 157
      "id": id,
      "source": source,
      "page_id": pageId,
      "click_id": clickId,
      "click_time": clickTime,
      "extras": extrasStr,
李增强's avatar
m  
李增强 committed
158
    };
李增强's avatar
m  
李增强 committed
159
    await ready;
李增强's avatar
m  
李增强 committed
160
    log("tap", map);
李增强's avatar
m  
李增强 committed
161
    await _db.db.insert("tap_tj", map);
李增强's avatar
李增强 committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175
  }

  /// 曝光
  /// [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
176
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
177 178 179 180 181 182
      "id": id,
      "source": source,
      "page_id": pageId,
      "exposure_id": exposureId,
      "exposure_time": exposureTime,
      "extras": extrasStr,
李增强's avatar
m  
李增强 committed
183
    };
李增强's avatar
m  
李增强 committed
184
    await ready;
李增强's avatar
m  
李增强 committed
185 186
    log("exposure", map);
    await _db.db.insert("exposure_tj", map);
李增强's avatar
李增强 committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200
  }

  /// 获取当前时间戳,单位:秒
  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
201

李增强's avatar
m  
李增强 committed
202
  static void log(String type, Map<String, dynamic> map) {
李增强's avatar
m  
李增强 committed
203 204 205 206 207 208 209
    if (!isDebug) {
      return;
    }
    print("$type=======================s===========================$type");
    print(map);
    print("$type=======================e===========================$type");
  }
李增强's avatar
李增强 committed
210
}