Skip to content

Commit eab8f82

Browse files
committed
修改登录逻辑
1 parent 4a81939 commit eab8f82

6 files changed

Lines changed: 126 additions & 13 deletions

File tree

‎lib/common/dao/UserDao.dart‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
import 'dart:convert';
3+
4+
import 'package:dio/dio.dart';
5+
import 'package:gsy_github_app_flutter/common/config/Config.dart';
6+
import 'package:gsy_github_app_flutter/common/config/ignoreConfig.dart';
7+
import 'package:gsy_github_app_flutter/common/local/LocalStorage.dart';
8+
import 'package:gsy_github_app_flutter/common/net/Address.dart';
9+
import 'package:gsy_github_app_flutter/common/net/Api.dart';
10+
11+
12+
class UserDao {
13+
14+
static login(userName, password, callback) async {
15+
String type= userName + ":" + password;
16+
var bytes = utf8.encode(type);
17+
var base64Str = base64.encode(bytes);
18+
if(Config.DEBUG) {
19+
print("base64Str login " + base64Str);
20+
}
21+
await LocalStorage.save(Config.USER_NAME_KEY, userName);
22+
await LocalStorage.save(Config.USER_BASIC_CODE, base64Str);
23+
24+
Map requestParams = {
25+
"scopes": ['user', 'repo', 'gist', 'notifications'],
26+
"note": "admin_script",
27+
"client_id": NetConfig.CLIENT_ID,
28+
"client_secret": NetConfig.CLIENT_SECRET
29+
};
30+
HttpManager.clearAuthorization();
31+
32+
var res = await HttpManager.netFetch(Address.getAuthorization(), requestParams, null, new Options(method: "post"));
33+
if (res != null && res.result) {
34+
await LocalStorage.save(Config.PW_KEY, password);
35+
//todo 登录成功后
36+
print("login result " + res.result.toString());
37+
print(res.data.toString());
38+
}
39+
if (callback != null) {
40+
callback(res.result);
41+
}
42+
}
43+
44+
}

‎lib/common/local/LocalStorage.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import 'package:shared_preferences/shared_preferences.dart';
22

33
///SharedPreferences 本地存储
44
class LocalStorage {
5+
56
static save(String key, value) async {
67
SharedPreferences prefs = await SharedPreferences.getInstance();
7-
prefs.setInt(key, value);
8+
prefs.setString(key, value);
89
}
910

1011
static get(String key) async {

‎lib/common/net/Api.dart‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'dart:_http';
1+
22

33
import 'package:dio/dio.dart';
44
import 'package:gsy_github_app_flutter/common/net/Code.dart';
@@ -25,7 +25,7 @@ class HttpManager {
2525
///[ header] 外加头
2626
///[ text] 是否text返回
2727
///[ option] 配置
28-
static netFetch(url, params, Map<String, String> header, text, Options option) async {
28+
static netFetch(url, params, Map<String, String> header, Options option) async {
2929

3030
//todo 没有网络
3131
/*var isConnected = await NetInfo.isConnected.fetch().done;
@@ -68,7 +68,7 @@ class HttpManager {
6868
}
6969

7070
try {
71-
if (option.contentType != null && option.contentType == ContentType.TEXT) {
71+
if (option.contentType != null && option.contentType.primaryType == "text") {
7272
return new ResultData(response.data, true, Code.SUCCESS);
7373
} else {
7474
var responseJson = await response.data;

‎lib/common/style/GSYStyle.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ class GSYConstant {}
3232

3333
class GSYStrings {
3434
static const String login_text = "登录";
35+
static const String login_username_hint_text = "请输入github用户名";
36+
static const String login_password_hint_text = "请输入密码";
3537
}

‎lib/page/LoginPage.dart‎

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
import 'package:flutter/material.dart';
2+
import 'package:gsy_github_app_flutter/common/config/Config.dart';
3+
import 'package:gsy_github_app_flutter/common/dao/UserDao.dart';
4+
import 'package:gsy_github_app_flutter/common/local/LocalStorage.dart';
25
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
36
import 'package:gsy_github_app_flutter/widget/GSYFlexButton.dart';
47
import 'package:gsy_github_app_flutter/widget/GSYInputWidget.dart';
58

6-
class LoginPage extends StatelessWidget {
9+
class LoginPage extends StatefulWidget {
10+
@override
11+
State createState() {
12+
return new _LoginPageState();
13+
}
14+
}
15+
16+
class _LoginPageState extends State<LoginPage> {
17+
var _userName = "";
18+
19+
var _password = "";
20+
21+
final TextEditingController userController = new TextEditingController();
22+
final TextEditingController pwController = new TextEditingController();
23+
24+
_LoginPageState() : super();
25+
26+
@override
27+
void initState() {
28+
super.initState();
29+
initParams();
30+
}
31+
32+
initParams() async {
33+
_userName = await LocalStorage.get(Config.USER_NAME_KEY);
34+
_password = await LocalStorage.get(Config.PW_KEY);
35+
userController.value = new TextEditingValue(text: _userName);
36+
pwController.value = new TextEditingValue(text: _password);
37+
}
38+
739
@override
840
Widget build(BuildContext context) {
941
return new Container(
@@ -22,15 +54,38 @@ class LoginPage extends StatelessWidget {
2254
children: <Widget>[
2355
new Image(image: new AssetImage('static/images/logo.png'), width: 90.0, height: 90.0),
2456
new Padding(padding: new EdgeInsets.all(10.0)),
25-
new GSYInputWidget(hintText: "11111", iconData: Icons.access_alarm),
57+
new GSYInputWidget(
58+
hintText: GSYStrings.login_username_hint_text,
59+
iconData: Icons.access_alarm,
60+
onChanged: (String value) {
61+
_userName = value;
62+
},
63+
controller: userController,
64+
),
2665
new Padding(padding: new EdgeInsets.all(10.0)),
27-
new GSYInputWidget(hintText: "11111", iconData: Icons.access_alarm),
66+
new GSYInputWidget(
67+
hintText: GSYStrings.login_password_hint_text,
68+
iconData: Icons.access_alarm,
69+
onChanged: (String value) {
70+
print(value);
71+
_password = value;
72+
},
73+
controller: pwController,
74+
),
2875
new Padding(padding: new EdgeInsets.all(30.0)),
2976
new GSYFlexButton(
3077
text: GSYStrings.login_text,
3178
color: Color(GSYColors.primaryValue),
3279
textColor: Color(GSYColors.textWhite),
33-
onPress: () {},
80+
onPress: () {
81+
if (_userName == null || _userName.length == 0) {
82+
return;
83+
}
84+
if (_password == null || _password.length == 0) {
85+
return;
86+
}
87+
UserDao.login(_userName, _password, (data) {});
88+
},
3489
)
3590
],
3691
),

‎lib/widget/GSYInputWidget.dart‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ class GSYInputWidget extends StatefulWidget {
66

77
final IconData iconData;
88

9-
GSYInputWidget({Key key, this.hintText, this.iconData}) : super(key: key);
9+
final ValueChanged<String> onChanged;
10+
11+
final TextStyle textStyle;
12+
13+
final TextEditingController controller;
14+
15+
GSYInputWidget({Key key, this.hintText, this.iconData, this.onChanged, this.textStyle, this.controller}) : super(key: key);
1016

1117
@override
12-
_GSYInputWidgetState createState() => new _GSYInputWidgetState(hintText, iconData);
18+
_GSYInputWidgetState createState() => new _GSYInputWidgetState(hintText, iconData, onChanged, textStyle, controller);
1319
}
1420

1521
/// State for [GSYInputWidget] widgets.
@@ -18,14 +24,19 @@ class _GSYInputWidgetState extends State<GSYInputWidget> {
1824

1925
final IconData iconData;
2026

21-
final TextEditingController _controller = new TextEditingController();
27+
final ValueChanged<String> onChanged;
28+
29+
final TextStyle textStyle;
30+
31+
final TextEditingController controller;
2232

23-
_GSYInputWidgetState(this.hintText, this.iconData) : super();
33+
_GSYInputWidgetState(this.hintText, this.iconData, this.onChanged, this.textStyle, this.controller) : super();
2434

2535
@override
2636
Widget build(BuildContext context) {
2737
return new TextField(
28-
controller: _controller,
38+
controller: controller,
39+
onChanged: onChanged,
2940
decoration: new InputDecoration(
3041
hintText: hintText,
3142
icon: new Icon(iconData),

0 commit comments

Comments
 (0)