- 修复真实缺陷:隐私红线 RegExp 使用 Dart 不支持的 (?i) 内联标志,构造即抛异常 导致带 props 的事件被静默丢弃;改为 caseSensitive: false - 清理埋点模块未用 import/字段/变量(4 warning) - 构造函数改用初始化形参(含 Dart 3.12 私有具名参数) - E2E 手动脚本声明 ignore avoid_print + library 指令 - 门禁:format 0 changed / analyze No issues / flutter test 34 passed
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
/// Simplified analytics client for M0 (report 13 + ticket 19): track events
|
||||
@@ -10,21 +9,18 @@ import 'package:uuid/uuid.dart';
|
||||
/// silently discarded (no retry as spec'd); privacy red-line enforced locally.
|
||||
class AnalyticsService {
|
||||
AnalyticsService({
|
||||
required String apiBaseUrl,
|
||||
required this.apiBaseUrl,
|
||||
required this.getAccessToken,
|
||||
String? anonymousId,
|
||||
String? userId,
|
||||
}) : _apiBaseUrl = apiBaseUrl,
|
||||
_anonymousId = anonymousId ?? const Uuid().v4(),
|
||||
_userId = userId;
|
||||
this._userId,
|
||||
}) : _anonymousId = anonymousId ?? const Uuid().v4();
|
||||
|
||||
static const _queueKey = 'patbond_analytics_queue';
|
||||
static const _queueMaxSize = 500;
|
||||
// M0:内存队列,满 _flushThreshold 条上传一次;持久化队列留 M1(报告 19 §3)。
|
||||
static const _flushThreshold = 20;
|
||||
|
||||
final String _apiBaseUrl;
|
||||
final String apiBaseUrl;
|
||||
final String Function()? getAccessToken;
|
||||
String _anonymousId;
|
||||
final String _anonymousId;
|
||||
String? _userId;
|
||||
final List<Map<String, dynamic>> _pendingEvents = [];
|
||||
|
||||
@@ -90,13 +86,8 @@ class AnalyticsService {
|
||||
|
||||
Future<void> _upload(List<Map<String, dynamic>> events) async {
|
||||
final token = getAccessToken?.call();
|
||||
final headers = {
|
||||
'Content-Type': 'application/json',
|
||||
if (token != null) 'Authorization': 'Bearer $token',
|
||||
};
|
||||
|
||||
final request =
|
||||
await HttpClient().postUrl(Uri.parse('$_apiBaseUrl/api/v1/events'))
|
||||
await HttpClient().postUrl(Uri.parse('$apiBaseUrl/api/v1/events'))
|
||||
..headers.contentType = ContentType.json;
|
||||
if (token != null) {
|
||||
request.headers.add('Authorization', 'Bearer $token');
|
||||
@@ -110,8 +101,10 @@ class AnalyticsService {
|
||||
}
|
||||
|
||||
bool _containsForbiddenField(Map<String, dynamic> props) {
|
||||
// Dart RegExp 不支持 (?i) 内联标志(原写法构造即抛异常,事件被静默丢弃)。
|
||||
final pattern = RegExp(
|
||||
r'(?i).*(password|token|secret|phone|mobile|email|credential|idfa|gaid).*',
|
||||
r'password|token|secret|phone|mobile|email|credential|idfa|gaid',
|
||||
caseSensitive: false,
|
||||
);
|
||||
return props.keys.any((key) => pattern.hasMatch(key));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user