From a8fba22dff9e5bca56828e5ccc59d0e5646b285f Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Thu, 5 Nov 2026 18:09:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(network):=20=E5=88=87=E6=8D=A2=E7=94=9F?= =?UTF-8?q?=E4=BA=A7=E7=8E=AF=E5=A2=83=20API=20=E5=9F=9F=E5=90=8D=E5=B9=B6?= =?UTF-8?q?=E5=89=8A=E5=BC=B1=E8=AE=A4=E8=AF=81=E6=8E=A5=E5=8F=A3=E4=BB=A4?= =?UTF-8?q?=E7=89=8C=E6=90=BA=E5=B8=A6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/app/constants/app_constants.dart | 2 +- lib/core/network/dio_client.dart | 57 ++++++++++++++++++- lib/features/auth/data/auth_api.dart | 2 +- .../auth/presentation/splash_page.dart | 18 ++++++ 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/lib/app/constants/app_constants.dart b/lib/app/constants/app_constants.dart index 1a7eae8..768f5b1 100644 --- a/lib/app/constants/app_constants.dart +++ b/lib/app/constants/app_constants.dart @@ -3,7 +3,7 @@ class AppConstants { const AppConstants._(); /// 生产环境基础地址。 - static const String kBaseUrl = 'http://192.168.100.244:8000/api/'; + static const String kBaseUrl = 'https://api.ttstd.com/api/'; /// 请求超时(毫秒)。 static const int kConnectTimeoutMs = 15000; diff --git a/lib/core/network/dio_client.dart b/lib/core/network/dio_client.dart index d351b4a..28056b3 100644 --- a/lib/core/network/dio_client.dart +++ b/lib/core/network/dio_client.dart @@ -7,6 +7,48 @@ import '../utils/local_network_access.dart'; import 'api_error.dart'; import 'api_exception.dart'; +const _sensitiveKeys = {'token', 'accessToken', 'refreshToken', 'password'}; + +/// 调试日志用:递归遮盖凭证字段,避免 token / 密码进入日志。 +dynamic _logBody(dynamic data) { + if (data is Map) { + return data.map( + (key, value) => + MapEntry(key, _sensitiveKeys.contains(key) ? '***' : _logBody(value)), + ); + } + if (data is List) return data.map(_logBody).toList(); + return data; +} + +String _logUri(Uri uri) { + if (uri.queryParameters.isEmpty) return uri.toString(); + return uri + .replace( + queryParameters: uri.queryParameters.map( + (key, value) => + MapEntry(key, _sensitiveKeys.contains(key) ? '***' : value), + ), + ) + .toString(); +} + +void _logRequest(RequestOptions options) { + if (kDebugMode) { + debugPrint('[Dio] -> ${options.method} ${_logUri(options.uri)}'); + } +} + +void _logResponse(Response response) { + if (kDebugMode) { + debugPrint( + '[Dio] <- ${response.requestOptions.method} ' + '${_logUri(response.requestOptions.uri)} ' + 'status=${response.statusCode} body=${_logBody(response.data)}', + ); + } +} + /// 统一 Dio 客户端。 /// /// 职责: @@ -213,10 +255,14 @@ class _AuthInterceptor extends Interceptor { ); return; } - final token = TokenStorage.accessToken; + // 登录/注册/找回密码等公开认证接口不携带旧令牌,避免过期令牌触发 401。 + final isPublicAuth = options.path.startsWith('v1/open/auth/') || + options.path.startsWith('/v1/open/auth/'); + final token = isPublicAuth ? null : TokenStorage.accessToken; if (token != null && token.isNotEmpty) { options.headers['Authorization'] = 'Bearer $token'; } + _logRequest(options); super.onRequest(options, handler); } } @@ -254,6 +300,7 @@ class _ResponseInterceptor extends Interceptor { Response response, ResponseInterceptorHandler handler, ) { + _logResponse(response); if (response.requestOptions.extra['sessionVersion'] != TokenStorage.sessionVersion) { handler.reject( @@ -295,6 +342,14 @@ class _ErrorInterceptor extends Interceptor { @override void onError(DioException err, ErrorInterceptorHandler handler) async { + if (kDebugMode) { + debugPrint( + '[Dio] !! ${err.requestOptions.method} ' + '${_logUri(err.requestOptions.uri)} ' + 'status=${err.response?.statusCode} type=${err.type} ' + 'body=${_logBody(err.response?.data)}', + ); + } final body = err.response?.data; final code = body is Map ? body['code']?.toString() : null; if (!isAuthError(code, err.response?.statusCode)) { diff --git a/lib/features/auth/data/auth_api.dart b/lib/features/auth/data/auth_api.dart index e0d077e..d19d293 100644 --- a/lib/features/auth/data/auth_api.dart +++ b/lib/features/auth/data/auth_api.dart @@ -18,7 +18,7 @@ class AuthApi { required String password, }) async { final data = await DioClient.post( - 'v1/open/login', + 'v1/open/auth/login', data: { 'username': username, 'password': password, diff --git a/lib/features/auth/presentation/splash_page.dart b/lib/features/auth/presentation/splash_page.dart index 3b7a6a1..9ab0255 100644 --- a/lib/features/auth/presentation/splash_page.dart +++ b/lib/features/auth/presentation/splash_page.dart @@ -33,6 +33,7 @@ class _SplashPageState extends ConsumerState { try { final target = TokenStorage.isLoggedIn ? '/home' : '/login'; context.go(target); + _repaintAfterNavigation(); } catch (_) { // 极端情况下的路由异常不应卡死首屏,重试一次。 if (mounted) { @@ -42,6 +43,23 @@ class _SplashPageState extends ConsumerState { } } + /// 规避 Impeller(OpenGLES) 冷启动首帧的字形图集纹理错位: + /// 首屏可能整块图标/文字残缺,且未变化的图层不会再绘制,画面会一直残缺, + /// 直到触发一次重排/重绘(如切页、旋转)才恢复。首个页面绘制完成后主动把 + /// 整棵渲染树标记为待重绘,强制重新录制并重新光栅化一次。 + static void _repaintAfterNavigation() { + WidgetsBinding.instance.addPostFrameCallback((_) { + for (final view in WidgetsBinding.instance.renderViews) { + _markNeedsPaint(view); + } + }); + } + + static void _markNeedsPaint(RenderObject object) { + object.markNeedsPaint(); + object.visitChildren(_markNeedsPaint); + } + @override Widget build(BuildContext context) { final l10n = AppLocalizations.of(context);