Skip to content

Commit

Permalink
登陆
Browse files Browse the repository at this point in the history
  • Loading branch information
Nealyang committed May 7, 2019
1 parent 4df5288 commit 4ac0bc2
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 28 deletions.
Binary file modified assets/app.db
Binary file not shown.
6 changes: 5 additions & 1 deletion lib/api/api.dart
Original file line number Diff line number Diff line change
@@ -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';//退出登陆
}
2 changes: 1 addition & 1 deletion lib/components/widget_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class _WidgetDemoState extends State<WidgetDemo> {
// 插入操作
_collectionControl
.insert(Collection(name: widget.title, router: _router))
.then((result) {
.then((result) {
if (this.mounted) {
setState(() {
_hasCollected = true;
Expand Down
43 changes: 29 additions & 14 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,49 @@ 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 {
MyApp() {
class MyApp extends StatefulWidget {
MyApp() {
final router = new Router();

Routes.configureRoutes(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() {
// 暂时关掉欢迎介绍
// return AppPage();
return LoginPage();
// bool showWelcome = sp.getBool(SharedPreferencesKeys.showWelcome);
// if (showWelcome == null || showWelcome == true) {
// return WelcomePage();
// } else {
// return AppPage();
// }
if (_hasLogin) {
return AppPage();
} else {
return LoginPage();
}
}

@override
Widget build(BuildContext context) {
return new MaterialApp(
Expand All @@ -52,17 +70,14 @@ 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],
);
}
}


void main() async {
final provider = new Provider();
await provider.init(true);
Expand Down
4 changes: 2 additions & 2 deletions lib/model/user_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down
62 changes: 62 additions & 0 deletions lib/model/user_info_cache.dart
Original file line number Diff line number Diff line change
@@ -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();
}
}
16 changes: 15 additions & 1 deletion lib/utils/data_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}
10 changes: 8 additions & 2 deletions lib/utils/net_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 5 additions & 1 deletion lib/utils/sql.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:async';


import 'package:sqflite/sqflite.dart';

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion lib/views/first_page/first_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/views/first_page/sub_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
56 changes: 52 additions & 4 deletions lib/views/login_page/login_page.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand All @@ -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: () {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -148,17 +179,34 @@ class _LoginPageState extends State<LoginPage> {
}

// 登陆操作
doLogin() {
doLogin() {
_signInFormKey.currentState.save();
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;
});
}).catchError((onError){
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(() {
isLoading = false;
Expand Down

0 comments on commit 4ac0bc2

Please sign in to comment.