2026-01-11 19:42:22 +08:00
|
|
|
|
import 'package:dio/dio.dart';
|
2026-01-18 20:21:14 +08:00
|
|
|
|
import 'package:maibu_satabot_v2/core/consts/http_api_consts.dart';
|
2026-03-16 12:40:02 +08:00
|
|
|
|
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
|
|
|
|
|
import 'package:maibu_satabot_v2/core/di/injection.dart';
|
|
|
|
|
|
import 'package:maibu_satabot_v2/features/auth/presentation/bloc/auth_cubit.dart';
|
2026-01-11 19:42:22 +08:00
|
|
|
|
|
|
|
|
|
|
class DioClient {
|
|
|
|
|
|
static Dio create() {
|
|
|
|
|
|
final dio = Dio(
|
|
|
|
|
|
BaseOptions(
|
2026-01-18 20:21:14 +08:00
|
|
|
|
baseUrl: HttpApiConsts.baseUrl,
|
2026-03-09 13:21:18 +08:00
|
|
|
|
connectTimeout: const Duration(seconds: 20),
|
|
|
|
|
|
receiveTimeout: const Duration(seconds: 20),
|
2026-01-11 19:42:22 +08:00
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2026-03-16 12:40:02 +08:00
|
|
|
|
|
2026-08-07 08:49:29 +08:00
|
|
|
|
dio.interceptors.add(
|
|
|
|
|
|
LogInterceptor(
|
|
|
|
|
|
requestBody: true,
|
|
|
|
|
|
responseBody: true,
|
|
|
|
|
|
requestHeader: true,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
2026-03-16 12:40:02 +08:00
|
|
|
|
|
2026-01-18 20:21:14 +08:00
|
|
|
|
dio.interceptors.add(
|
|
|
|
|
|
InterceptorsWrapper(
|
|
|
|
|
|
onRequest: (options, handler) {
|
|
|
|
|
|
final userCubit = sl<AppUserCubit>();
|
|
|
|
|
|
final token = userCubit.state.user?.token;
|
|
|
|
|
|
|
|
|
|
|
|
if (token != null && token.isNotEmpty) {
|
|
|
|
|
|
options.headers['Authorization'] = 'Bearer $token';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return handler.next(options);
|
|
|
|
|
|
},
|
2026-03-16 12:40:02 +08:00
|
|
|
|
|
|
|
|
|
|
onResponse: (response, handler) {
|
2026-08-07 08:49:29 +08:00
|
|
|
|
if (response.data is Map<String, dynamic>) {
|
|
|
|
|
|
final code = response.data['code'];
|
|
|
|
|
|
if (code == 401 || code == 403) {
|
|
|
|
|
|
print(
|
|
|
|
|
|
'>>> [DIO] 🚨🚨🚨 收到业务错误码 $code,触发 Token 过期处理!URL: ${response.requestOptions.uri},时间: ${DateTime.now()}',
|
|
|
|
|
|
);
|
|
|
|
|
|
try {
|
|
|
|
|
|
sl<AuthCubit>().tokenExpired();
|
|
|
|
|
|
} catch (ex) {}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-03-16 12:40:02 +08:00
|
|
|
|
return handler.next(response);
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
onError: (DioException e, handler) async {
|
2026-08-07 08:49:29 +08:00
|
|
|
|
if (e.response?.statusCode == 401 || e.response?.statusCode == 403) {
|
|
|
|
|
|
print(
|
|
|
|
|
|
'>>> [DIO] 🚨🚨🚨 收到 HTTP ${e.response?.statusCode},触发 Token 过期处理!URL: ${e.requestOptions.uri},时间: ${DateTime.now()}',
|
|
|
|
|
|
);
|
2026-03-16 12:40:02 +08:00
|
|
|
|
try {
|
2026-08-07 08:49:29 +08:00
|
|
|
|
sl<AuthCubit>().tokenExpired();
|
|
|
|
|
|
} catch (ex) {}
|
2026-03-16 12:40:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return handler.next(e);
|
|
|
|
|
|
},
|
2026-01-18 20:21:14 +08:00
|
|
|
|
),
|
|
|
|
|
|
);
|
2026-03-16 12:40:02 +08:00
|
|
|
|
|
2026-01-11 19:42:22 +08:00
|
|
|
|
return dio;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|