1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import 'dart:convert';
import '../db.dart';
import '../utils.dart';
class Exposure {
DB _db;
Exposure(DB db) {
_db = db;
}
/// 曝光
void onExposure(
{String pageId, String exposureId, Map<String, dynamic> extras}) async {
int exposureTime = Utils.getDateNow();
String extrasStr = json.encode(extras);
String id = Utils.md5("${pageId}_${exposureId}_${extrasStr}_$exposureTime");
Map<String, dynamic> map = {
"id": id,
"page_id": pageId,
"exposure_id": exposureId,
"exposure_time": exposureTime,
"extras": extrasStr,
};
await _db.ready;
Utils.log("exposure", map);
await _db.db.insert("exposure_tj", map);
}
// 上传数据至接口
Future<void> upload() async {
await _db.ready;
List<Map> list = await _db.db.query("exposure_tj", limit: 100, offset: 0);
if (list.length == 0) {
return;
}
String deviceId = await Utils.getDeviceId();
bool result = await Utils.uploadData({
"device_id": deviceId,
"projects": [
{"event_name": "elem_exposure", "event_data": list}
]
});
if (result) {
List<String> ids = List<String>.from(list.map((e) => e['id']).toList());
await _db.db.rawDelete(
"delete from exposure_tj where id in('${ids.join("','")}')");
}
if (Utils.isDebug) {
Utils.log("exposure upload_result", {"ok": result, "length": list.length});
}
if (list.length == 100) {
await upload();
}
}
}