mirror of
https://github.com/alibaba/flutter-go.git
synced 2025-07-03 06:00:54 +08:00
登陆
This commit is contained in:
BIN
assets/app.db
BIN
assets/app.db
Binary file not shown.
@ -1,5 +1,9 @@
|
|||||||
class Api{
|
class Api{
|
||||||
static const String BASE_URL = 'http://127.0.0.1:6001/';
|
static const String BASE_URL = 'http://127.0.0.1:6001/';
|
||||||
|
|
||||||
static const String DO_LOGIN = BASE_URL+'doLogin';
|
static const String DO_LOGIN = BASE_URL+'doLogin';//登陆
|
||||||
|
|
||||||
|
static const String CHECK_LOGIN = BASE_URL+'checkLogin';//验证登陆
|
||||||
|
|
||||||
|
static const String LOGOUT = BASE_URL+'logout';//退出登陆
|
||||||
}
|
}
|
@ -10,13 +10,15 @@ import 'package:flutter_go/views/home.dart';
|
|||||||
import 'package:flutter_go/model/search_history.dart';
|
import 'package:flutter_go/model/search_history.dart';
|
||||||
import 'package:flutter_go/utils/analytics.dart' as Analytics;
|
import 'package:flutter_go/utils/analytics.dart' as Analytics;
|
||||||
import 'package:flutter_go/views/login_page/login_page.dart';
|
import 'package:flutter_go/views/login_page/login_page.dart';
|
||||||
|
import 'package:flutter_go/utils/data_utils.dart';
|
||||||
|
|
||||||
//import 'views/welcome_page/index.dart';
|
//import 'views/welcome_page/index.dart';
|
||||||
|
|
||||||
const int ThemeColor = 0xFFC91B3A;
|
const int ThemeColor = 0xFFC91B3A;
|
||||||
SpUtil sp;
|
SpUtil sp;
|
||||||
var db;
|
var db;
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatefulWidget {
|
||||||
MyApp() {
|
MyApp() {
|
||||||
final router = new Router();
|
final router = new Router();
|
||||||
|
|
||||||
@ -24,17 +26,33 @@ class MyApp extends StatelessWidget {
|
|||||||
|
|
||||||
Application.router = router;
|
Application.router = router;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
_MyAppState createState() => _MyAppState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _MyAppState extends State<MyApp> {
|
||||||
|
bool _hasLogin = false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
DataUtils.checkLogin().then((hasLogin) {
|
||||||
|
setState(() {
|
||||||
|
_hasLogin = hasLogin;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
showWelcomePage() {
|
showWelcomePage() {
|
||||||
// 暂时关掉欢迎介绍
|
// 暂时关掉欢迎介绍
|
||||||
// return AppPage();
|
if (_hasLogin) {
|
||||||
|
return AppPage();
|
||||||
|
} else {
|
||||||
return LoginPage();
|
return LoginPage();
|
||||||
// bool showWelcome = sp.getBool(SharedPreferencesKeys.showWelcome);
|
|
||||||
// if (showWelcome == null || showWelcome == true) {
|
|
||||||
// return WelcomePage();
|
|
||||||
// } else {
|
|
||||||
// return AppPage();
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return new MaterialApp(
|
return new MaterialApp(
|
||||||
@ -52,9 +70,7 @@ class MyApp extends StatelessWidget {
|
|||||||
size: 35.0,
|
size: 35.0,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
home: new Scaffold(
|
home: new Scaffold(body: showWelcomePage()),
|
||||||
body: showWelcomePage()
|
|
||||||
),
|
|
||||||
debugShowCheckedModeBanner: false,
|
debugShowCheckedModeBanner: false,
|
||||||
onGenerateRoute: Application.router.generator,
|
onGenerateRoute: Application.router.generator,
|
||||||
navigatorObservers: <NavigatorObserver>[Analytics.observer],
|
navigatorObservers: <NavigatorObserver>[Analytics.observer],
|
||||||
@ -62,7 +78,6 @@ class MyApp extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
final provider = new Provider();
|
final provider = new Provider();
|
||||||
await provider.init(true);
|
await provider.init(true);
|
||||||
|
@ -16,8 +16,8 @@ class UserInfo {
|
|||||||
factory UserInfo.fromJson(Map<String, dynamic> json) {
|
factory UserInfo.fromJson(Map<String, dynamic> json) {
|
||||||
return UserInfo(
|
return UserInfo(
|
||||||
avatarPic: json['avatar_pic'],
|
avatarPic: json['avatar_pic'],
|
||||||
id: json['id'],
|
id: int.parse(json['id']),
|
||||||
username: json['username'],
|
username: json['name'],
|
||||||
themeColor: json['theme_color'],
|
themeColor: json['theme_color'],
|
||||||
urlName: json['url_name']);
|
urlName: json['url_name']);
|
||||||
}
|
}
|
||||||
|
62
lib/model/user_info_cache.dart
Normal file
62
lib/model/user_info_cache.dart
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/// @Author: 一凨
|
||||||
|
/// @Date: 2019-01-07 16:24:42
|
||||||
|
/// @Last Modified by: 一凨
|
||||||
|
/// @Last Modified time: 2019-01-08 17:37:42
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter_go/utils/sql.dart';
|
||||||
|
|
||||||
|
abstract class UserInfoInterface {
|
||||||
|
String get username;
|
||||||
|
String get password;
|
||||||
|
}
|
||||||
|
|
||||||
|
class UserInfo implements UserInfoInterface {
|
||||||
|
String username;
|
||||||
|
String password;
|
||||||
|
|
||||||
|
UserInfo({this.username, this.password});
|
||||||
|
|
||||||
|
factory UserInfo.fromJSON(Map json){
|
||||||
|
return UserInfo(username: json['username'],password: json['password']);
|
||||||
|
}
|
||||||
|
|
||||||
|
Object toMap() {
|
||||||
|
return {'username': username, 'password': password};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class UserInfoControlModel {
|
||||||
|
final String table = 'userInfo';
|
||||||
|
Sql sql;
|
||||||
|
|
||||||
|
UserInfoControlModel() {
|
||||||
|
sql = Sql.setTable(table);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取所有的收藏
|
||||||
|
|
||||||
|
// 插入新的缓存
|
||||||
|
Future insert(UserInfo userInfo) {
|
||||||
|
var result =
|
||||||
|
sql.insert({'username': userInfo.username, 'password': userInfo.password});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取用户信息
|
||||||
|
Future<List<UserInfo>> getAllInfo() async {
|
||||||
|
List list = await sql.getByCondition();
|
||||||
|
List<UserInfo> resultList = [];
|
||||||
|
list.forEach((item){
|
||||||
|
print(item);
|
||||||
|
resultList.add(UserInfo.fromJSON(item));
|
||||||
|
});
|
||||||
|
return resultList;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清空表中数据
|
||||||
|
Future deleteAll() async{
|
||||||
|
return await sql.deleteAll();
|
||||||
|
}
|
||||||
|
}
|
@ -9,8 +9,22 @@ class DataUtils{
|
|||||||
// 登陆获取用户信息
|
// 登陆获取用户信息
|
||||||
static Future<UserInfo> doLogin(Map<String,String> params) async{
|
static Future<UserInfo> doLogin(Map<String,String> params) async{
|
||||||
var response = await NetUtils.post(Api.DO_LOGIN, params);
|
var response = await NetUtils.post(Api.DO_LOGIN, params);
|
||||||
print('url:${Api.DO_LOGIN} $response');
|
|
||||||
UserInfo userInfo = UserInfo.fromJson(response['data']);
|
UserInfo userInfo = UserInfo.fromJson(response['data']);
|
||||||
return userInfo;
|
return userInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 验证登陆
|
||||||
|
|
||||||
|
static Future<bool> checkLogin() async{
|
||||||
|
var response = await NetUtils.get(Api.CHECK_LOGIN);
|
||||||
|
print('验证登陆:$response');
|
||||||
|
return response['success'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 退出登陆
|
||||||
|
static Future<bool> logout() async{
|
||||||
|
var response = await NetUtils.get(Api.LOGOUT);
|
||||||
|
print('退出登陆 $response');
|
||||||
|
return response['success'];
|
||||||
|
}
|
||||||
}
|
}
|
@ -7,8 +7,14 @@ var dio = new Dio();
|
|||||||
|
|
||||||
class NetUtils {
|
class NetUtils {
|
||||||
|
|
||||||
static Future get(String url,{Map<String,dynamic> params}) async{
|
static Future get(String url,[Map<String,dynamic> params]) async{
|
||||||
var response = await dio.get(url, data: params);
|
var response;
|
||||||
|
if(params != null){
|
||||||
|
response = await dio.get(url, data: params);
|
||||||
|
}else{
|
||||||
|
response = await dio.get(url);
|
||||||
|
}
|
||||||
|
|
||||||
return response.data;
|
return response.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:sqflite/sqflite.dart';
|
import 'package:sqflite/sqflite.dart';
|
||||||
|
|
||||||
@ -31,6 +31,10 @@ class Sql extends BaseModel {
|
|||||||
return await this.db.delete(tableName,where:'$key = ?',whereArgs:[value]);
|
return await this.db.delete(tableName,where:'$key = ?',whereArgs:[value]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<int> deleteAll() async{
|
||||||
|
return await this.db.delete(tableName);
|
||||||
|
}
|
||||||
|
|
||||||
Future<List> getByCondition({Map<dynamic, dynamic> conditions}) async {
|
Future<List> getByCondition({Map<dynamic, dynamic> conditions}) async {
|
||||||
if (conditions == null || conditions.isEmpty) {
|
if (conditions == null || conditions.isEmpty) {
|
||||||
return this.get();
|
return this.get();
|
||||||
|
@ -57,7 +57,7 @@ class FirstPageState extends State<FirstPage> with AutomaticKeepAliveClientMixin
|
|||||||
var pageTotal = 0;
|
var pageTotal = 0;
|
||||||
|
|
||||||
try{
|
try{
|
||||||
var response = await NetUtils.get(juejin_flutter, params: _param);
|
var response = await NetUtils.get(juejin_flutter, _param);
|
||||||
responseList = response['d']['entrylist'];
|
responseList = response['d']['entrylist'];
|
||||||
pageTotal = response['d']['total'];
|
pageTotal = response['d']['total'];
|
||||||
if (!(pageTotal is int) || pageTotal <= 0) {
|
if (!(pageTotal is int) || pageTotal <= 0) {
|
||||||
|
@ -57,7 +57,7 @@ class SubPageState extends State<SubPage> with AutomaticKeepAliveClientMixin{
|
|||||||
var pageTotal = 0;
|
var pageTotal = 0;
|
||||||
|
|
||||||
try{
|
try{
|
||||||
var response = await NetUtils.get(juejin_flutter, params: _param);
|
var response = await NetUtils.get(juejin_flutter, _param);
|
||||||
responseList = response['d']['entrylist'];
|
responseList = response['d']['entrylist'];
|
||||||
pageTotal = response['d']['total'];
|
pageTotal = response['d']['total'];
|
||||||
if (!(pageTotal is int) || pageTotal <= 0) {
|
if (!(pageTotal is int) || pageTotal <= 0) {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_spinkit/flutter_spinkit.dart';
|
import 'package:flutter_spinkit/flutter_spinkit.dart';
|
||||||
import 'package:flutter_go/utils/data_utils.dart';
|
import 'package:flutter_go/utils/data_utils.dart';
|
||||||
|
import 'package:flutter_go/views/home.dart';
|
||||||
|
|
||||||
|
import 'package:flutter_go/model/user_info_cache.dart';
|
||||||
|
|
||||||
class LoginPage extends StatefulWidget {
|
class LoginPage extends StatefulWidget {
|
||||||
@override
|
@override
|
||||||
@ -12,14 +15,40 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
FocusNode _emailFocusNode = new FocusNode();
|
FocusNode _emailFocusNode = new FocusNode();
|
||||||
FocusNode _passwordFocusNode = new FocusNode();
|
FocusNode _passwordFocusNode = new FocusNode();
|
||||||
FocusScopeNode _focusScopeNode = new FocusScopeNode();
|
FocusScopeNode _focusScopeNode = new FocusScopeNode();
|
||||||
|
UserInfoControlModel _userInfoControlModel = new UserInfoControlModel();
|
||||||
|
|
||||||
GlobalKey<FormState> _signInFormKey = new GlobalKey();
|
GlobalKey<FormState> _signInFormKey = new GlobalKey();
|
||||||
|
TextEditingController _userNameEditingController =
|
||||||
|
new TextEditingController();
|
||||||
|
TextEditingController _passwordEditingController =
|
||||||
|
new TextEditingController();
|
||||||
|
|
||||||
bool isShowPassWord = false;
|
bool isShowPassWord = false;
|
||||||
String username = '';
|
String username = '';
|
||||||
String password = '';
|
String password = '';
|
||||||
bool isLoading = false;
|
bool isLoading = false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
try {
|
||||||
|
_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;
|
||||||
|
username = _userInfo.username;
|
||||||
|
password = _userInfo.password;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
print('读取用户本地存储的用户信息出错 $err');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 创建登录界面的TextForm
|
// 创建登录界面的TextForm
|
||||||
Widget buildSignInTextForm() {
|
Widget buildSignInTextForm() {
|
||||||
return new Container(
|
return new Container(
|
||||||
@ -39,6 +68,7 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
padding: const EdgeInsets.only(
|
padding: const EdgeInsets.only(
|
||||||
left: 25, right: 25, top: 20, bottom: 20),
|
left: 25, right: 25, top: 20, bottom: 20),
|
||||||
child: new TextFormField(
|
child: new TextFormField(
|
||||||
|
controller: _userNameEditingController,
|
||||||
//关联焦点
|
//关联焦点
|
||||||
focusNode: _emailFocusNode,
|
focusNode: _emailFocusNode,
|
||||||
onEditingComplete: () {
|
onEditingComplete: () {
|
||||||
@ -79,6 +109,7 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.only(left: 25, right: 25, top: 20),
|
padding: const EdgeInsets.only(left: 25, right: 25, top: 20),
|
||||||
child: new TextFormField(
|
child: new TextFormField(
|
||||||
|
controller: _passwordEditingController,
|
||||||
focusNode: _passwordFocusNode,
|
focusNode: _passwordFocusNode,
|
||||||
decoration: new InputDecoration(
|
decoration: new InputDecoration(
|
||||||
icon: new Icon(
|
icon: new Icon(
|
||||||
@ -153,11 +184,28 @@ class _LoginPageState extends State<LoginPage> {
|
|||||||
setState(() {
|
setState(() {
|
||||||
isLoading = true;
|
isLoading = true;
|
||||||
});
|
});
|
||||||
DataUtils.doLogin({'username':username,'password':password}).then((result){
|
DataUtils.doLogin({'username': username, 'password': password})
|
||||||
print(result.username);
|
.then((result) {
|
||||||
setState(() {
|
setState(() {
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
});
|
});
|
||||||
|
try {
|
||||||
|
_userInfoControlModel.deleteAll().then((result) {
|
||||||
|
// print('删除结果:$result');
|
||||||
|
_userInfoControlModel
|
||||||
|
.insert(UserInfo(password: password, username: username))
|
||||||
|
.then((value) {
|
||||||
|
// print('存储成功:$value');
|
||||||
|
Navigator.of(context).pushAndRemoveUntil(
|
||||||
|
new MaterialPageRoute(builder: (context) => AppPage()),
|
||||||
|
(route) => route == null);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
Navigator.of(context).pushAndRemoveUntil(
|
||||||
|
new MaterialPageRoute(builder: (context) => AppPage()),
|
||||||
|
(route) => route == null);
|
||||||
|
}
|
||||||
}).catchError((onError) {
|
}).catchError((onError) {
|
||||||
print(onError);
|
print(onError);
|
||||||
setState(() {
|
setState(() {
|
||||||
|
Reference in New Issue
Block a user