|
@@ -1,9 +1,12 @@
|
|
|
+import 'dart:convert';
|
|
|
import 'dart:io';
|
|
|
|
|
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
|
|
import 'package:dio/dio.dart';
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
+import 'package:logger/logger.dart';
|
|
|
|
|
|
+import '../common/SecureStorageUtil.dart';
|
|
|
//import 'custom_interceptors.dart';
|
|
|
|
|
|
class DioClient {
|
|
@@ -11,6 +14,7 @@ class DioClient {
|
|
|
late Dio _dio;
|
|
|
|
|
|
factory DioClient() => _instance;
|
|
|
+ final Logger _logger = Logger();
|
|
|
|
|
|
DioClient._internal() {
|
|
|
_dio = Dio(BaseOptions(
|
|
@@ -19,12 +23,72 @@ class DioClient {
|
|
|
receiveTimeout: 3000,
|
|
|
));
|
|
|
|
|
|
+ // 仅在调试模式下添加日志拦截器
|
|
|
+ assert(() {
|
|
|
+ _dio.interceptors.add(LogInterceptor(
|
|
|
+ request: true,
|
|
|
+ requestBody: true,
|
|
|
+ responseBody: true,
|
|
|
+ error: true,
|
|
|
+ logPrint: _logger.d, // 使用 Logger 插件打印日志
|
|
|
+ ));
|
|
|
+ return true;
|
|
|
+ }());
|
|
|
+
|
|
|
+ //token
|
|
|
+ _dio.interceptors.add(TokenInterceptor());
|
|
|
// 添加拦截器
|
|
|
_dio.interceptors.add(CustomInterceptors());
|
|
|
+
|
|
|
+ // 添加响应拦截器
|
|
|
+ _dio.interceptors.add(InterceptorsWrapper(
|
|
|
+ onResponse: (Response<dynamic> response, ResponseInterceptorHandler handler) {
|
|
|
+ final responseData = response.data as Map<String, dynamic>;
|
|
|
+ if (responseData['ret'] == 1) {
|
|
|
+ handler.next(
|
|
|
+ Response<dynamic>(
|
|
|
+ data: responseData['data'],
|
|
|
+ headers: response.headers,
|
|
|
+ requestOptions: response.requestOptions,
|
|
|
+ statusCode: response.statusCode,
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ handler.reject(
|
|
|
+ DioError(
|
|
|
+ requestOptions: response.requestOptions,
|
|
|
+ error: AppException(message: responseData['msg'] ?? 'Unknown Error'),
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
+ },
|
|
|
+ ));
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
Dio get dio => _dio;
|
|
|
}
|
|
|
+
|
|
|
+class TokenInterceptor extends Interceptor {
|
|
|
+ // 这里假设您有一个方法来从安全存储中获取Token
|
|
|
+ Future<String?> getToken() async {
|
|
|
+ // 从您存储Token的地方获取Token,例如从SharedPreferences或SecureStorage
|
|
|
+ String? token = await SecureStorageUtil().getString("token");
|
|
|
+ return token;
|
|
|
+ //return "Your_Token";
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ Future<void> onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
|
|
|
+ final token = await getToken();
|
|
|
+ if (token != null) {
|
|
|
+ options.headers["Authorization"] = "Bearer $token";
|
|
|
+ }
|
|
|
+ return super.onRequest(options, handler);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
class CustomInterceptors extends Interceptor {
|
|
|
int _retryCount = 0;
|
|
|
final List<String> _backupUrls = ['https://api.androidrj03.top', 'https://api.androidrj88.com'];
|
|
@@ -119,3 +183,23 @@ class AppException implements Exception {
|
|
|
return message;
|
|
|
}
|
|
|
}
|
|
|
+extension DioClientExtension on DioClient {
|
|
|
+ Future<Map<String, dynamic>> get(String path, {Map<String, dynamic>? queryParameters}) async {
|
|
|
+ final response = await _dio.get(path, queryParameters: queryParameters);
|
|
|
+ return response.data as Map<String, dynamic>;
|
|
|
+ }
|
|
|
+
|
|
|
+ Future<Map<String, dynamic>> post(String path, {Map<String, dynamic>? data}) async {
|
|
|
+ final response = await _dio.post(path, data: data);
|
|
|
+ return response.data as Map<String, dynamic>;
|
|
|
+ }
|
|
|
+
|
|
|
+ Future<Map<String, dynamic>> put(String path, {Map<String, dynamic>? data}) async {
|
|
|
+ final response = await _dio.put(path, data: data);
|
|
|
+ return response.data as Map<String, dynamic>;
|
|
|
+ }
|
|
|
+
|
|
|
+ Future<void> download(String urlPath, String savePath) async {
|
|
|
+ await _dio.download(urlPath, savePath);
|
|
|
+ }
|
|
|
+}
|