Skip to content

Commit 4a81939

Browse files
committed
增加DIO
1 parent 4fc9a21 commit 4a81939

7 files changed

Lines changed: 220 additions & 0 deletions

File tree

‎lib/common/config/Config.dart‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
class Config {
22
static const PAGE_SIZE = 20;
3+
static const DEBUG = true;
4+
5+
6+
/// //////////////////////////////////////常量////////////////////////////////////// ///
7+
static const TOKEN_KEY = "token";
8+
static const USER_NAME_KEY = "user-name";
9+
static const PW_KEY = "user-pw";
10+
static const USER_BASIC_CODE = "user-basic-code";
11+
static const USER_INFO = "user-info";
12+
static const LANGUAGE_SELECT = "language-select";
13+
static const LANGUAGE_SELECT_NAME = "language-select-name";
14+
static const REFRESH_LANGUAGE = "refreshLanguageApp";
315
}

‎lib/common/local/LocalStorage.dart‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:shared_preferences/shared_preferences.dart';
2+
3+
///SharedPreferences 本地存储
4+
class LocalStorage {
5+
static save(String key, value) async {
6+
SharedPreferences prefs = await SharedPreferences.getInstance();
7+
prefs.setInt(key, value);
8+
}
9+
10+
static get(String key) async {
11+
SharedPreferences prefs = await SharedPreferences.getInstance();
12+
return prefs.get(key);
13+
}
14+
15+
static remove(String key) async {
16+
SharedPreferences prefs = await SharedPreferences.getInstance();
17+
prefs.remove(key);
18+
}
19+
}

‎lib/common/net/Api.dart‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import 'dart:_http';
2+
3+
import 'package:dio/dio.dart';
4+
import 'package:gsy_github_app_flutter/common/net/Code.dart';
5+
6+
import 'dart:collection';
7+
8+
import 'package:gsy_github_app_flutter/common/config/Config.dart';
9+
import 'package:gsy_github_app_flutter/common/local/LocalStorage.dart';
10+
import 'package:gsy_github_app_flutter/common/net/ResultData.dart';
11+
12+
///http请求
13+
class HttpManager {
14+
static const CONTENT_TYPE_JSON = "application/json";
15+
static const CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";
16+
static Map optionParams = {
17+
"timeoutMs": 15000,
18+
"token": null,
19+
"authorizationCode": null,
20+
};
21+
22+
///发起网络请求
23+
///[ url] 请求url
24+
///[ params] 请求参数
25+
///[ header] 外加头
26+
///[ text] 是否text返回
27+
///[ option] 配置
28+
static netFetch(url, params, Map<String, String> header, text, Options option) async {
29+
30+
//todo 没有网络
31+
/*var isConnected = await NetInfo.isConnected.fetch().done;
32+
if (!isConnected) {
33+
return {
34+
result: false,
35+
code: Code.NETWORK_ERROR,
36+
msg: I18n('netError')
37+
}
38+
}*/
39+
40+
Map<String, String> headers = new HashMap();
41+
if (header != null) {
42+
headers.addAll(headers);
43+
}
44+
45+
//授权码
46+
if (optionParams["authorizationCode"] == null) {
47+
var authorizationCode = await getAuthorization();
48+
if (authorizationCode != null) {
49+
optionParams["authorizationCode"] = authorizationCode;
50+
}
51+
}
52+
53+
headers["Authorization"] = optionParams["authorizationCode"];
54+
55+
option.headers = headers;
56+
57+
Dio dio = new Dio();
58+
Response response = await dio.get(url, data: params, options: option);
59+
60+
if (Config.DEBUG) {
61+
print('请求url: ' + url);
62+
if (params != null) {
63+
print('请求参数: ' + params.toString());
64+
}
65+
if (response != null) {
66+
print('返回参数: ' + response.toString());
67+
}
68+
}
69+
70+
try {
71+
if (option.contentType != null && option.contentType == ContentType.TEXT) {
72+
return new ResultData(response.data, true, Code.SUCCESS);
73+
} else {
74+
var responseJson = await response.data;
75+
if (response.statusCode == 201 && responseJson.token != null) {
76+
optionParams["authorizationCode"] = 'token ' + responseJson.token;
77+
await LocalStorage.save(Config.TOKEN_KEY, optionParams["authorizationCode"]);
78+
}
79+
}
80+
if (response.statusCode == 200 || response.statusCode == 201) {
81+
return new ResultData(response.data, true, Code.SUCCESS, headers: response.headers);
82+
}
83+
} catch (e) {
84+
print(e.toString() + url);
85+
return new ResultData(response.data, false, response.statusCode, headers: response.headers);
86+
}
87+
return new ResultData(Code.errorHandleFunction(response.statusCode), false, response.statusCode);
88+
}
89+
90+
///清除授权
91+
static clearAuthorization() {
92+
optionParams["authorizationCode"] = null;
93+
LocalStorage.remove(Config.TOKEN_KEY);
94+
}
95+
96+
///获取授权token
97+
static getAuthorization() async {
98+
String token = await LocalStorage.get(Config.TOKEN_KEY);
99+
if (token == null) {
100+
String basic = await LocalStorage.get(Config.USER_BASIC_CODE);
101+
if (basic == null) {
102+
//提示输入账号密码
103+
} else {
104+
//通过 basic 去获取token,获取到设置,返回token
105+
return "Basic $basic";
106+
}
107+
} else {
108+
optionParams["authorizationCode"] = token;
109+
return token;
110+
}
111+
}
112+
}

‎lib/common/net/Code.dart‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
///错误编码
3+
class Code {
4+
//网络错误
5+
static const NETWORK_ERROR = 1;
6+
//网络超时
7+
static const NETWORK_TIMEOUT = 2;
8+
//网络返回数据格式化一次
9+
static const NETWORK_JSON_EXCEPTION = 3;
10+
11+
static const SUCCESS = 200;
12+
13+
static errorHandleFunction(code) {
14+
/*switch (code) {
15+
case 401:
16+
//授权逻辑
17+
if (Actions.currentScene !== 'LoginPage') {
18+
Actions.reset("LoginPage");
19+
}
20+
return "未授权或授权失败"; //401 Unauthorized
21+
case 403:
22+
Toast(I18n('noPower'));
23+
return "403权限错误";
24+
case 404:
25+
//Toast(I18n('notFound'));
26+
return "404错误";
27+
case 410:
28+
Toast(I18n('gone410'));
29+
return "410错误";
30+
case NETWORK_TIMEOUT:
31+
//超时
32+
Toast(I18n('netTimeout'));
33+
return I18n('netTimeout');
34+
default:
35+
if (statusText) {
36+
Toast(statusText);
37+
} else {
38+
Toast(I18n('errorUnKnow'));
39+
}
40+
return "其他异常"
41+
}*/
42+
}
43+
}
44+
45+

‎lib/common/net/ResultData.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class ResultData {
2+
var data;
3+
bool result;
4+
int code;
5+
var headers;
6+
7+
ResultData(this.data, this.result, this.code, {this.headers});
8+
}

‎pubspec.lock‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ packages:
5050
url: "https://pub.flutter-io.cn"
5151
source: hosted
5252
version: "2.0.1"
53+
cookie_jar:
54+
dependency: transitive
55+
description:
56+
name: cookie_jar
57+
url: "https://pub.flutter-io.cn"
58+
source: hosted
59+
version: "0.0.4"
5360
crypto:
5461
dependency: transitive
5562
description:
@@ -71,6 +78,13 @@ packages:
7178
url: "https://pub.flutter-io.cn"
7279
source: hosted
7380
version: "0.1.2"
81+
dio:
82+
dependency: "direct main"
83+
description:
84+
name: dio
85+
url: "https://pub.flutter-io.cn"
86+
source: hosted
87+
version: "0.0.14"
7488
flutter:
7589
dependency: "direct main"
7690
description: flutter
@@ -235,6 +249,13 @@ packages:
235249
url: "https://pub.flutter-io.cn"
236250
source: hosted
237251
version: "0.29.0+1"
252+
shared_preferences:
253+
dependency: "direct main"
254+
description:
255+
name: shared_preferences
256+
url: "https://pub.flutter-io.cn"
257+
source: hosted
258+
version: "0.4.2"
238259
shelf:
239260
dependency: transitive
240261
description:
@@ -368,3 +389,4 @@ packages:
368389
version: "2.1.13"
369390
sdks:
370391
dart: ">=2.0.0-dev.52.0 <=2.0.0-dev.58.0.flutter-f981f09760"
392+
flutter: ">=0.1.4 <2.0.0"

‎pubspec.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ dependencies:
88
# The following adds the Cupertino Icons font to your application.
99
# Use with the CupertinoIcons class for iOS style icons.
1010
cupertino_icons: ^0.1.2
11+
dio: ^0.0.14
12+
shared_preferences: ^0.4.2
1113

1214
dev_dependencies:
1315
flutter_test:

0 commit comments

Comments
 (0)