fix:参考金值,研听,研值工程通用通用工程公共能力,重点对比网络请求、登录认证、状态管理、本地存储、WebView、推送、版本更新、一键登录、主题/UI 等模块,确定后续统一整改方案
This commit is contained in:
1 parent
6997a65c96
commit
d364c6d908
46 files changed
+2060
-395
No files matched your search
@@ -6,6 +6,8 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../config/app_config.dart';
|
||||
import '../storage/secure_identity_storage.dart';
|
||||
import '../../features/auth/application/auth_session_coordinator.dart';
|
||||
import 'api_exception.dart';
|
||||
import 'auth_interceptor.dart';
|
||||
import 'kratos_error_interceptor.dart';
|
||||
|
||||
@@ -26,7 +28,9 @@ AppConfig appConfig(Ref ref) => AppConfig.fromEnvironment();
|
||||
@Riverpod(keepAlive: true)
|
||||
Dio dio(Ref ref) {
|
||||
final config = ref.watch(appConfigProvider);
|
||||
final identityStorage = ref.watch(secureIdentityStorageProvider);
|
||||
final sessionStorage = ref.watch(authSessionStorageProvider);
|
||||
final coordinator = ref.watch(authSessionCoordinatorProvider);
|
||||
final backendOrigin = Uri.parse(config.apiBaseUrl);
|
||||
final d = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: config.apiBaseUrl,
|
||||
@@ -40,11 +44,57 @@ Dio dio(Ref ref) {
|
||||
},
|
||||
),
|
||||
);
|
||||
d.interceptors.add(AuthInterceptor(identityStorage));
|
||||
d.interceptors.add(
|
||||
AuthInterceptor(sessionStorage, backendOrigin: backendOrigin),
|
||||
);
|
||||
d.interceptors.add(KratosErrorInterceptor());
|
||||
d.interceptors.add(_SessionExpiryInterceptor(coordinator));
|
||||
if (!kReleaseMode) {
|
||||
d.interceptors.add(LogInterceptor(requestBody: false, responseBody: false));
|
||||
d.interceptors.add(FancyDioInterceptor());
|
||||
}
|
||||
d.interceptors.add(FancyDioInterceptor());
|
||||
return d;
|
||||
}
|
||||
|
||||
/// Explicit name for new code. [dioProvider] remains as a compatibility alias
|
||||
/// while existing feature clients are migrated incrementally.
|
||||
final backendDioProvider = dioProvider;
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
Dio publicDio(Ref ref) => Dio(
|
||||
BaseOptions(
|
||||
connectTimeout: const Duration(seconds: 15),
|
||||
receiveTimeout: const Duration(seconds: 30),
|
||||
),
|
||||
);
|
||||
|
||||
class _SessionExpiryInterceptor extends Interceptor {
|
||||
_SessionExpiryInterceptor(this._coordinator);
|
||||
|
||||
final AuthSessionCoordinator _coordinator;
|
||||
|
||||
@override
|
||||
Future<void> onError(
|
||||
DioException err,
|
||||
ErrorInterceptorHandler handler,
|
||||
) async {
|
||||
final exception = ApiException.from(err);
|
||||
if (exception.isUnauthorized) {
|
||||
// Network code only emits the session event. Navigation and presentation
|
||||
// observe AuthState elsewhere.
|
||||
try {
|
||||
await _coordinator.expireIfNeeded(exception);
|
||||
} catch (error, stackTrace) {
|
||||
FlutterError.reportError(
|
||||
FlutterErrorDetails(
|
||||
exception: error,
|
||||
stack: stackTrace,
|
||||
library: 'jinzhi network',
|
||||
context: ErrorDescription('while expiring an unauthorized session'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
handler.next(err);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user