feat(network): 切换生产环境 API 域名并削弱认证接口令牌携带逻辑
This commit is contained in:
1 parent
4976a26892
commit
a8fba22dff
4 files changed
+76
-3
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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<dynamic> 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<dynamic> 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)) {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -33,6 +33,7 @@ class _SplashPageState extends ConsumerState<SplashPage> {
|
||||
try {
|
||||
final target = TokenStorage.isLoggedIn ? '/home' : '/login';
|
||||
context.go(target);
|
||||
_repaintAfterNavigation();
|
||||
} catch (_) {
|
||||
// 极端情况下的路由异常不应卡死首屏,重试一次。
|
||||
if (mounted) {
|
||||
@@ -42,6 +43,23 @@ class _SplashPageState extends ConsumerState<SplashPage> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 规避 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);
|
||||
|
||||
Reference in new issue
Block a user