Skip to content

Commit 4ac0bc2

Browse files
committed
登陆
1 parent 4df5288 commit 4ac0bc2

File tree

12 files changed

+181
-28
lines changed

12 files changed

+181
-28
lines changed

assets/app.db

4 KB
Binary file not shown.

lib/api/api.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
class Api{
22
static const String BASE_URL = 'http://127.0.0.1:6001/';
33

4-
static const String DO_LOGIN = BASE_URL+'doLogin';
4+
static const String DO_LOGIN = BASE_URL+'doLogin';//登陆
5+
6+
static const String CHECK_LOGIN = BASE_URL+'checkLogin';//验证登陆
7+
8+
static const String LOGOUT = BASE_URL+'logout';//退出登陆
59
}

lib/components/widget_demo.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class _WidgetDemoState extends State<WidgetDemo> {
101101
// 插入操作
102102
_collectionControl
103103
.insert(Collection(name: widget.title, router: _router))
104-
.then((result) {
104+
.then((result) {
105105
if (this.mounted) {
106106
setState(() {
107107
_hasCollected = true;

lib/main.dart

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,49 @@ import 'package:flutter_go/views/home.dart';
1010
import 'package:flutter_go/model/search_history.dart';
1111
import 'package:flutter_go/utils/analytics.dart' as Analytics;
1212
import 'package:flutter_go/views/login_page/login_page.dart';
13+
import 'package:flutter_go/utils/data_utils.dart';
14+
1315
//import 'views/welcome_page/index.dart';
1416

1517
const int ThemeColor = 0xFFC91B3A;
1618
SpUtil sp;
1719
var db;
1820

19-
class MyApp extends StatelessWidget {
20-
MyApp() {
21+
class MyApp extends StatefulWidget {
22+
MyApp() {
2123
final router = new Router();
2224

2325
Routes.configureRoutes(router);
2426

2527
Application.router = router;
2628
}
29+
30+
@override
31+
_MyAppState createState() => _MyAppState();
32+
}
33+
34+
class _MyAppState extends State<MyApp> {
35+
bool _hasLogin = false;
36+
37+
@override
38+
void initState() {
39+
super.initState();
40+
DataUtils.checkLogin().then((hasLogin) {
41+
setState(() {
42+
_hasLogin = hasLogin;
43+
});
44+
});
45+
}
46+
2747
showWelcomePage() {
2848
// 暂时关掉欢迎介绍
29-
// return AppPage();
30-
return LoginPage();
31-
// bool showWelcome = sp.getBool(SharedPreferencesKeys.showWelcome);
32-
// if (showWelcome == null || showWelcome == true) {
33-
// return WelcomePage();
34-
// } else {
35-
// return AppPage();
36-
// }
49+
if (_hasLogin) {
50+
return AppPage();
51+
} else {
52+
return LoginPage();
53+
}
3754
}
55+
3856
@override
3957
Widget build(BuildContext context) {
4058
return new MaterialApp(
@@ -52,17 +70,14 @@ class MyApp extends StatelessWidget {
5270
size: 35.0,
5371
),
5472
),
55-
home: new Scaffold(
56-
body: showWelcomePage()
57-
),
73+
home: new Scaffold(body: showWelcomePage()),
5874
debugShowCheckedModeBanner: false,
5975
onGenerateRoute: Application.router.generator,
6076
navigatorObservers: <NavigatorObserver>[Analytics.observer],
6177
);
6278
}
6379
}
6480

65-
6681
void main() async {
6782
final provider = new Provider();
6883
await provider.init(true);

lib/model/user_info.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class UserInfo {
1616
factory UserInfo.fromJson(Map<String, dynamic> json) {
1717
return UserInfo(
1818
avatarPic: json['avatar_pic'],
19-
id: json['id'],
20-
username: json['username'],
19+
id: int.parse(json['id']),
20+
username: json['name'],
2121
themeColor: json['theme_color'],
2222
urlName: json['url_name']);
2323
}

lib/model/user_info_cache.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/// @Author: 一凨
2+
/// @Date: 2019-01-07 16:24:42
3+
/// @Last Modified by: 一凨
4+
/// @Last Modified time: 2019-01-08 17:37:42
5+
6+
import 'dart:async';
7+
8+
import 'package:flutter_go/utils/sql.dart';
9+
10+
abstract class UserInfoInterface {
11+
String get username;
12+
String get password;
13+
}
14+
15+
class UserInfo implements UserInfoInterface {
16+
String username;
17+
String password;
18+
19+
UserInfo({this.username, this.password});
20+
21+
factory UserInfo.fromJSON(Map json){
22+
return UserInfo(username: json['username'],password: json['password']);
23+
}
24+
25+
Object toMap() {
26+
return {'username': username, 'password': password};
27+
}
28+
}
29+
30+
class UserInfoControlModel {
31+
final String table = 'userInfo';
32+
Sql sql;
33+
34+
UserInfoControlModel() {
35+
sql = Sql.setTable(table);
36+
}
37+
38+
// 获取所有的收藏
39+
40+
// 插入新的缓存
41+
Future insert(UserInfo userInfo) {
42+
var result =
43+
sql.insert({'username': userInfo.username, 'password': userInfo.password});
44+
return result;
45+
}
46+
47+
// 获取用户信息
48+
Future<List<UserInfo>> getAllInfo() async {
49+
List list = await sql.getByCondition();
50+
List<UserInfo> resultList = [];
51+
list.forEach((item){
52+
print(item);
53+
resultList.add(UserInfo.fromJSON(item));
54+
});
55+
return resultList;
56+
}
57+
58+
// 清空表中数据
59+
Future deleteAll() async{
60+
return await sql.deleteAll();
61+
}
62+
}

lib/utils/data_utils.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,22 @@ class DataUtils{
99
// 登陆获取用户信息
1010
static Future<UserInfo> doLogin(Map<String,String> params) async{
1111
var response = await NetUtils.post(Api.DO_LOGIN, params);
12-
print('url:${Api.DO_LOGIN} $response');
1312
UserInfo userInfo = UserInfo.fromJson(response['data']);
1413
return userInfo;
1514
}
15+
16+
// 验证登陆
17+
18+
static Future<bool> checkLogin() async{
19+
var response = await NetUtils.get(Api.CHECK_LOGIN);
20+
print('验证登陆:$response');
21+
return response['success'];
22+
}
23+
24+
// 退出登陆
25+
static Future<bool> logout() async{
26+
var response = await NetUtils.get(Api.LOGOUT);
27+
print('退出登陆 $response');
28+
return response['success'];
29+
}
1630
}

lib/utils/net_utils.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ var dio = new Dio();
77

88
class NetUtils {
99

10-
static Future get(String url,{Map<String,dynamic> params}) async{
11-
var response = await dio.get(url, data: params);
10+
static Future get(String url,[Map<String,dynamic> params]) async{
11+
var response;
12+
if(params != null){
13+
response = await dio.get(url, data: params);
14+
}else{
15+
response = await dio.get(url);
16+
}
17+
1218
return response.data;
1319
}
1420

lib/utils/sql.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'dart:async';
1+
22

33
import 'package:sqflite/sqflite.dart';
44

@@ -31,6 +31,10 @@ class Sql extends BaseModel {
3131
return await this.db.delete(tableName,where:'$key = ?',whereArgs:[value]);
3232
}
3333

34+
Future<int> deleteAll() async{
35+
return await this.db.delete(tableName);
36+
}
37+
3438
Future<List> getByCondition({Map<dynamic, dynamic> conditions}) async {
3539
if (conditions == null || conditions.isEmpty) {
3640
return this.get();

lib/views/first_page/first_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class FirstPageState extends State<FirstPage> with AutomaticKeepAliveClientMixin
5757
var pageTotal = 0;
5858

5959
try{
60-
var response = await NetUtils.get(juejin_flutter, params: _param);
60+
var response = await NetUtils.get(juejin_flutter, _param);
6161
responseList = response['d']['entrylist'];
6262
pageTotal = response['d']['total'];
6363
if (!(pageTotal is int) || pageTotal <= 0) {

0 commit comments

Comments
 (0)