添加 github Oauth ,个人中心

This commit is contained in:
yifeng.yl
2019-05-30 20:04:01 +08:00
parent dbcdbab7d2
commit 617e2953b3
13 changed files with 294 additions and 91 deletions

BIN
assets/images/arrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@ -1,5 +1,5 @@
class Api{
static const String BASE_URL = 'http://30.10.29.190:6001/';
static const String BASE_URL = 'http://30.10.25.17:6001/';
// static const String BASE_URL = 'http://flutter-go.alibaba.net/';
static const String DO_LOGIN = BASE_URL+'doLogin';//登陆

View File

@ -11,6 +11,7 @@ import 'package:flutter_go/model/search_history.dart';
import 'package:flutter_go/utils/analytics.dart' as Analytics;
import 'package:flutter_go/views/login_page/login_page.dart';
import 'package:flutter_go/utils/data_utils.dart';
import 'package:flutter_go/model/user_info.dart';
//import 'views/welcome_page/index.dart';
@ -34,18 +35,27 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
bool _hasLogin = false;
bool _isLoading = true;
UserInformation _userInfo;
@override
void initState() {
super.initState();
DataUtils.checkLogin().then((hasLogin) {
if (hasLogin.runtimeType == UserInformation) {
setState(() {
_hasLogin = true;
_isLoading = false;
_userInfo = hasLogin;
});
} else {
setState(() {
_hasLogin = hasLogin;
_isLoading = false;
});
}
}).catchError((onError) {
setState(() {
_hasLogin = hasLogin;
_isLoading = false;
});
}).catchError((onError){
setState(() {
_hasLogin = true;
_hasLogin = false;
_isLoading = false;
});
print('身份信息验证失败:$onError');
@ -63,7 +73,7 @@ class _MyAppState extends State<MyApp> {
} else {
// 判断是否已经登录
if (_hasLogin) {
return AppPage();
return AppPage(_userInfo);
} else {
return LoginPage();
}

View File

@ -1,24 +1,25 @@
class UserInfo {
class UserInformation {
String username;
int id;
String avatarPic;
String themeColor;
String urlName;
UserInfo({
UserInformation({
this.avatarPic,
this.id,
this.themeColor,
this.urlName,
this.username,
});
factory UserInfo.fromJson(Map<String, dynamic> json) {
return UserInfo(
factory UserInformation.fromJson(Map<String, dynamic> json) {
String name = json['name'];
if(json['name'] == null){
name = json['url_name'];
}
return UserInformation(
avatarPic: json['avatar_pic'],
id: int.parse(json['id']),
username: json['name'],
themeColor: json['theme_color'],
urlName: json['url_name']);
username: name,
themeColor: json['theme_color']);
}
}

View File

@ -7,11 +7,12 @@ import 'package:flutter_go/components/full_screen_code_dialog.dart';
import 'package:flutter_go/views/web_page/web_view_page.dart';
import 'package:flutter_go/views/home.dart';
import 'package:flutter_go/views/login_page/login_page.dart';
import 'package:flutter_go/model/user_info.dart';
// app的首页
var homeHandler = new Handler(
handlerFunc: (BuildContext context, Map<String, List<String>> params) {
return new AppPage();
return new AppPage(UserInformation(id: 0));
},
);

View File

@ -8,29 +8,38 @@ class DataUtils {
// 登陆获取用户信息
static Future doLogin(Map<String, String> params) async {
var response = await NetUtils.post(Api.DO_LOGIN, params);
print(response);
try {
UserInfo userInfo = UserInfo.fromJson(response['data']);
UserInformation userInfo = UserInformation.fromJson(response['data']);
return userInfo;
} catch (err) {
return response['data'];
return response['message'];
}
}
// 获取用户信息
static Future<UserInfo> getUserInfo(Map<String, String> params) async {
static Future<UserInformation> getUserInfo(Map<String, String> params) async {
var response = await NetUtils.get(Api.GET_USER_INFO, params);
print(response);
UserInfo userInfo = UserInfo.fromJson(response['data']);
return userInfo;
try {
UserInformation userInfo = UserInformation.fromJson(response['data']);
return userInfo;
} catch (err) {
return response['message'];
}
}
// 验证登陆
static Future<bool> checkLogin() async {
static Future checkLogin() async {
var response = await NetUtils.get(Api.CHECK_LOGIN);
print('验证登陆:$response');
return response['success'];
try {
if (response['success']) {
UserInformation userInfo = UserInformation.fromJson(response['data']);
return userInfo;
} else {
return response['success'];
}
} catch (err) {
return response['message'];
}
}
// 退出登陆

View File

@ -17,12 +17,12 @@ class NetUtils {
var response;
// 设置代理 便于本地 charles 抓包
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.findProxy = (uri) {
return "PROXY 30.10.29.190:8888";
};
};
// (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
// (HttpClient client) {
// client.findProxy = (uri) {
// return "PROXY 30.10.25.17:8888";
// };
// };
Directory documentsDir = await getApplicationDocumentsDirectory();
String documentsPath = documentsDir.path;

View File

@ -0,0 +1,130 @@
import 'package:flutter/material.dart';
import 'package:flutter_go/model/user_info.dart';
import 'package:share/share.dart';
class DrawerPage extends StatefulWidget {
final UserInformation userInfo;
DrawerPage({Key key, this.userInfo}) : super(key: key);
@override
_DrawerPageState createState() => _DrawerPageState();
}
class _DrawerPageState extends State<DrawerPage> {
final TextStyle textStyle =
TextStyle(fontSize: 16, fontWeight: FontWeight.w300);
@override
Widget build(BuildContext context) {
return ListView(
padding: EdgeInsets.zero,
children: <Widget>[
UserAccountsDrawerHeader(
accountName: Text(''),
accountEmail: Container(
padding: const EdgeInsets.only(bottom: 20.0),
child: Text(
widget.userInfo.username,
style: TextStyle(fontSize: 28),
),
),
decoration: BoxDecoration(
image: new DecorationImage(
fit: BoxFit.cover,
image: new NetworkImage(widget.userInfo.avatarPic),
),
),
),
ListTile(
leading: Icon(
Icons.search,
size: 27.0,
color: Theme.of(context).primaryColor,
),
title: Text(
'搜索',
style: textStyle,
),
onTap: () {},
),
// new Divider(),
ListTile(
leading: Icon(
Icons.favorite,
size: 27.0,
),
title: Text(
'我的收藏',
style: textStyle,
),
onTap: () {},
),
// new Divider(),
ListTile(
leading: Icon(
Icons.settings,
size: 27.0,
),
title: Text(
'更多设置',
style: textStyle,
),
onTap: () {},
),
new Divider(),
ListTile(
leading: Icon(
Icons.email,
size: 27.0,
),
title: Text(
'反馈/建议',
style: textStyle,
),
onTap: () {},
),
ListTile(
leading: Icon(
Icons.info,
size: 27.0,
),
title: Text(
'关于 App',
style: textStyle,
),
onTap: () {},
),
ListTile(
leading: Icon(
Icons.share,
size: 27.0,
),
title: Text(
'分享 App',
style: textStyle,
),
onTap: () {
Share.share('https://github.com/alibaba/flutter-go');
},
),
new Divider(),
ListTile(
leading: Icon(
Icons.exit_to_app,
size: 27.0,
),
title: Text(
'登出',
style: textStyle,
),
onTap: () {
Share.share('check out my website https://example.com');
},
),
],
);
}
}

View File

@ -1,9 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_go/views/first_page/drawer_page.dart';
import './first_page.dart';
import './sub_page.dart';
import './main_app_bar.dart';
import './search_page.dart';
import 'package:flutter_go/model/user_info.dart';
class _Page {
final String labelId;
@ -19,6 +21,10 @@ final List<_Page> _allPages = <_Page>[
];
class MainPage extends StatelessWidget {
final UserInformation userInfo;
MainPage({Key key, this.userInfo}) : super(key: key);
@override
Widget build(BuildContext context) {
print("MainPagess build......");
@ -29,7 +35,7 @@ class MainPage extends StatelessWidget {
leading: Container(
child: new ClipOval(
child: Image.network(
'https://hbimg.huabanimg.com/9bfa0fad3b1284d652d370fa0a8155e1222c62c0bf9d-YjG0Vt_fw658',
userInfo.avatarPic,
scale: 15.0,
),
)),
@ -44,17 +50,8 @@ class MainPage extends StatelessWidget {
],
),
drawer: Drawer(
child: new ListView(
children: <Widget>[
new ListTile(
title: new Text("欢迎"),
),
new Divider(),
new ListTile(
title: new Text("设置"),
trailing: new Icon(Icons.settings),
onTap: () {}),
],
child: DrawerPage(
userInfo: userInfo,
),
),
body: TabBarViewLayout(),

View File

@ -20,10 +20,16 @@ import 'package:flutter_go/widgets/index.dart';
import 'package:flutter_go/components/search_input.dart';
import 'package:flutter_go/model/search_history.dart';
import 'package:flutter_go/resources/widget_name_to_icon.dart';
import 'package:flutter_go/model/user_info.dart';
const int ThemeColor = 0xFFC91B3A;
class AppPage extends StatefulWidget {
final UserInformation userInfo;
AppPage(this.userInfo);
@override
State<StatefulWidget> createState() {
return _MyHomePageState();
@ -32,6 +38,8 @@ class AppPage extends StatefulWidget {
class _MyHomePageState extends State<AppPage>
with SingleTickerProviderStateMixin {
SpUtil sp;
WidgetControlModel widgetControl = new WidgetControlModel();
SearchHistoryList searchHistoryList;
@ -62,7 +70,7 @@ class _MyHomePageState extends State<AppPage>
}
list
// ..add(FirstPage())
..add(MainPage())
..add(MainPage(userInfo:widget.userInfo))
..add(WidgetPage(Provider.db))
..add(CollectionPage())
..add(FourthPage());
@ -98,7 +106,7 @@ class _MyHomePageState extends State<AppPage>
}
Widget buildSearchInput(BuildContext context) {
return new SearchInput((value) async {
return new SearchInput((value) async {
if (value != '') {
List<WidgetPoint> list = await widgetControl.search(value);
return list
@ -107,7 +115,7 @@ class _MyHomePageState extends State<AppPage>
icon: WidgetName2Icon.icons[item.name] ?? null,
text: 'widget',
onTap: () {
onWidgetTap(item, context);
onWidgetTap(item, context);
},
))
.toList();
@ -117,9 +125,9 @@ class _MyHomePageState extends State<AppPage>
}, (value) {}, () {});
}
renderAppBar(BuildContext context,Widget widget,int index) {
renderAppBar(BuildContext context, Widget widget, int index) {
print('renderAppBar=====>>>>>>${index}');
if(index == 0) {
if (index == 0) {
return null;
}
return AppBar(title: buildSearchInput(context));
@ -128,7 +136,7 @@ class _MyHomePageState extends State<AppPage>
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: renderAppBar(context,widget,_currentIndex),
appBar: renderAppBar(context, widget, _currentIndex),
body: list[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
items: myTabs,

View File

@ -11,6 +11,7 @@ import 'package:flutter_go/event/event_model.dart';
import 'package:flutter_go/model/user_info_cache.dart';
import 'package:flutter_go/routers/application.dart';
import 'package:flutter_go/routers/routers.dart';
import 'package:flutter_go/model/user_info.dart';
class LoginPage extends StatefulWidget {
@override
@ -47,7 +48,6 @@ class _LoginPageState extends State<LoginPage> {
_userInfoControlModel.getAllInfo().then((list) {
if (list.length > 0) {
UserInfo _userInfo = list[0];
print('获取的数据:${_userInfo.username} ${_userInfo.password}');
setState(() {
_userNameEditingController.text = _userInfo.username;
_passwordEditingController.text = _userInfo.password;
@ -62,7 +62,6 @@ class _LoginPageState extends State<LoginPage> {
ApplicationEvent.event.on<UserGithubOAuthEvent>().listen((event) {
if (event.isSuccess == true) {
print('请求接口');
// oAuth 认证成功
setState(() {
isLoading = true;
@ -73,7 +72,7 @@ class _LoginPageState extends State<LoginPage> {
isLoading = false;
});
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => AppPage()),
MaterialPageRoute(builder: (context) => AppPage(result)),
(route) => route == null);
}).catchError((onError) {
print('获取身份信息 error:::$onError');
@ -81,9 +80,16 @@ class _LoginPageState extends State<LoginPage> {
isLoading = false;
});
});
} else {
Fluttertoast.showToast(
msg: '验证失败',
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIos: 1,
backgroundColor: Theme.of(context).primaryColor,
textColor: Colors.white,
fontSize: 16.0);
}
print('这是接受到的 event ${event.isSuccess} ${event.loginName}');
// _getList();
});
}
@ -223,10 +229,10 @@ class _LoginPageState extends State<LoginPage> {
isLoading = true;
});
DataUtils.doLogin({'username': username, 'password': password})
.then((result) {
if(result.runtimeType == String){
throw result;
}
.then((userResult) {
if (userResult.runtimeType == String) {
throw userResult;
}
setState(() {
isLoading = false;
});
@ -236,15 +242,15 @@ class _LoginPageState extends State<LoginPage> {
_userInfoControlModel
.insert(UserInfo(password: password, username: username))
.then((value) {
// print('存储成功:$value');
print('存储成功:$value');
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => AppPage()),
MaterialPageRoute(builder: (context) => AppPage(userResult)),
(route) => route == null);
});
});
} catch (err) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => AppPage()),
MaterialPageRoute(builder: (context) => AppPage(userResult)),
(route) => route == null);
}
}).catchError((errorMsg) {
@ -252,14 +258,13 @@ class _LoginPageState extends State<LoginPage> {
isLoading = false;
});
Fluttertoast.showToast(
msg: errorMsg.toString(),
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIos: 1,
backgroundColor: Theme.of(context).primaryColor,
textColor: Colors.white,
fontSize: 16.0
);
msg: errorMsg.toString(),
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIos: 1,
backgroundColor: Theme.of(context).primaryColor,
textColor: Colors.white,
fontSize: 16.0);
});
}
@ -315,11 +320,28 @@ class _LoginPageState extends State<LoginPage> {
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(height: 35.0),
Image.asset(
'assets/images/FlutterGo.png',
fit: BoxFit.contain,
width: 100.0,
height: 100.0,
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset(
'assets/images/gitHub.png',
fit: BoxFit.contain,
width: 60.0,
height: 60.0,
),
Image.asset(
'assets/images/arrow.png',
fit: BoxFit.contain,
width: 40.0,
height: 30.0,
),
Image.asset(
'assets/images/FlutterGo.png',
fit: BoxFit.contain,
width: 60.0,
height: 60.0,
),
],
),
buildSignInTextForm(),
buildSignInButton(),
@ -330,19 +352,36 @@ class _LoginPageState extends State<LoginPage> {
color: Colors.grey[400],
margin: const EdgeInsets.only(bottom: 10.0),
),
FlatButton(
child: Text(
'Github OAuth 认证',
style: TextStyle(
color: Theme.of(context).primaryColor,
decoration: TextDecoration.underline),
),
onPressed: () {
// _launchURL('https://github.com/login/oauth/authorize?client_id=cfe4795e76382ae8a5bd&scope=user,public_repo');
Application.router.navigateTo(context,
'${Routes.webViewPage}?title=Github&url=${Uri.encodeComponent("https://github.com/login/oauth/authorize?client_id=cfe4795e76382ae8a5bd&scope=user,public_repo")}');
},
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
child: Text(
'Github OAuth 认证',
style: TextStyle(
color: Theme.of(context).primaryColor,
decoration: TextDecoration.underline),
),
onPressed: () {
Application.router.navigateTo(context,
'${Routes.webViewPage}?title=Github&url=${Uri.encodeComponent("https://github.com/login/oauth/authorize?client_id=cfe4795e76382ae8a5bd&scope=user,public_repo")}');
},
),
FlatButton(
child: Text(
'游客登录',
style: TextStyle(
color: Theme.of(context).primaryColor,
decoration: TextDecoration.underline),
),
onPressed: () {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => AppPage(UserInformation(id: 0,avatarPic: 'https://hbimg.huabanimg.com/9bfa0fad3b1284d652d370fa0a8155e1222c62c0bf9d-YjG0Vt_fw658'))),
(route) => route == null);
},
)
],
)
],
),

View File

@ -235,6 +235,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.21.0"
share:
dependency: "direct main"
description:
name: share
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.6.1+1"
shared_preferences:
dependency: "direct main"
description:

View File

@ -27,6 +27,7 @@ dependencies:
url_launcher: ^5.0.2
# 本地存储、收藏功能
shared_preferences: ^0.4.3
share: ^0.6.1+1
flutter_spinkit: "^3.1.0"
path_provider: ^1.0.0
fluttertoast: ^3.1.0