优化:日志输出
This commit is contained in:
parent
c6449734f9
commit
83c1c56c36
@ -1,4 +1,5 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:encrypt/encrypt.dart';
|
import 'package:encrypt/encrypt.dart';
|
||||||
|
|
||||||
@ -28,18 +29,19 @@ class ApiCrypto {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 生成随机 Base64 字符串(用于噪音字段)
|
/// 生成随机 Base64 字符串(用于噪音字段)
|
||||||
static String randomBase64([int byteLength = 16]) {
|
static String randomBase64() {
|
||||||
final bytes = List<int>.generate(
|
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||||
byteLength, (_) => DateTime.now().millisecondsSinceEpoch % 256);
|
final random = Random();
|
||||||
return base64Encode(bytes);
|
final length = 4 + random.nextInt(5); // 4-8位
|
||||||
|
final randomStr = List.generate(length, (_) => chars[random.nextInt(chars.length)]).join();
|
||||||
|
return base64Encode(utf8.encode(randomStr));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 生成 8 位随机字母数字
|
/// 生成 8 位随机字母数字
|
||||||
static String randomAlnum() {
|
static String randomAlnum() {
|
||||||
const chars =
|
const chars =
|
||||||
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||||
return List.generate(8,
|
final random = Random();
|
||||||
(_) => chars[DateTime.now().microsecondsSinceEpoch % chars.length])
|
return List.generate(8, (_) => chars[random.nextInt(chars.length)]).join();
|
||||||
.join();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -135,7 +135,7 @@ class ProxyClient {
|
|||||||
final v2BodyEncoded = jsonEncode(v2Body);
|
final v2BodyEncoded = jsonEncode(v2Body);
|
||||||
|
|
||||||
final logStr =
|
final logStr =
|
||||||
'========== 原始入参 ===========\npath: $path\nmethod: $method\nqueryParams: $paramsEncoded\nbody(sanctum): ${jsonEncode(sanctum)}';
|
'========== 原始入参 ===========\npath: $path\nmethod: $method \nheaders: $headersEncoded\nqueryParams: $paramsEncoded\nbody(sanctum): ${jsonEncode(sanctum)}';
|
||||||
logWithEmbeddedJson(logStr);
|
logWithEmbeddedJson(logStr);
|
||||||
|
|
||||||
final proxyBody = <String, dynamic>{
|
final proxyBody = <String, dynamic>{
|
||||||
@ -153,6 +153,7 @@ class ProxyClient {
|
|||||||
|
|
||||||
final url = '$_baseUrl${config.proxyPath}';
|
final url = '$_baseUrl${config.proxyPath}';
|
||||||
|
|
||||||
|
logWithEmbeddedJson('========== 请求信息 ===========\n\n目标URL: $url');
|
||||||
logWithEmbeddedJson('========== 实际请求体 ===========\n${jsonEncode(proxyBody)}');
|
logWithEmbeddedJson('========== 实际请求体 ===========\n${jsonEncode(proxyBody)}');
|
||||||
final response = await http.post(
|
final response = await http.post(
|
||||||
Uri.parse(url),
|
Uri.parse(url),
|
||||||
|
|||||||
@ -54,6 +54,7 @@ abstract class AttributionCallbacks {
|
|||||||
Future<String?> getReferrer();
|
Future<String?> getReferrer();
|
||||||
Future<String?> getAdjustReferrer();
|
Future<String?> getAdjustReferrer();
|
||||||
Future<String?> getPlatformReferrer();
|
Future<String?> getPlatformReferrer();
|
||||||
|
Future<String?> getFacebookReferrer();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 默认归因实现
|
/// 默认归因实现
|
||||||
@ -66,6 +67,9 @@ class DefaultAttributionCallbacks implements AttributionCallbacks {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String?> getPlatformReferrer() async => null;
|
Future<String?> getPlatformReferrer() async => null;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<String?> getFacebookReferrer() async => null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 归因服务
|
/// 归因服务
|
||||||
@ -87,4 +91,6 @@ abstract class AttributionService {
|
|||||||
static Future<String?> getAdjustReferrer() => callbacks.getAdjustReferrer();
|
static Future<String?> getAdjustReferrer() => callbacks.getAdjustReferrer();
|
||||||
static Future<String?> getPlatformReferrer() =>
|
static Future<String?> getPlatformReferrer() =>
|
||||||
callbacks.getPlatformReferrer();
|
callbacks.getPlatformReferrer();
|
||||||
|
static Future<String?> getFacebookReferrer() =>
|
||||||
|
callbacks.getFacebookReferrer();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -50,26 +50,48 @@ void _logLong(String text) {
|
|||||||
_proxyLog.d(text);
|
_proxyLog.d(text);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final buffer = StringBuffer();
|
final buffer = StringBuffer();
|
||||||
int chunkIndex = 0;
|
var chunkIndex = 0;
|
||||||
|
|
||||||
|
void emitBuffer() {
|
||||||
|
if (buffer.isEmpty) return;
|
||||||
|
chunkIndex++;
|
||||||
|
_proxyLog.d(
|
||||||
|
chunkIndex > 1 ? '(part $chunkIndex)\n$buffer' : buffer.toString(),
|
||||||
|
);
|
||||||
|
buffer.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 单条 print / logcat 往往有长度上限;超长单行必须再切分,否则会截断在任意字符处(例如字符串中间的 `"`)。
|
||||||
|
void emitLineInChunks(String line) {
|
||||||
|
for (var offset = 0; offset < line.length; offset += _maxLogChunk) {
|
||||||
|
final end = (offset + _maxLogChunk <= line.length)
|
||||||
|
? offset + _maxLogChunk
|
||||||
|
: line.length;
|
||||||
|
chunkIndex++;
|
||||||
|
final piece = line.substring(offset, end);
|
||||||
|
_proxyLog.d(chunkIndex > 1 ? '(part $chunkIndex)\n$piece' : piece);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (final line in lines) {
|
for (final line in lines) {
|
||||||
|
if (line.length > _maxLogChunk) {
|
||||||
|
emitBuffer();
|
||||||
|
emitLineInChunks(line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
final lineWithNewline = buffer.isEmpty ? line : '\n$line';
|
final lineWithNewline = buffer.isEmpty ? line : '\n$line';
|
||||||
if (buffer.length + lineWithNewline.length > _maxLogChunk &&
|
if (buffer.length + lineWithNewline.length > _maxLogChunk &&
|
||||||
buffer.isNotEmpty) {
|
buffer.isNotEmpty) {
|
||||||
chunkIndex++;
|
emitBuffer();
|
||||||
_proxyLog.d('(part $chunkIndex)\n$buffer');
|
|
||||||
buffer.clear();
|
|
||||||
buffer.write(line);
|
buffer.write(line);
|
||||||
} else {
|
} else {
|
||||||
if (buffer.isNotEmpty) buffer.write('\n');
|
if (buffer.isNotEmpty) buffer.write('\n');
|
||||||
buffer.write(line);
|
buffer.write(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (buffer.isNotEmpty) {
|
emitBuffer();
|
||||||
chunkIndex++;
|
|
||||||
_proxyLog
|
|
||||||
.d(chunkIndex > 1 ? '(part $chunkIndex)\n$buffer' : buffer.toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 格式化输出嵌入在字符串中的 JSON,保持缩进对齐。
|
/// 格式化输出嵌入在字符串中的 JSON,保持缩进对齐。
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import 'dart:async';
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import '../api/api_client.dart';
|
import '../api/api_client.dart';
|
||||||
import '../api/proxy_client.dart';
|
import '../api/proxy_client.dart';
|
||||||
@ -90,6 +91,23 @@ abstract class FrameworkAuthService {
|
|||||||
debugPrint('[AuthService] start: referer=$referer');
|
debugPrint('[AuthService] start: referer=$referer');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 确定归因类型
|
||||||
|
String? referrerType;
|
||||||
|
final adjustReferrer = await AttributionService.getAdjustReferrer();
|
||||||
|
final fbReferrer = await AttributionService.getFacebookReferrer();
|
||||||
|
|
||||||
|
if (adjustReferrer != null && adjustReferrer.isNotEmpty) {
|
||||||
|
referrerType = defaultTargetPlatform == TargetPlatform.iOS
|
||||||
|
? 'ios_adjust'
|
||||||
|
: 'android_adjust';
|
||||||
|
} else if (fbReferrer != null && fbReferrer.isNotEmpty) {
|
||||||
|
referrerType = 'fb';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (kDebugMode) {
|
||||||
|
debugPrint('[AuthService] start: referrerType=$referrerType');
|
||||||
|
}
|
||||||
|
|
||||||
// 尝试快速登录
|
// 尝试快速登录
|
||||||
EntityResponse<FastLoginResponse>? res;
|
EntityResponse<FastLoginResponse>? res;
|
||||||
for (var i = 0; i < maxRetries; i++) {
|
for (var i = 0; i < maxRetries; i++) {
|
||||||
@ -100,10 +118,14 @@ abstract class FrameworkAuthService {
|
|||||||
await Future<void>.delayed(Duration(seconds: retryDelaySeconds));
|
await Future<void>.delayed(Duration(seconds: retryDelaySeconds));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
final appType =
|
||||||
|
defaultTargetPlatform == TargetPlatform.iOS ? 'HIOS' : 'HAndroid';
|
||||||
res = await UserApi.fastLogin(
|
res = await UserApi.fastLogin(
|
||||||
deviceId: deviceId,
|
deviceId: deviceId,
|
||||||
sign: sign,
|
sign: sign,
|
||||||
referer: referer ?? '',
|
referer: referer ?? '',
|
||||||
|
app: appType,
|
||||||
|
type: referrerType,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@ -8,21 +8,30 @@ abstract final class UserApi {
|
|||||||
static ProxyClient get _client => ApiClient.instance.proxy;
|
static ProxyClient get _client => ApiClient.instance.proxy;
|
||||||
|
|
||||||
/// 设备快速登录
|
/// 设备快速登录
|
||||||
|
/// [deviceId] 设备ID (必填)
|
||||||
|
/// [sign] 签名,MD5(deviceId)大写32位 (必填)
|
||||||
|
/// [referer] 归因信息
|
||||||
|
/// [ch] 渠道
|
||||||
|
/// [type] referrer类型 (af/fb/gg/ios_adjust/android_adjust)
|
||||||
|
/// [app] 应用类型 (iOS: HIOS / Android: HAndroid)
|
||||||
static Future<EntityResponse<FastLoginResponse>> fastLogin({
|
static Future<EntityResponse<FastLoginResponse>> fastLogin({
|
||||||
required String deviceId,
|
required String deviceId,
|
||||||
required String sign,
|
required String sign,
|
||||||
String? referer,
|
String? referer,
|
||||||
String? ch,
|
String? ch,
|
||||||
String? type,
|
String? type,
|
||||||
|
String? app,
|
||||||
}) async {
|
}) async {
|
||||||
|
final config = ApiClient.instance.config;
|
||||||
return _client.requestEntity(
|
return _client.requestEntity(
|
||||||
path: '/v1/user/fast_login',
|
path: '/v1/user/fast_login',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
entityFactory: FastLoginResponse.fromJson,
|
entityFactory: FastLoginResponse.fromJson,
|
||||||
queryParams: {
|
queryParams: {
|
||||||
if (ch != null) 'ch': ch,
|
if (ch != null) 'ch': ch,
|
||||||
'pkg': ApiClient.instance.config.packageName,
|
'pkg': config.packageName,
|
||||||
if (type != null) 'type': type,
|
if (type != null) 'type': type,
|
||||||
|
if (app != null) 'app': app,
|
||||||
},
|
},
|
||||||
body: {
|
body: {
|
||||||
'referer': referer ?? '',
|
'referer': referer ?? '',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user