This commit is contained in:
yifeng.yl
2019-05-07 15:41:22 +08:00
parent 4df5288940
commit 4ac0bc2ed6
12 changed files with 181 additions and 28 deletions

Binary file not shown.

View File

@ -1,5 +1,9 @@
class Api{
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';//退出登陆
}

View File

@ -10,13 +10,15 @@ import 'package:flutter_go/views/home.dart';
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 'views/welcome_page/index.dart';
const int ThemeColor = 0xFFC91B3A;
SpUtil sp;
var db;
class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
MyApp() {
final router = new Router();
@ -24,17 +26,33 @@ class MyApp extends StatelessWidget {
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() {
// 暂时关掉欢迎介绍
// return AppPage();
if (_hasLogin) {
return AppPage();
} else {
return LoginPage();
// bool showWelcome = sp.getBool(SharedPreferencesKeys.showWelcome);
// if (showWelcome == null || showWelcome == true) {
// return WelcomePage();
// } else {
// return AppPage();
// }
}
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
@ -52,9 +70,7 @@ class MyApp extends StatelessWidget {
size: 35.0,
),
),
home: new Scaffold(
body: showWelcomePage()
),
home: new Scaffold(body: showWelcomePage()),
debugShowCheckedModeBanner: false,
onGenerateRoute: Application.router.generator,
navigatorObservers: <NavigatorObserver>[Analytics.observer],
@ -62,7 +78,6 @@ class MyApp extends StatelessWidget {
}
}
void main() async {
final provider = new Provider();
await provider.init(true);

View File

@ -16,8 +16,8 @@ class UserInfo {
factory UserInfo.fromJson(Map<String, dynamic> json) {
return UserInfo(
avatarPic: json['avatar_pic'],
id: json['id'],
username: json['username'],
id: int.parse(json['id']),
username: json['name'],
themeColor: json['theme_color'],
urlName: json['url_name']);
}

View 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();
}
}

View File

@ -9,8 +9,22 @@ class DataUtils{
// 登陆获取用户信息
static Future<UserInfo> doLogin(Map<String,String> params) async{
var response = await NetUtils.post(Api.DO_LOGIN, params);
print('url:${Api.DO_LOGIN} $response');
UserInfo userInfo = UserInfo.fromJson(response['data']);
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'];
}
}

View File

@ -7,8 +7,14 @@ var dio = new Dio();
class NetUtils {
static Future get(String url,{Map<String,dynamic> params}) async{
var response = await dio.get(url, data: params);
static Future get(String url,[Map<String,dynamic> params]) async{
var response;
if(params != null){
response = await dio.get(url, data: params);
}else{
response = await dio.get(url);
}
return response.data;
}

View File

@ -1,4 +1,4 @@
import 'dart:async';
import 'package:sqflite/sqflite.dart';
@ -31,6 +31,10 @@ class Sql extends BaseModel {
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 {
if (conditions == null || conditions.isEmpty) {
return this.get();

View File

@ -57,7 +57,7 @@ class FirstPageState extends State<FirstPage> with AutomaticKeepAliveClientMixin
var pageTotal = 0;
try{
var response = await NetUtils.get(juejin_flutter, params: _param);
var response = await NetUtils.get(juejin_flutter, _param);
responseList = response['d']['entrylist'];
pageTotal = response['d']['total'];
if (!(pageTotal is int) || pageTotal <= 0) {

View File

@ -57,7 +57,7 @@ class SubPageState extends State<SubPage> with AutomaticKeepAliveClientMixin{
var pageTotal = 0;
try{
var response = await NetUtils.get(juejin_flutter, params: _param);
var response = await NetUtils.get(juejin_flutter, _param);
responseList = response['d']['entrylist'];
pageTotal = response['d']['total'];
if (!(pageTotal is int) || pageTotal <= 0) {

View File

@ -1,6 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.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 {
@override
@ -12,14 +15,40 @@ class _LoginPageState extends State<LoginPage> {
FocusNode _emailFocusNode = new FocusNode();
FocusNode _passwordFocusNode = new FocusNode();
FocusScopeNode _focusScopeNode = new FocusScopeNode();
UserInfoControlModel _userInfoControlModel = new UserInfoControlModel();
GlobalKey<FormState> _signInFormKey = new GlobalKey();
TextEditingController _userNameEditingController =
new TextEditingController();
TextEditingController _passwordEditingController =
new TextEditingController();
bool isShowPassWord = false;
String username = '';
String password = '';
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
Widget buildSignInTextForm() {
return new Container(
@ -39,6 +68,7 @@ class _LoginPageState extends State<LoginPage> {
padding: const EdgeInsets.only(
left: 25, right: 25, top: 20, bottom: 20),
child: new TextFormField(
controller: _userNameEditingController,
//关联焦点
focusNode: _emailFocusNode,
onEditingComplete: () {
@ -79,6 +109,7 @@ class _LoginPageState extends State<LoginPage> {
child: Padding(
padding: const EdgeInsets.only(left: 25, right: 25, top: 20),
child: new TextFormField(
controller: _passwordEditingController,
focusNode: _passwordFocusNode,
decoration: new InputDecoration(
icon: new Icon(
@ -153,11 +184,28 @@ class _LoginPageState extends State<LoginPage> {
setState(() {
isLoading = true;
});
DataUtils.doLogin({'username':username,'password':password}).then((result){
print(result.username);
DataUtils.doLogin({'username': username, 'password': password})
.then((result) {
setState(() {
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) {
print(onError);
setState(() {