5dd7c07138
网络嗅探与自动恢复: - ConnectivityNotifier:在线时 30s 检测,离线时切换为 1min 嗅探 - 新增 networkRecoveryCountProvider:网络恢复时自增,各数据 provider 通过 ref.watch 实现自动重新加载 - inventory/product/stock_in/stock_out/partner provider 均已接入 FutureBuilder 类屏幕: - finance_screen + batch_tracking_screen 通过 ref.listen 监听恢复事件 修复问题 1:API 超时 10s/30s → 5s/15s,减少无网络时等待 修复问题 2:offline banner 文字由"写操作已禁用"改为实际行为描述 连通性感知: - app 启动时(_AppBootstrap)立即 forceCheck,路由跳转前状态已就绪 - 登录页:watch connectivityProvider,离线时显示警告 banner - 登录按钮:离线状态下直接提示,不等待超时 测试: - login_screen_test + login_flow_test 通过 ConnectivityNotifier(skipInit: true) 覆盖 provider,避免测试环境中 pending HTTP timer Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
128 lines
4.4 KiB
Dart
128 lines
4.4 KiB
Dart
import 'package:dio/dio.dart';
|
||
import 'package:flutter/foundation.dart';
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
import '../auth/auth_state.dart';
|
||
import '../config/app_config.dart';
|
||
import '../../providers/connectivity_provider.dart';
|
||
|
||
/// Public Dio instance for unauthenticated calls (login / refresh)
|
||
final _publicDio = Dio(BaseOptions(
|
||
baseUrl: AppConfig.apiBaseUrl,
|
||
connectTimeout: const Duration(seconds: 5),
|
||
receiveTimeout: const Duration(seconds: 15),
|
||
headers: {'Content-Type': 'application/json'},
|
||
));
|
||
|
||
final apiClientProvider = Provider<ApiClient>((ref) {
|
||
// 只监听登录/登出,不监听 token 内容变化。
|
||
// token refresh 只改 accessToken,isLoggedIn 不变,provider 不重建,
|
||
// 避免旧 Dio 被 close 后还在执行 fetch 导致 "adapter was closed"。
|
||
ref.watch(authStateProvider.select((s) => s.isLoggedIn));
|
||
final user = ref.read(authStateProvider).user;
|
||
|
||
final client = ApiClient(
|
||
token: user?.accessToken,
|
||
refreshToken: user?.refreshToken,
|
||
onTokenRefreshed: (newToken) {
|
||
ref.read(authStateProvider.notifier).updateAccessToken(newToken);
|
||
},
|
||
onAuthFailed: () {
|
||
if (ref.read(authStateProvider).isLoggedIn) {
|
||
ref.read(authStateProvider.notifier).logout();
|
||
}
|
||
},
|
||
onConnectionError: () {
|
||
ref.read(connectivityProvider.notifier).forceCheck();
|
||
},
|
||
);
|
||
ref.onDispose(client.dispose);
|
||
return client;
|
||
});
|
||
|
||
class ApiClient {
|
||
late final Dio _dio;
|
||
bool _disposed = false;
|
||
|
||
ApiClient({
|
||
String? token,
|
||
String? refreshToken,
|
||
void Function(String newToken)? onTokenRefreshed,
|
||
void Function()? onAuthFailed,
|
||
void Function()? onConnectionError,
|
||
}) {
|
||
_dio = Dio(BaseOptions(
|
||
baseUrl: AppConfig.apiBaseUrl,
|
||
connectTimeout: const Duration(seconds: 5),
|
||
receiveTimeout: const Duration(seconds: 15),
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
if (token != null) 'Authorization': 'Bearer $token',
|
||
},
|
||
));
|
||
|
||
// 网络错误 + 401 拦截器
|
||
_dio.interceptors.add(
|
||
InterceptorsWrapper(
|
||
onError: (DioException e, ErrorInterceptorHandler handler) async {
|
||
if (_disposed) return handler.next(e);
|
||
|
||
// 网络层错误(连接拒绝、超时等)→ 立即触发连通性检测
|
||
final isNetworkError = e.type == DioExceptionType.connectionError ||
|
||
e.type == DioExceptionType.connectionTimeout ||
|
||
e.type == DioExceptionType.receiveTimeout ||
|
||
e.type == DioExceptionType.sendTimeout;
|
||
if (isNetworkError) {
|
||
onConnectionError?.call();
|
||
return handler.next(e);
|
||
}
|
||
|
||
if (e.response?.statusCode == 401 && (refreshToken ?? '').isNotEmpty) {
|
||
debugPrint('[ApiClient] got 401 on ${e.requestOptions.path}, trying refresh...');
|
||
try {
|
||
final resp = await _publicDio.post('/auth/refresh', data: {
|
||
'refresh_token': refreshToken,
|
||
});
|
||
final newToken = resp.data['data']['access_token'] as String;
|
||
_dio.options.headers['Authorization'] = 'Bearer $newToken';
|
||
if (!_disposed) onTokenRefreshed?.call(newToken);
|
||
final opts = e.requestOptions;
|
||
opts.headers['Authorization'] = 'Bearer $newToken';
|
||
final retryResp = await _dio.fetch(opts);
|
||
return handler.resolve(retryResp);
|
||
} catch (refreshErr) {
|
||
debugPrint('[ApiClient] refresh failed: $refreshErr');
|
||
if (!_disposed) onAuthFailed?.call();
|
||
}
|
||
} else if (e.response?.statusCode == 401) {
|
||
debugPrint('[ApiClient] got 401 on ${e.requestOptions.path}, no refresh token');
|
||
}
|
||
return handler.next(e);
|
||
},
|
||
),
|
||
);
|
||
}
|
||
|
||
/// 取消所有进行中的请求,标记实例为已废弃
|
||
void dispose() {
|
||
_disposed = true;
|
||
_dio.close(force: true);
|
||
}
|
||
|
||
Future<Response> get(String path, {Map<String, dynamic>? params}) =>
|
||
_dio.get(path, queryParameters: params);
|
||
|
||
Future<Response> post(String path, {dynamic data}) =>
|
||
_dio.post(path, data: data);
|
||
|
||
Future<Response> put(String path, {dynamic data}) =>
|
||
_dio.put(path, data: data);
|
||
|
||
Future<Response> delete(String path) => _dio.delete(path);
|
||
}
|
||
|
||
/// Unauthenticated client for login/refresh
|
||
class PublicApiClient {
|
||
static Future<Response> post(String path, {dynamic data}) =>
|
||
_publicDio.post(path, data: data);
|
||
}
|