qm_tj.dart 6.51 KB
Newer Older
李增强's avatar
李增强 committed
1 2 3 4 5 6 7 8 9 10
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;
李增强's avatar
m  
李增强 committed
11
  static Http _http;
李增强's avatar
李增强 committed
12 13
  static String _appStartUpId;

李增强's avatar
m  
李增强 committed
14 15
  static Future<bool> ready;

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

  static void debug() {
    isDebug = true;
  }

李增强's avatar
m  
李增强 committed
22
  static init() {
李增强's avatar
m  
李增强 committed
23
    _http = Http(scheme: "http", host: "8.135.58.206", port: 8802);
李增强's avatar
李增强 committed
24
    _appStartUpId = _md5(_getDateNow().toString());
李增强's avatar
m  
李增强 committed
25 26
    ready = new Future<bool>(() async {
      _db = new DB();
李增强's avatar
m  
李增强 committed
27
      await _db.init();
李增强's avatar
m  
李增强 committed
28
      uploadPageTj();
李增强's avatar
m  
李增强 committed
29 30
      return true;
    });
李增强's avatar
李增强 committed
31 32 33 34 35 36 37 38
  }

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

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

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

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

  /// 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
116
    int disappearTime = _getDateNow();
李增强's avatar
m  
李增强 committed
117 118 119 120 121 122 123
    Map<String, dynamic> map = {
      "id": id,
      "page_id": pageId,
      "last_page_id": lastPageId,
      "disappear_time": disappearTime,
      "extras": extrasStr,
    };
李增强's avatar
m  
李增强 committed
124 125
    await ready;
    log("pageDisappear", map);
李增强's avatar
李增强 committed
126 127 128
    await _db.db.update(
        "page_tj",
        {
李增强's avatar
m  
李增强 committed
129
          "disappear_time": disappearTime,
李增强's avatar
李增强 committed
130
        },
李增强's avatar
m  
李增强 committed
131
        where: "id = ?  and disappear_time = 0",
李增强's avatar
李增强 committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        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
147
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
148 149 150 151 152 153
      "id": id,
      "source": source,
      "page_id": pageId,
      "click_id": clickId,
      "click_time": clickTime,
      "extras": extrasStr,
李增强's avatar
m  
李增强 committed
154
    };
李增强's avatar
m  
李增强 committed
155
    await ready;
李增强's avatar
m  
李增强 committed
156
    log("tap", map);
李增强's avatar
m  
李增强 committed
157
    await _db.db.insert("tap_tj", map);
李增强's avatar
李增强 committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171
  }

  /// 曝光
  /// [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
172
    Map<String, dynamic> map = {
李增强's avatar
李增强 committed
173 174 175 176 177 178
      "id": id,
      "source": source,
      "page_id": pageId,
      "exposure_id": exposureId,
      "exposure_time": exposureTime,
      "extras": extrasStr,
李增强's avatar
m  
李增强 committed
179
    };
李增强's avatar
m  
李增强 committed
180
    await ready;
李增强's avatar
m  
李增强 committed
181 182
    log("exposure", map);
    await _db.db.insert("exposure_tj", map);
李增强's avatar
李增强 committed
183 184
  }

李增强's avatar
m  
李增强 committed
185 186 187 188 189 190
  // 上传页面统计
  static void uploadPageTj() async {
    List<Map> list = await _db.db.query("page_tj", limit: 100, offset: 0);
    if (list.length == 0) {
      return;
    }
李增强's avatar
m  
李增强 committed
191
    List<String> ids = [];
李增强's avatar
m  
李增强 committed
192 193 194 195 196 197 198 199 200 201 202 203 204
    list = list.map((e) {
      var item = {
        "app_version": '0.0.1',
        "app_channel": "APP渠道",
        "devices_id": '3A26610FFE43D5838DDE7A40BA2E3E2C',
        "inter": '4G',
        "source": e['source'],
        "page_id": e['page_id'],
        "last_page_id": e['last_page_id'],
        "come_time": e['appear_time'],
        "out_time": e['disappear_time'],
        "extras": json.decode(e['extras'])
      };
李增强's avatar
m  
李增强 committed
205
      ids.add(e['id']);
李增强's avatar
m  
李增强 committed
206 207 208 209 210 211 212 213
      Map project = {
        "event_name": "page_scan",
        "app": "xx",
        "event_info": item
      };
      return project;
    }).toList();

李增强's avatar
m  
李增强 committed
214 215
    _http.post("/collection", {"project": json.encode(list)}).then(
        (value) async {
李增强's avatar
m  
李增强 committed
216
      if (isDebug) {
李增强's avatar
m  
李增强 committed
217
        log("upload_page_tj",  Map<String,dynamic>.from(value));
李增强's avatar
m  
李增强 committed
218
      }
李增强's avatar
m  
李增强 committed
219 220
      if (value != null && value['code'] == 200) {
        int result = await _db.db
李增强's avatar
m  
李增强 committed
221
            .rawDelete("delete from page_tj where id in('${ids.join("','")}')");
李增强's avatar
m  
李增强 committed
222
        log("clear_page_tj", Map<String,dynamic>.from({"olength": list.length, "length": result}));
李增强's avatar
m  
李增强 committed
223 224

        uploadPageTj();
李增强's avatar
m  
李增强 committed
225 226 227
    });
  }

李增强's avatar
李增强 committed
228 229 230 231 232 233 234 235 236 237 238 239
  /// 获取当前时间戳,单位:秒
  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
240

李增强's avatar
m  
李增强 committed
241
  static void log(String type, Map<String, dynamic> map) {
李增强's avatar
m  
李增强 committed
242 243 244 245 246 247 248
    if (!isDebug) {
      return;
    }
    print("$type=======================s===========================$type");
    print(map);
    print("$type=======================e===========================$type");
  }
李增强's avatar
李增强 committed
249
}