优化:日志输出

This commit is contained in:
ivan 2026-03-26 14:46:50 +08:00
parent c6449734f9
commit 83c1c56c36
6 changed files with 81 additions and 19 deletions

View File

@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:math';
import 'package:encrypt/encrypt.dart';
@ -28,18 +29,19 @@ class ApiCrypto {
}
/// Base64
static String randomBase64([int byteLength = 16]) {
final bytes = List<int>.generate(
byteLength, (_) => DateTime.now().millisecondsSinceEpoch % 256);
return base64Encode(bytes);
static String randomBase64() {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
final random = Random();
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
static String randomAlnum() {
const chars =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
return List.generate(8,
(_) => chars[DateTime.now().microsecondsSinceEpoch % chars.length])
.join();
final random = Random();
return List.generate(8, (_) => chars[random.nextInt(chars.length)]).join();
}
}

View File

@ -135,7 +135,7 @@ class ProxyClient {
final v2BodyEncoded = jsonEncode(v2Body);
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);
final proxyBody = <String, dynamic>{
@ -153,6 +153,7 @@ class ProxyClient {
final url = '$_baseUrl${config.proxyPath}';
logWithEmbeddedJson('========== 请求信息 ===========\n\n目标URL: $url');
logWithEmbeddedJson('========== 实际请求体 ===========\n${jsonEncode(proxyBody)}');
final response = await http.post(
Uri.parse(url),

View File

@ -54,6 +54,7 @@ abstract class AttributionCallbacks {
Future<String?> getReferrer();
Future<String?> getAdjustReferrer();
Future<String?> getPlatformReferrer();
Future<String?> getFacebookReferrer();
}
///
@ -66,6 +67,9 @@ class DefaultAttributionCallbacks implements AttributionCallbacks {
@override
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?> getPlatformReferrer() =>
callbacks.getPlatformReferrer();
static Future<String?> getFacebookReferrer() =>
callbacks.getFacebookReferrer();
}

View File

@ -50,26 +50,48 @@ void _logLong(String text) {
_proxyLog.d(text);
return;
}
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) {
if (line.length > _maxLogChunk) {
emitBuffer();
emitLineInChunks(line);
continue;
}
final lineWithNewline = buffer.isEmpty ? line : '\n$line';
if (buffer.length + lineWithNewline.length > _maxLogChunk &&
buffer.isNotEmpty) {
chunkIndex++;
_proxyLog.d('(part $chunkIndex)\n$buffer');
buffer.clear();
emitBuffer();
buffer.write(line);
} else {
if (buffer.isNotEmpty) buffer.write('\n');
buffer.write(line);
}
}
if (buffer.isNotEmpty) {
chunkIndex++;
_proxyLog
.d(chunkIndex > 1 ? '(part $chunkIndex)\n$buffer' : buffer.toString());
}
emitBuffer();
}
/// JSON

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../api/api_client.dart';
import '../api/proxy_client.dart';
@ -90,6 +91,23 @@ abstract class FrameworkAuthService {
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;
for (var i = 0; i < maxRetries; i++) {
@ -100,10 +118,14 @@ abstract class FrameworkAuthService {
await Future<void>.delayed(Duration(seconds: retryDelaySeconds));
}
try {
final appType =
defaultTargetPlatform == TargetPlatform.iOS ? 'HIOS' : 'HAndroid';
res = await UserApi.fastLogin(
deviceId: deviceId,
sign: sign,
referer: referer ?? '',
app: appType,
type: referrerType,
);
break;
} catch (e) {

View File

@ -8,21 +8,30 @@ abstract final class UserApi {
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({
required String deviceId,
required String sign,
String? referer,
String? ch,
String? type,
String? app,
}) async {
final config = ApiClient.instance.config;
return _client.requestEntity(
path: '/v1/user/fast_login',
method: 'POST',
entityFactory: FastLoginResponse.fromJson,
queryParams: {
if (ch != null) 'ch': ch,
'pkg': ApiClient.instance.config.packageName,
'pkg': config.packageName,
if (type != null) 'type': type,
if (app != null) 'app': app,
},
body: {
'referer': referer ?? '',