dio_utils.dart
6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:one_poem/res/constant.dart';
import 'package:one_poem/util/log_utils.dart';
import 'base_entity.dart';
import 'error_handle.dart';
/// 默认dio配置
int _connectTimeout = 15000;
int _receiveTimeout = 15000;
int _sendTimeout = 10000;
String _baseUrl = '';
List<Interceptor> _interceptors = [];
/// 初始化Dio配置
void configDio({
int? connectTimeout,
int? receiveTimeout,
int? sendTimeout,
String? baseUrl,
List<Interceptor>? interceptors,
}) {
_connectTimeout = connectTimeout ?? _connectTimeout;
_receiveTimeout = receiveTimeout ?? _receiveTimeout;
_sendTimeout = sendTimeout ?? _sendTimeout;
_baseUrl = baseUrl ?? _baseUrl;
_interceptors = interceptors ?? _interceptors;
}
typedef NetSuccessCallback<T> = Function(T data);
typedef NetSuccessListCallback<T> = Function(List<T> data);
typedef NetErrorCallback = Function(int code, String msg);
/// @weilu https://github.com/simplezhli
class DioUtils {
factory DioUtils() => _singleton;
DioUtils._() {
final BaseOptions _options = BaseOptions(
connectTimeout: _connectTimeout,
receiveTimeout: _receiveTimeout,
sendTimeout: _sendTimeout,
/// dio默认json解析,这里指定返回UTF8字符串,自己处理解析。(可也以自定义Transformer实现)
responseType: ResponseType.plain,
validateStatus: (_) {
// 不使用http状态码判断状态,使用AdapterInterceptor来处理(适用于标准REST风格)
return true;
},
baseUrl: _baseUrl,
// contentType: Headers.formUrlEncodedContentType, // 适用于post form表单提交
);
_dio = Dio(_options);
/// Fiddler抓包代理配置 https://www.jianshu.com/p/d831b1f7c45b
// (_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
// (HttpClient client) {
// client.findProxy = (uri) {
// //proxy all request to localhost:8888
// return 'PROXY 10.41.0.132:8888';
// };
// client.badCertificateCallback =
// (X509Certificate cert, String host, int port) => true;
// };
/// 添加拦截器
void addInterceptor(Interceptor interceptor) {
_dio.interceptors.add(interceptor);
}
_interceptors.forEach(addInterceptor);
}
static final DioUtils _singleton = DioUtils._();
static DioUtils get instance => DioUtils();
static late Dio _dio;
Dio get dio => _dio;
// 数据返回格式统一,统一处理异常
Future<BaseEntity<T>> _request<T>(String method, String url, {
Object? data,
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
Options? options,
}) async {
final Response<String> response = await _dio.request<String>(
url,
data: data,
queryParameters: queryParameters,
options: _checkOptions(method, options),
cancelToken: cancelToken,
);
try {
final String data = response.data.toString();
/// 集成测试无法使用 isolate https://github.com/flutter/flutter/issues/24703
/// 使用compute条件:数据大于10KB(粗略使用10 * 1024)且当前不是集成测试(后面可能会根据Web环境进行调整)
/// 主要目的减少不必要的性能开销
final bool isCompute = !Constant.isDriverTest && data.length > 10 * 1024;
debugPrint('isCompute:$isCompute');
final Map<String, dynamic> _map = isCompute ? await compute(parseData, data) : parseData(data);
return BaseEntity<T>.fromJson(_map);
} catch(e) {
debugPrint(e.toString());
return BaseEntity<T>(ExceptionHandle.parse_error, '数据解析错误!', null);
}
}
Options _checkOptions(String method, Options? options) {
options ??= Options();
options.method = method;
return options;
}
Future requestNetwork<T>(Method method, String url, {
NetSuccessCallback<T?>? onSuccess,
NetErrorCallback? onError,
Object? params,
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
Options? options,
}) {
return _request<T>(method.value, url,
data: params,
queryParameters: queryParameters,
options: options,
cancelToken: cancelToken,
).then<void>((BaseEntity<T> result) {
if (result.code == 0) {
onSuccess?.call(result.data);
} else {
_onError(result.code, result.message, onError);
}
}, onError: (dynamic e) {
_cancelLogPrint(e, url);
final NetError error = ExceptionHandle.handleException(e);
_onError(error.code, error.msg, onError);
});
}
/// 统一处理(onSuccess返回T对象,onSuccessList返回 List<T>)
void asyncRequestNetwork<T>(Method method, String url, {
NetSuccessCallback<T?>? onSuccess,
NetErrorCallback? onError,
Object? params,
Map<String, dynamic>? queryParameters,
CancelToken? cancelToken,
Options? options,
}) {
Stream.fromFuture(_request<T>(method.value, url,
data: params,
queryParameters: queryParameters,
options: options,
cancelToken: cancelToken,
)).asBroadcastStream()
.listen((result) {
if (result.code == 0) {
if (onSuccess != null) {
onSuccess(result.data);
}
} else {
_onError(result.code, result.message, onError);
}
}, onError: (dynamic e) {
_cancelLogPrint(e, url);
final NetError error = ExceptionHandle.handleException(e);
_onError(error.code, error.msg, onError);
});
}
void _cancelLogPrint(dynamic e, String url) {
if (e is DioError && CancelToken.isCancel(e)) {
Log.e('取消请求接口: $url');
}
}
void _onError(int? code, String msg, NetErrorCallback? onError) {
if (code == null) {
code = ExceptionHandle.unknown_error;
msg = '未知异常';
}
Log.e('接口请求异常: code: $code, mag: $msg');
onError?.call(code, msg);
}
}
Map<String, dynamic> parseData(String data) {
return json.decode(data) as Map<String, dynamic>;
}
enum Method {
get,
post,
put,
patch,
delete,
head
}
/// 使用拓展枚举替代 switch判断取值
/// https://zhuanlan.zhihu.com/p/98545689
extension MethodExtension on Method {
String get value => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'][index];
}