网络请求打通,ws 打通

This commit is contained in:
Cody
2026-03-09 19:05:55 +08:00
parent 997d821447
commit 3c1976b343
60 changed files with 1392 additions and 552 deletions

View File

@@ -17,9 +17,6 @@ class ApiConfig {
/// Token 过期时的刷新回调
final OnTokenRefresh? onTokenRefresh;
/// 需要强制登出时的回调
final OnForceLogout? onForceLogout;
/// Token 更新后的通知回调
///
/// 在 [updateToken] 被调用且新 token 非空时触发。
@@ -61,14 +58,6 @@ class ApiConfig {
/// `{ code, data, message }` 结构。返回 null 表示不变换。
final OnTransformResponse? onTransformResponse;
// ── 错误码集合 ──
/// App 层定义的 Token 过期错误码集合
final Set<int> tokenExpiredCodes;
/// App 层定义的强制登出错误码集合
final Set<int> forceLogoutCodes;
// ── 重试配置 ──
/// 瞬态错误最大重试次数5xx / 超时 / 连接失败)
@@ -110,7 +99,6 @@ class ApiConfig {
this.token,
this.platformHeaders = const {},
this.onTokenRefresh,
this.onForceLogout,
this.onTokenUpdated,
this.onLog,
this.onCheckNetworkAvailable,
@@ -118,8 +106,6 @@ class ApiConfig {
this.onDecryptResponse,
this.onBusinessError,
this.onTransformResponse,
this.tokenExpiredCodes = const {},
this.forceLogoutCodes = const {},
this.maxRetries = 0,
this.retryBaseDelay = const Duration(seconds: 1),
this.tokenRefreshTimeout = const Duration(seconds: 10),
@@ -136,12 +122,12 @@ class ApiConfig {
Map<String, String>? customHeaders,
}) {
final headers = <String, String>{
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
'Keep-Alive': 'timeout=60',
'content-type': 'application/json; charset=utf-8',
'accept': 'application/json',
'keep-alive': 'timeout=60',
// Unix 时间戳(秒),整数值,非格式化日期字符串
'Timestamp': '${DateTime.now().millisecondsSinceEpoch ~/ 1000}',
'APP-Request-ID': _generateRequestId(),
'timestamp': '${DateTime.now().millisecondsSinceEpoch ~/ 1000}',
'app-request-id': _generateRequestId(),
};
// 合并平台 headersApp 层注入的 version、platform 等)

View File

@@ -8,9 +8,6 @@ import 'package:networks_sdk/src/domain/entities/encrypted_request.dart';
/// Token 刷新回调,返回新 token返回 null 表示刷新失败
typedef OnTokenRefresh = Future<String?> Function();
/// 强制登出回调
typedef OnForceLogout = void Function();
// ── Token 生命周期 ──
/// 获取 token 过期时间
@@ -73,11 +70,27 @@ typedef OnDecryptResponse =
// ── 业务错误 ──
/// 业务错误拦截回调
/// SDK 层收到 App 层对业务错误码的处置指令
enum BusinessErrorAction {
/// 刷新 token 后重试原请求(原 tokenExpiredCodes 行为)
refreshToken,
/// 强制登出,中断当前请求(原 forceLogoutCodes 行为)
forceLogout,
/// App 层已处理(如全局弹窗/ToastSDK 正常放行响应,不在 decodeResponse 中抛错
handled,
/// 未处理SDK 继续正常流程decodeResponse 会抛 ApiError 给调用方
unhandled,
}
/// 业务错误统一回调
///
/// App 层统一处理特定错误码,返回 true = 已处理SDK 不再抛错)
/// 返回 false = 未处理SDK 继续正常流程)
typedef OnBusinessError = bool Function(int code, String message, String path);
/// 所有非 0 业务码token 过期、强制登出、踢下线、普通业务错误)全部经此入口
/// App 层通过返回 [BusinessErrorAction] 告诉 SDK 该怎么做
typedef OnBusinessError =
BusinessErrorAction Function(int code, String message, String path);
/// 响应变换回调
///