http_utils.dart
3.71 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
import 'package:flutter/cupertino.dart';
import 'package:centercontrol/utils/Constant.dart';
import 'package:centercontrol/utils/ToastUtil.dart';
import 'package:centercontrol/view/progress_dialog.dart';
/**
* http_utils.dart
*
* Created by iotjin on 2020/07/07.
* description: 项目接口请求 管理类
*/
import 'apis.dart';
import 'dio_utils.dart';
import 'log_utils.dart';
typedef Success<T> = Function(T data);
typedef Fail = Function(int code);
class HttpUtils {
static ProgressDialog pr;
//登录
static void login<T>(
BuildContext context,
parameters, {
Success success,
Fail fail,
}) {
post(context, HttpConstant.LOGIN, parameters, success: success, fail: fail);
}
static void pdaWeld<T>(
BuildContext context,
parameters, {
Success success,
Fail fail,
}) {
post(context, HttpConstant.PDAWELD, parameters, success: success, fail: fail);
}
static void pdaStation<T>(
BuildContext context,
parameters, {
Success success,
Fail fail,
}) {
post(context, HttpConstant.PDAStation, parameters, success: success, fail: fail);
}
static void pdaError<T>(
BuildContext context,
parameters, {
Success success,
Fail fail,
}) {
post(context, HttpConstant.PDAError, parameters, success: success, fail: fail);
}
static void pdaWeidHandle<T>(
BuildContext context,
parameters, {
Success success,
Fail fail,
}) {
post(context, HttpConstant.PDAWeidHandle, parameters, success: success, fail: fail);
}
//GET
static void GetRequest<T>(
BuildContext context,
APIType apiType,
parameters, {
Success success,
Fail fail,
}) {
post(context, APITypeValues[apiType], parameters, success: success, fail: fail);
}
//POST
static void PostRequest<T>(
BuildContext context,
APIType apiType,
parameters, {
Success success,
Fail fail,
}) {
post(context, APITypeValues[apiType], parameters, success: success, fail: fail);
}
/********************************* 分割线 ********************************/
//get 请求
static void get<T>(
BuildContext context,
String url,
parameters, {
Success success,
Fail fail,
}) {
_request(context, Method.GET, url, parameters, success: success, fail: fail);
}
//post 请求
static void post<T>(
BuildContext context,
String url,
parameters, {
Success success,
Fail fail,
}) {
_request(context, Method.POST, url, parameters, success: success, fail: fail);
}
//_request 请求
static void _request<T>(
BuildContext context,
Method method,
String url,
parameters, {
Success success,
Fail fail,
}) {
if(pr == null) {
pr = new ProgressDialog(context, ProgressDialogType.Normal);
}
if(!pr.isShowing()) {
pr.setMessage('正在加载信息...');
pr.show();
}
/// restful 请求处理
/// /base/search/hist/:user_id user_id=27
/// 最终生成 url 为 /base/search/hist/27
parameters.forEach((key, value) {
if (url.indexOf(key) != -1) {
url = url.replaceAll(':$key', value.toString());
}
});
DioUtils.request(method, url, parameters, success: (result) {
if(pr.isShowing()) {
pr.hide();
}
if (result['code'] == 200) {
if (success != null) {
success(result);
}
} else {
//其他状态,弹出错误提示信息
ToastUtil.showText(result['msg']);
}
}, fail: (code, msg) {
if(pr.isShowing()) {
pr.hide();
}
ToastUtil.showText(msg);
if (fail != null) {
fail(code);
}
});
}
}