mirror of
https://github.com/alibaba/flutter-go.git
synced 2025-05-17 21:05:56 +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{
|
||||
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';//退出登陆
|
||||
}
|
@ -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;
|
||||
|
@ -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(
|
||||
@ -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);
|
||||
|
@ -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']);
|
||||
}
|
||||
|
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{
|
||||
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'];
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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(
|
||||
@ -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;
|
||||
|
Reference in New Issue
Block a user