|
|
@@ -0,0 +1,95 @@
|
|
|
+import 'dart:convert';
|
|
|
+
|
|
|
+import 'package:flutter/foundation.dart';
|
|
|
+import 'package:flutter_asa_attribution/flutter_asa_attribution.dart';
|
|
|
+import 'package:http/http.dart' as http;
|
|
|
+import 'package:location/utils/date_util.dart';
|
|
|
+import '../../device/atmob_platform_info.dart';
|
|
|
+import '../../utils/hash_utils.dart';
|
|
|
+
|
|
|
+class AsaHelper {
|
|
|
+ AsaHelper._();
|
|
|
+
|
|
|
+ static final String _orgId = "9038030";
|
|
|
+ static final String _secret = "86728a25f9e806dc05bca13e6bffc16d";
|
|
|
+ static final String _sign = HashUtils.md5String("$_secret&kiwi");
|
|
|
+ static Map<String, dynamic>? attributionMap;
|
|
|
+
|
|
|
+ static Future<String?> getToken() {
|
|
|
+ return FlutterAsaAttribution.instance.attributionToken();
|
|
|
+ }
|
|
|
+
|
|
|
+ static void init() {
|
|
|
+ requestAttributionDetails();
|
|
|
+ }
|
|
|
+
|
|
|
+ static Future<Map<String, dynamic>?> requestAttributionDetails() async {
|
|
|
+ if (attributionMap != null) {
|
|
|
+ return attributionMap;
|
|
|
+ }
|
|
|
+ attributionMap =
|
|
|
+ await FlutterAsaAttribution.instance.requestAttributionDetails();
|
|
|
+ return attributionMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ static Future<bool> reportEvent(AsaEvent event, {double? amount}) async {
|
|
|
+ final attrMap = await requestAttributionDetails();
|
|
|
+
|
|
|
+ final Map<String, dynamic> map = {
|
|
|
+ "campaign_id": attrMap?["campaignId"] ?? "",
|
|
|
+ "ad_group_id": attrMap?["adGroupId"] ?? "",
|
|
|
+ "keyword_id": attrMap?["keywordId"] ?? "",
|
|
|
+ "date": DateUtil.formatDateTime(DateUtil.getNow(), "yyyy-MM-dd"),
|
|
|
+ "unique_device": atmobPlatformInfo.idfa ?? atmobPlatformInfo.idfv,
|
|
|
+ "timezone": 'ORTZ',
|
|
|
+ "event_name": event.value,
|
|
|
+ };
|
|
|
+ if (amount == null) {
|
|
|
+ map.addAll({"event_val": 1});
|
|
|
+ } else {
|
|
|
+ map.addAll({"event_val": amount});
|
|
|
+ }
|
|
|
+ return reportBackendDataDetail(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ static Future<bool> reportBackendDataDetail(
|
|
|
+ Map<String, dynamic> params) async {
|
|
|
+ try {
|
|
|
+ final String url = _getReportDetailApi();
|
|
|
+ final headers = {
|
|
|
+ "Content-Type": "application/json",
|
|
|
+ };
|
|
|
+ final requestBody = {"backend_data_detail": params};
|
|
|
+ final resp = await http.post(
|
|
|
+ Uri.parse(url),
|
|
|
+ headers: headers,
|
|
|
+ body: jsonEncode(requestBody),
|
|
|
+ );
|
|
|
+
|
|
|
+ if (resp.statusCode == 200) {
|
|
|
+ final resMap = jsonDecode(resp.body);
|
|
|
+ debugPrint("asa 上报结果: $resMap");
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ debugPrint("asa 上报错误: ${resp.statusCode} ${resp.body}");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static String _getReportDetailApi() {
|
|
|
+ return 'http://asafrontend.mokiwi.com/api/asabackendDataDetail/add?org_id=$_orgId&sign=$_sign&debug=$kDebugMode';
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+enum AsaEvent {
|
|
|
+ active("active"), //激活
|
|
|
+ register("register"), //注册
|
|
|
+ pay("pay"); //付费
|
|
|
+
|
|
|
+ const AsaEvent(this.value);
|
|
|
+
|
|
|
+ final String value;
|
|
|
+}
|