Repository navigation
Expand file tree
/
Copy pathapi.dart
More file actions
89 lines (71 loc) · 2.66 KB
/
Copy pathapi.dart
File metadata and controls
89 lines (71 loc) · 2.66 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
import 'package:dio/dio.dart';
import 'package:gsy_github_app_flutter/common/net/code.dart';
import 'dart:collection';
import 'package:gsy_github_app_flutter/common/net/interceptors/error_interceptor.dart';
import 'package:gsy_github_app_flutter/common/net/interceptors/header_interceptor.dart';
import 'package:gsy_github_app_flutter/common/net/interceptors/log_interceptor.dart';
import 'package:gsy_github_app_flutter/common/net/interceptors/response_interceptor.dart';
import 'package:gsy_github_app_flutter/common/net/interceptors/token_interceptor.dart';
import 'package:gsy_github_app_flutter/common/net/result_data.dart';
///http请求
class HttpManager {
static const CONTENT_TYPE_JSON = "application/json";
static const CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";
Dio _dio = new Dio(); // 使用默认配置
final TokenInterceptors _tokenInterceptors = new TokenInterceptors();
HttpManager() {
_dio.interceptors.add(new HeaderInterceptors());
_dio.interceptors.add(_tokenInterceptors);
_dio.interceptors.add(new LogsInterceptors());
_dio.interceptors.add(new ErrorInterceptors(_dio));
_dio.interceptors.add(new ResponseInterceptors());
}
///发起网络请求
///[ url] 请求url
///[ params] 请求参数
///[ header] 外加头
///[ option] 配置
netFetch(url, params, Map<String, dynamic> header, Options option, {noTip = false}) async {
Map<String, dynamic> headers = new HashMap();
if (header != null) {
headers.addAll(header);
}
if (option != null) {
option.headers = headers;
} else {
option = new Options(method: "get");
option.headers = headers;
}
resultError(DioError e) {
Response errorResponse;
if (e.response != null) {
errorResponse = e.response;
} else {
errorResponse = new Response(statusCode: 666);
}
if (e.type == DioErrorType.CONNECT_TIMEOUT || e.type == DioErrorType.RECEIVE_TIMEOUT) {
errorResponse.statusCode = Code.NETWORK_TIMEOUT;
}
return new ResultData(Code.errorHandleFunction(errorResponse.statusCode, e.message, noTip), false, errorResponse.statusCode);
}
Response response;
try {
response = await _dio.request(url, data: params, options: option);
} on DioError catch (e) {
return resultError(e);
}
if(response.data is DioError) {
return resultError(response.data);
}
return response.data;
}
///清除授权
clearAuthorization() {
_tokenInterceptors.clearAuthorization();
}
///获取授权token
getAuthorization() async {
return _tokenInterceptors.getAuthorization();
}
}
final HttpManager httpManager = new HttpManager();