Skip to content

Commit fb74356

Browse files
committed
增加序列化模板
1 parent b02e77e commit fb74356

8 files changed

Lines changed: 298 additions & 18 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ class Strings{
2121
然后在你的代码中,你可以像访问你的字符串一样:new Text(Strings.welcomeMessage)
2222

2323

24-
welcome -> login
24+
welcome -> login
25+
26+
生成序列化模板
27+
flutter packages pub run build_runner build

‎lib/common/dao/DaoResult.dart‎

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

‎lib/common/dao/UserDao.dart‎

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
21
import 'dart:convert';
32

43
import 'package:dio/dio.dart';
54
import 'package:gsy_github_app_flutter/common/config/Config.dart';
65
import 'package:gsy_github_app_flutter/common/config/ignoreConfig.dart';
6+
import 'package:gsy_github_app_flutter/common/dao/DaoResult.dart';
77
import 'package:gsy_github_app_flutter/common/local/LocalStorage.dart';
8+
import 'package:gsy_github_app_flutter/common/model/User.dart';
89
import 'package:gsy_github_app_flutter/common/net/Address.dart';
910
import 'package:gsy_github_app_flutter/common/net/Api.dart';
1011

11-
1212
class UserDao {
13-
1413
static login(userName, password, callback) async {
15-
String type= userName + ":" + password;
14+
String type = userName + ":" + password;
1615
var bytes = utf8.encode(type);
1716
var base64Str = base64.encode(bytes);
18-
if(Config.DEBUG) {
17+
if (Config.DEBUG) {
1918
print("base64Str login " + base64Str);
2019
}
2120
await LocalStorage.save(Config.USER_NAME_KEY, userName);
@@ -29,16 +28,68 @@ class UserDao {
2928
};
3029
HttpManager.clearAuthorization();
3130

32-
var res = await HttpManager.netFetch(Address.getAuthorization(), requestParams, null, new Options(method: "post"));
31+
var res = await HttpManager.netFetch(Address.getAuthorization(), json.encode(requestParams), null, new Options(method: "post"));
3332
if (res != null && res.result) {
34-
await LocalStorage.save(Config.PW_KEY, password);
33+
await LocalStorage.save(Config.PW_KEY, password);
34+
var resultData = await getUserInfo(null);
3535
//todo 登录成功后
36-
print("login result " + res.result.toString());
37-
print(res.data.toString());
36+
if(Config.DEBUG) {
37+
print("user result " + resultData.result.toString());
38+
print(resultData.data);
39+
print(res.data.toString());
40+
}
3841
}
3942
if (callback != null) {
4043
callback(res);
4144
}
4245
}
4346

44-
}
47+
///初始化用户信息
48+
static initUserInfo() async {
49+
var token = await LocalStorage.get(Config.TOKEN_KEY);
50+
var res = await getUserInfoLocal();
51+
if (res && res.result && token) {
52+
//todo store
53+
}
54+
return new DataResult(res.data, (res.result && (token != null)));
55+
}
56+
57+
///获取本地登录用户信息
58+
static getUserInfoLocal() async {
59+
var userText = await LocalStorage.get(Config.USER_INFO);
60+
if (userText != null) {
61+
var res = json.decode(userText);
62+
return new DataResult(res.data, true);
63+
} else {
64+
return new DataResult(null, false);
65+
}
66+
}
67+
68+
///获取用户详细信息
69+
static getUserInfo(userName) async {
70+
var res;
71+
if (userName == null) {
72+
res = await HttpManager.netFetch(Address.getMyUserInfo(), null, null, null);
73+
} else {
74+
res = await HttpManager.netFetch(Address.getUserInfo(userName), null, null, null);
75+
}
76+
if (res != null && res.result) {
77+
/*
78+
let countRes = await getUserStaredCountNet(res.data.login);
79+
let starred = "---";
80+
if (countRes.result) {
81+
starred = countRes.data;
82+
}
83+
let totalInfo = Object.assign({}, res.data, {starred: starred});
84+
*/
85+
User user = User.fromJson(res.data);
86+
user.starred = "---";
87+
if (userName == null) {
88+
LocalStorage.save(Config.USER_INFO, json.encode(res.data));
89+
}
90+
return new DataResult(user, true);
91+
} else {
92+
return new DataResult(res.data, false);
93+
}
94+
}
95+
}

‎lib/common/model/User.dart‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
part 'User.g.dart';
4+
5+
@JsonSerializable()
6+
class User extends Object with _$UserSerializerMixin {
7+
User(
8+
this.login,
9+
this.id,
10+
this.node_id,
11+
this.avatar_url,
12+
this.gravatar_id,
13+
this.url,
14+
this.html_url,
15+
this.followers_url,
16+
this.following_url,
17+
this.gists_url,
18+
this.starred_url,
19+
this.subscriptions_url,
20+
this.organizations_url,
21+
this.repos_url,
22+
this.events_url,
23+
this.received_events_url,
24+
this.type,
25+
this.site_admin,
26+
this.name,
27+
this.company,
28+
this.blog,
29+
this.location,
30+
this.email,
31+
this.hireable,
32+
this.starred,
33+
this.bio,
34+
this.public_repos,
35+
this.public_gists,
36+
this.followers,
37+
this.following,
38+
this.created_at,
39+
this.updated_at,
40+
this.private_gists,
41+
this.total_private_repos,
42+
this.owned_private_repos,
43+
this.disk_usage,
44+
this.collaborators,
45+
this.two_factor_authentication);
46+
47+
String login;
48+
int id;
49+
String node_id;
50+
String avatar_url;
51+
String gravatar_id;
52+
String url;
53+
String html_url;
54+
String followers_url;
55+
String following_url;
56+
String gists_url;
57+
String starred_url;
58+
String subscriptions_url;
59+
String organizations_url;
60+
String repos_url;
61+
String events_url;
62+
String received_events_url;
63+
String type;
64+
bool site_admin;
65+
String name;
66+
String company;
67+
String blog;
68+
String location;
69+
String email;
70+
String hireable;
71+
String starred;
72+
String bio;
73+
int public_repos;
74+
int public_gists;
75+
int followers;
76+
int following;
77+
DateTime created_at;
78+
DateTime updated_at;
79+
int private_gists;
80+
int total_private_repos;
81+
int owned_private_repos;
82+
int disk_usage;
83+
int collaborators;
84+
bool two_factor_authentication;
85+
86+
/// A necessary factory constructor for creating a new User instance
87+
/// from a map. We pass the map to the generated _$UserFromJson constructor.
88+
/// The constructor is named after the source class, in this case User.
89+
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
90+
}

‎lib/common/net/Api.dart‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:io';
2+
13
import 'package:dio/dio.dart';
24
import 'package:gsy_github_app_flutter/common/net/Code.dart';
35

@@ -49,12 +51,17 @@ class HttpManager {
4951

5052
headers["Authorization"] = optionParams["authorizationCode"];
5153

52-
option.headers = headers;
54+
if (option != null) {
55+
option.headers = headers;
56+
} else{
57+
option = new Options(method: "get");
58+
option.headers = headers;
59+
}
5360

5461
Dio dio = new Dio();
5562
Response response;
5663
try {
57-
response = await dio.get(url, data: params, options: option);
64+
response = await dio.request(url, data: params, options: option);
5865
} on DioError catch (e) {
5966
Response errorResponse;
6067
if (e.response != null) {
@@ -85,9 +92,9 @@ class HttpManager {
8592
if (option.contentType != null && option.contentType.primaryType == "text") {
8693
return new ResultData(response.data, true, Code.SUCCESS);
8794
} else {
88-
var responseJson = await response.data;
89-
if (response.statusCode == 201 && responseJson.token != null) {
90-
optionParams["authorizationCode"] = 'token ' + responseJson.token;
95+
var responseJson = response.data;
96+
if (response.statusCode == 201 && responseJson["token"] != null) {
97+
optionParams["authorizationCode"] = 'token ' + responseJson["token"];
9198
await LocalStorage.save(Config.TOKEN_KEY, optionParams["authorizationCode"]);
9299
}
93100
}

‎lib/page/WelcomePage.dart‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,31 @@ import 'package:gsy_github_app_flutter/page/LoginPage.dart';
77
class WelcomePage extends StatelessWidget {
88
@override
99
Widget build(BuildContext context) {
10+
11+
toNext(res) {
12+
Widget widget;
13+
if (res != null && res.result) {
14+
widget = new HomePage();
15+
} else {
16+
widget = new LoginPage();
17+
}
18+
Navigator.of(context).pushReplacement(new MaterialPageRoute(builder: (context) {
19+
//判断是去登录还是主页
20+
return widget;
21+
}));
22+
}
1023
// 一秒以后将任务添加至event队列
1124
new Future.delayed(const Duration(seconds: 2), () {
1225
Navigator.of(context).pushReplacement(new MaterialPageRoute(builder: (context) {
26+
//判断是去登录还是主页
1327
return new LoginPage();
1428
}));
1529
});
30+
1631
return new Container(
1732
color: Colors.white,
1833
child: new Center(
19-
child: new Text("李彦宏传闻辟谣", style: new TextStyle(color: Colors.black, fontSize: 22.0)),
34+
child: new Text("Welcome", style: new TextStyle(color: Colors.black, fontSize: 22.0)),
2035
),
2136
);
2237
}

0 commit comments

Comments
 (0)