Commit 5506a457 authored by 李增强's avatar 李增强

m

parent 597c9cde
......@@ -5,15 +5,18 @@ import 'dart:convert';
import 'package:qm_tj/src/utils.dart';
import 'src/db.dart';
import 'src/http.dart';
import 'package:crypto/crypto.dart' as crypto;
import 'src/tj/app.dart';
import 'src/tj/page.dart';
import 'src/tj/tap.dart';
import 'src/tj/exposure.dart';
class QMTJ {
static DB _db;
static App _app;
static Page _page;
static Tap _tap;
static Exposure _exposure;
static void debug() {
Utils.debug();
......@@ -23,9 +26,14 @@ class QMTJ {
_db = DB();
_app = App(_db);
_page = Page(_db);
_tap = Tap(_db);
_exposure = Exposure(_db);
_db.init();
Future.delayed(Duration(seconds: 5)).then((value) {
uploadPageTj();
_app.upload();
_page.upload();
_tap.upload();
_exposure.upload();
});
}
......@@ -56,24 +64,8 @@ class QMTJ {
/// [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}");
// Map<String, dynamic> map = {
// "id": id,
// "source": source,
// "page_id": pageId,
// "click_id": clickId,
// "click_time": clickTime,
// "extras": extrasStr,
// };
// await _db.ready;
// log("tap", map);
// await _db.db.insert("tap_tj", map);
{String pageId, String clickId, Map<String, dynamic> extras}) async {
_tap.onTap(pageId: pageId, clickId: clickId, extras: extras);
}
/// 曝光
......@@ -81,78 +73,8 @@ class QMTJ {
/// [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}");
// Map<String, dynamic> map = {
// "id": id,
// "source": source,
// "page_id": pageId,
// "exposure_id": exposureId,
// "exposure_time": exposureTime,
// "extras": extrasStr,
// };
// await _db.ready;
// log("exposure", map);
// await _db.db.insert("exposure_tj", map);
}
// 上传页面统计
static void uploadPageTj() async {
// await _db.ready;
// List<Map> list = await _db.db.query("page_tj", limit: 100, offset: 0);
// if (list.length == 0) {
// return;
// }
// List<String> ids = [];
// 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'])
// };
// ids.add(e['id']);
// Map project = {
// "event_name": "page_scan",
// "app": "xx",
// "event_info": item
// };
// return project;
// }).toList();
// print({"project": json.encode(list)});
// }
// static Map<String, dynamic> getCommonParams() {
// return {
// "inter": "网络情况",
// "app_channel": "APP渠道",
// "app_version": "APP版本",
// "device_id": "设备ID",
// "source": "来源",
// };
}
/// 获取当前时间戳,单位:秒
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();
{String pageId, String exposureId, Map<String, dynamic> extras}) async {
_exposure.onExposure(
pageId: pageId, exposureId: exposureId, extras: extras);
}
}
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);
}
// 上传数据至接口
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.map((e) => e['id']).toList();
await _db.db.rawDelete(
"delete from exposure_tj where id in('${ids.join("','")}')");
}
if (Utils.isDebug) {
Utils.log(
"exposure_tj upload_result", {"ok": result, "length": list.length});
}
if (list.length == 100) {
upload();
}
}
}
import 'dart:convert';
import '../../src/db.dart';
import '../../src/utils.dart';
class Tap {
DB _db;
Tap(DB db) {
_db = db;
}
/// 点击
void onTap(
{String pageId, String clickId, Map<String, dynamic> extras}) async {
int clickTime = Utils.getDateNow();
String extrasStr = json.encode(extras);
String id = Utils.md5("${pageId}_${clickId}_${extrasStr}_$clickTime");
Map<String, dynamic> map = {
"id": id,
"page_id": pageId,
"click_id": clickId,
"click_time": clickTime,
"extras": extrasStr,
};
Map<String, dynamic> common = await Utils.getCommonParams();
map.addAll(common);
await _db.ready;
Utils.log("tap", map);
await _db.db.insert("tap_tj", map);
}
// 上传数据至接口
void upload() async {
await _db.ready;
List<Map> list = await _db.db.query("tap_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_click", "event_data": list}
]
});
if (result) {
List<String> ids = list.map((e) => e['id']).toList();
await _db.db
.rawDelete("delete from tap_tj where id in('${ids.join("','")}')");
}
if (Utils.isDebug) {
Utils.log("tap_tj upload_result", {"ok": result, "length": list.length});
}
if (list.length == 100) {
upload();
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment