Skip to content

Commit 58ef9c6

Browse files
committed
cookie 校验
1 parent 802980f commit 58ef9c6

File tree

8 files changed

+90
-18
lines changed

8 files changed

+90
-18
lines changed

android/app/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
4+
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
5+
<classpathentry kind="output" path="bin/default"/>
6+
</classpath>

android/app/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>app</name>
4+
<comment>Project app created by Buildship.</comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
connection.project.dir=..
2+
eclipse.preferences.version=1

lib/api/api.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Api{
2-
static const String BASE_URL = 'http://127.0.0.1:6001/';
2+
// static const String BASE_URL = 'http://127.0.0.1:6001/';
3+
static const String BASE_URL = 'http://flutter-go.alibaba.net/';
34

45
static const String DO_LOGIN = BASE_URL+'doLogin';//登陆
56

lib/main.dart

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:fluro/fluro.dart';
33
import 'package:flutter/rendering.dart';
44
import 'routers/routers.dart';
55
import 'routers/application.dart';
6-
6+
import 'package:flutter_spinkit/flutter_spinkit.dart';
77
import 'package:flutter_go/utils/provider.dart';
88
import 'package:flutter_go/utils/shared_preferences.dart';
99
import 'package:flutter_go/views/home.dart';
@@ -33,23 +33,40 @@ class MyApp extends StatefulWidget {
3333

3434
class _MyAppState extends State<MyApp> {
3535
bool _hasLogin = false;
36+
bool _isLoading = true;
3637

3738
@override
3839
void initState() {
3940
super.initState();
4041
DataUtils.checkLogin().then((hasLogin) {
4142
setState(() {
4243
_hasLogin = hasLogin;
44+
_isLoading = false;
45+
});
46+
}).catchError((onError){
47+
setState(() {
48+
_hasLogin = true;
49+
_isLoading = false;
4350
});
51+
print('身份信息验证失败:$onError');
4452
});
4553
}
4654

4755
showWelcomePage() {
48-
// 暂时关掉欢迎介绍
49-
if (_hasLogin) {
50-
return AppPage();
56+
if (_isLoading) {
57+
return Container(
58+
color: const Color(ThemeColor),
59+
child: Center(
60+
child: SpinKitPouringHourglass(color: Colors.white),
61+
),
62+
);
5163
} else {
52-
return LoginPage();
64+
// 判断是否已经登录
65+
if (_hasLogin) {
66+
return AppPage();
67+
} else {
68+
return LoginPage();
69+
}
5370
}
5471
}
5572

lib/utils/net_utils.dart

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
11
import 'dart:async';
2-
2+
import 'dart:io';
3+
import 'package:cookie_jar/cookie_jar.dart';
34
import 'package:dio/dio.dart';
5+
import 'package:path_provider/path_provider.dart';
46

7+
Map<String,dynamic> optHeader = {
8+
'accept-language':'zh-cn',
9+
'content-type':'application/json'
10+
};
511

6-
var dio = new Dio();
12+
var dio = new Dio(BaseOptions(connectTimeout: 30000,headers: optHeader));
713

814
class NetUtils {
9-
10-
static Future get(String url,[Map<String,dynamic> params]) async{
15+
16+
static Future get(String url, [Map<String, dynamic> params]) async {
1117
var response;
12-
if(params != null){
13-
response = await dio.get(url, data: params);
14-
}else{
18+
19+
// 设置代理 便于本地 charles 抓包
20+
// (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
21+
// (HttpClient client) {
22+
// client.findProxy = (uri) {
23+
// return "PROXY 30.10.26.193:8888";
24+
// };
25+
// };
26+
27+
Directory documentsDir = await getApplicationDocumentsDirectory();
28+
String documentsPath = documentsDir.path;
29+
var dir = new Directory("$documentsPath/cookies");
30+
await dir.create();
31+
// print('documentPath:${dir.path}');
32+
dio.interceptors.add(CookieManager(PersistCookieJar(dir: dir.path)));
33+
if (params != null) {
34+
response = await dio.get(url, queryParameters: params);
35+
} else {
1536
response = await dio.get(url);
1637
}
17-
18-
return response.data;
38+
return response.data;
1939
}
2040

21-
static Future post(String url,Map<String,dynamic> params) async{
41+
static Future post(String url, Map<String, dynamic> params) async {
2242
var response = await dio.post(url, data: params);
2343
return response.data;
2444
}
25-
}
45+
}

lib/views/login_page/login_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ class _LoginPageState extends State<LoginPage> {
186186
});
187187
DataUtils.doLogin({'username': username, 'password': password})
188188
.then((result) {
189+
print(result);
189190
setState(() {
190191
isLoading = false;
191192
});

pubspec.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ dependencies:
2828
# 本地存储、收藏功能
2929
shared_preferences: ^0.4.3
3030
flutter_spinkit: "^3.1.0"
31-
dio: ^1.0.6
31+
path_provider: ^1.0.0
32+
dio: ^2.0.15
3233
flutter_webview_plugin: ^0.3.4
34+
cookie_jar: ^1.0.0
3335
# 日期格式化
3436
intl: 0.15.7
3537
city_pickers: ^0.0.1

0 commit comments

Comments
 (0)