2a29701f
liangchengyou
feat:提交代码
|
1
|
import 'package:dio/dio.dart';
|
478f2c8c
liangchengyou
feat:更新插件
|
2
|
import 'package:flutter/foundation.dart';
|
ea2c8205
liangchengyou
feat:更新代码
|
3
|
import 'package:wow_english/models/response_model.dart';
|
056970d8
Key
feat: api
|
4
|
import 'package:wow_english/network/basic_configuration.dart.txt';
|
23384a42
liangchengyou
feat:登陆请求
|
5
|
|
2a29701f
liangchengyou
feat:提交代码
|
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
enum HttpMethod {
get,
put,
post,
head,
patch,
delete,
}
class DioUtil {
static final Dio _dio = getDefDio();
static Dio getDefDio() {
Dio dio = Dio();
dio.options = getDefOptions();
return dio;
}
static BaseOptions getDefOptions() {
final BaseOptions options = BaseOptions();
|
056970d8
Key
feat: api
|
26
|
options.baseUrl = BasicConfigurationManager().baseUrl ?? '';
|
2a29701f
liangchengyou
feat:提交代码
|
27
28
|
options.connectTimeout = const Duration(milliseconds: 1000);
options.receiveTimeout = const Duration(milliseconds: 1000);
|
2a29701f
liangchengyou
feat:提交代码
|
29
30
31
32
|
return options;
}
Future<void> requestData<T>(
|
056970d8
Key
feat: api
|
33
34
35
36
37
38
39
40
41
|
HttpMethod method,
String path, {
data,
Function? successCallBack,
Function? errorCallBack,
Map<String, dynamic>? queryParameters,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
}) async {
|
2a29701f
liangchengyou
feat:提交代码
|
42
|
try {
|
a117a5a3
liangchengyou
feat:更新代码
|
43
44
45
|
Map<String, dynamic> headers = <String, dynamic>{};
if (method == HttpMethod.post) {
|
23384a42
liangchengyou
feat:登陆请求
|
46
|
headers['content-type'] = 'application/json';
|
a117a5a3
liangchengyou
feat:更新代码
|
47
|
}
|
2a29701f
liangchengyou
feat:提交代码
|
48
|
Response<dynamic> response;
|
056970d8
Key
feat: api
|
49
50
|
response = await _dio.request(path,
data: data ?? {},
|
2a29701f
liangchengyou
feat:提交代码
|
51
|
queryParameters: queryParameters,
|
056970d8
Key
feat: api
|
52
|
options: Options(method: method.name, headers: headers),
|
2a29701f
liangchengyou
feat:提交代码
|
53
54
|
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress);
|
056970d8
Key
feat: api
|
55
56
|
int statusCode = response.statusCode ?? 0;
if (statusCode >= 200 && statusCode < 300) {
|
23384a42
liangchengyou
feat:登陆请求
|
57
58
59
60
|
if (kDebugMode) {
print(response.data);
}
final ResponseModel model = ResponseModel.fromJson(response.data);
|
056970d8
Key
feat: api
|
61
62
|
if (model.code == 200) {
successCallBack?.call(response.data);
|
23384a42
liangchengyou
feat:登陆请求
|
63
|
} else {
|
056970d8
Key
feat: api
|
64
|
errorCallBack?.call(model.msg);
|
23384a42
liangchengyou
feat:登陆请求
|
65
|
}
|
2a29701f
liangchengyou
feat:提交代码
|
66
|
} else {
|
056970d8
Key
feat: api
|
67
|
errorCallBack?.call('请求失败');
|
2a29701f
liangchengyou
feat:提交代码
|
68
|
}
|
056970d8
Key
feat: api
|
69
|
} on DioException catch (error) {
|
478f2c8c
liangchengyou
feat:更新插件
|
70
71
72
|
if (kDebugMode) {
print(error);
}
|
056970d8
Key
feat: api
|
73
|
errorCallBack?.call("网络错误: ${error.message}");
|
2a29701f
liangchengyou
feat:提交代码
|
74
75
|
}
}
|
056970d8
Key
feat: api
|
76
|
}
|