1-
21import 'dart:convert' ;
32
43import 'package:dio/dio.dart' ;
54import 'package:gsy_github_app_flutter/common/config/Config.dart' ;
65import 'package:gsy_github_app_flutter/common/config/ignoreConfig.dart' ;
6+ import 'package:gsy_github_app_flutter/common/dao/DaoResult.dart' ;
77import 'package:gsy_github_app_flutter/common/local/LocalStorage.dart' ;
8+ import 'package:gsy_github_app_flutter/common/model/User.dart' ;
89import 'package:gsy_github_app_flutter/common/net/Address.dart' ;
910import 'package:gsy_github_app_flutter/common/net/Api.dart' ;
1011
11-
1212class 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+ }
0 commit comments