056970d8
Key
feat: api
|
1
|
import 'dart:convert';
|
47569bdf
吴启风
feat:支持按需启用 Whist...
|
2
|
import 'dart:io';
|
056970d8
Key
feat: api
|
3
4
|
import 'package:dio/dio.dart';
|
47569bdf
吴启风
feat:支持按需启用 Whist...
|
5
|
import 'package:dio/io.dart';
|
99efc573
Key
兼容非json response ...
|
6
|
import 'package:flutter/foundation.dart';
|
056970d8
Key
feat: api
|
7
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
e55bbdf3
Key
fixed: aliyun oss...
|
8
|
import 'package:wow_english/utils/toast_util.dart';
|
056970d8
Key
feat: api
|
9
|
|
1892df31
Key
优化接口调用
|
10
|
import '../core/user_util.dart';
|
056970d8
Key
feat: api
|
11
12
13
14
15
16
|
import 'api_response/api_response_entity.dart';
import 'api_response/raw_data.dart';
import 'config.dart';
import 'exception.dart';
import 'token_interceptor.dart';
|
4b2c2f07
Key
feat: 三种修改密码的类型及接口
|
17
18
|
part 'apis.dart';
|
056970d8
Key
feat: api
|
19
20
21
|
RequestClient requestClient = RequestClient();
class RequestClient {
|
47569bdf
吴启风
feat:支持按需启用 Whist...
|
22
23
24
25
26
|
static const bool _enableWhistleCaptureInRelease = bool.fromEnvironment(
'ENABLE_WHISTLE_CA',
defaultValue: false,
);
|
056970d8
Key
feat: api
|
27
28
29
|
late Dio _dio;
RequestClient() {
|
47569bdf
吴启风
feat:支持按需启用 Whist...
|
30
31
32
|
_dio = Dio(BaseOptions(
baseUrl: RequestConfig.baseUrl,
connectTimeout: RequestConfig.connectTimeout));
|
056970d8
Key
feat: api
|
33
|
_dio.interceptors.add(TokenInterceptor());
|
99efc573
Key
兼容非json response ...
|
34
|
if (kDebugMode) {
|
47569bdf
吴启风
feat:支持按需启用 Whist...
|
35
36
37
38
39
40
41
42
43
44
45
46
47
|
_dio.interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
responseHeader: true,
maxWidth: 120));
}
// 仅供内部抓包构建使用;启用后会接受任意 HTTPS 证书。
if (Platform.isAndroid && (kDebugMode || _enableWhistleCaptureInRelease)) {
_dio.httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () =>
HttpClient()..badCertificateCallback = (_, __, ___) => true,
);
|
99efc573
Key
兼容非json response ...
|
48
|
}
|
056970d8
Key
feat: api
|
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
}
Future<T?> request<T>(
String url, {
required String method,
Map<String, dynamic>? queryParameters,
data,
Map<String, dynamic>? headers,
Function(ApiResponse<T>)? onResponse,
bool Function(ApiException)? onError,
}) async {
try {
Options options = Options()
..method = method
..headers = headers;
data = _convertRequestData(data);
Response response = await _dio.request(url, queryParameters: queryParameters, data: data, options: options);
|
934e2b47
liangchengyou
feat:权限调整+课程进度接口对接
|
68
|
debugPrint("response.body type=${response.data.runtimeType}");
|
056970d8
Key
feat: api
|
69
70
|
return _handleResponse<T>(response, onResponse);
} catch (e) {
|
934e2b47
liangchengyou
feat:权限调整+课程进度接口对接
|
71
|
debugPrint("e type=${e.runtimeType}");
|
99efc573
Key
兼容非json response ...
|
72
|
if (e is ApiException && e.code == 405) {
|
e55bbdf3
Key
fixed: aliyun oss...
|
73
|
showToast('登录已失效,请重新登录!', duration: const Duration(seconds: 3));
|
1892df31
Key
优化接口调用
|
74
75
76
|
UserUtil.logout();
return null;
}
|
056970d8
Key
feat: api
|
77
78
79
80
81
|
var exception = ApiException.from(e);
if (onError?.call(exception) != true) {
throw exception;
}
}
|
056970d8
Key
feat: api
|
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
return null;
}
_convertRequestData(data) {
if (data != null) {
data = jsonDecode(jsonEncode(data));
}
return data;
}
/// get
Future<T?> get<T>(
String url, {
Map<String, dynamic>? queryParameters,
Map<String, dynamic>? headers,
bool showLoading = true,
Function(ApiResponse<T>)? onResponse,
bool Function(ApiException)? onError,
}) {
return request(url,
|
47569bdf
吴启风
feat:支持按需启用 Whist...
|
102
103
104
105
106
|
method: 'GET',
queryParameters: queryParameters,
headers: headers,
onResponse: onResponse,
onError: onError);
|
056970d8
Key
feat: api
|
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
|
}
/// post
Future<T?> post<T>(
String url, {
Map<String, dynamic>? queryParameters,
data,
Map<String, dynamic>? headers,
bool showLoading = true,
Function(ApiResponse<T>)? onResponse,
bool Function(ApiException)? onError,
}) {
return request(url,
method: "POST",
queryParameters: queryParameters,
data: data,
headers: headers,
onResponse: onResponse,
onError: onError);
}
/// delete
Future<T?> delete<T>(
String url, {
Map<String, dynamic>? queryParameters,
data,
Map<String, dynamic>? headers,
bool showLoading = true,
Function(ApiResponse<T>)? onResponse,
bool Function(ApiException)? onError,
}) {
return request(url,
method: "DELETE",
queryParameters: queryParameters,
data: data,
headers: headers,
onResponse: onResponse,
onError: onError);
}
/// put
Future<T?> put<T>(
String url, {
Map<String, dynamic>? queryParameters,
data,
Map<String, dynamic>? headers,
bool showLoading = true,
Function(ApiResponse<T>)? onResponse,
bool Function(ApiException)? onError,
}) {
return request(url,
method: "PUT",
queryParameters: queryParameters,
data: data,
headers: headers,
onResponse: onResponse,
onError: onError);
}
/// 请求响应内容处理
T? _handleResponse<T>(
Response response,
Function(ApiResponse<T>)? onResponse,
) {
int statusCode = response.statusCode ?? -1;
|
056970d8
Key
feat: api
|
172
173
174
175
176
177
178
|
// 200..299 成功响应
if (statusCode >= 200 && statusCode <= 299) {
if (T.toString() == (RawData).toString()) {
RawData raw = RawData();
raw.value = response.data;
return raw as T;
} else {
|
99efc573
Key
兼容非json response ...
|
179
180
181
182
183
184
185
186
|
// 应对response的header中content-type: [text/html;charset=utf-8]的情况
// 实际上都是json格式返回
Map<String, dynamic> json;
if (response.data is String) {
json = jsonDecode(response.data);
} else {
json = response.data;
}
|
99efc573
Key
兼容非json response ...
|
187
|
ApiResponse<T> apiResponse = ApiResponse.fromJson(json);
|
056970d8
Key
feat: api
|
188
189
190
191
|
onResponse?.call(apiResponse);
return _handleBusinessResponse<T>(apiResponse);
}
} else {
|
99efc573
Key
兼容非json response ...
|
192
|
ApiException exception = ApiException(response.statusCode, ApiException.unknownException);
|
934e2b47
liangchengyou
feat:权限调整+课程进度接口对接
|
193
|
debugPrint("_handleResponse exception type=${exception.runtimeType}");
|
056970d8
Key
feat: api
|
194
195
196
197
198
199
200
201
202
|
throw exception;
}
}
/// 业务内容处理
T? _handleBusinessResponse<T>(ApiResponse<T> response) {
if (response.code == RequestConfig.successCode) {
return response.data;
} else {
|
99efc573
Key
兼容非json response ...
|
203
|
ApiException exception = ApiException(response.code, response.msg);
|
934e2b47
liangchengyou
feat:权限调整+课程进度接口对接
|
204
|
debugPrint("_handleBusinessResponse exception type=${exception.runtimeType}");
|
056970d8
Key
feat: api
|
205
206
207
208
|
throw exception;
}
}
}
|