@@ -20,6 +20,7 @@ import 'package:collection/collection.dart' show IterableExtension;
20
20
import 'package:dan_xi/model/person.dart' ;
21
21
import 'package:dan_xi/repository/base_repository.dart' ;
22
22
import 'package:dan_xi/repository/fdu/uis_login_tool.dart' ;
23
+ import 'package:dan_xi/util/io/dio_utils.dart' ;
23
24
import 'package:dan_xi/util/public_extension_methods.dart' ;
24
25
import 'package:dan_xi/util/retrier.dart' ;
25
26
import 'package:dio/dio.dart' ;
@@ -28,7 +29,6 @@ import 'package:html/dom.dart';
28
29
import 'package:intl/intl.dart' ;
29
30
30
31
class CardRepository extends BaseRepositoryWithDio {
31
- PersonInfo ? _info;
32
32
static const String _LOGIN_URL =
33
33
"https://uis.fudan.edu.cn/authserver/login?service=https%3A%2F%2Fecard.fudan.edu.cn%2Fepay%2Fj_spring_cas_security_check" ;
34
34
static const String _USER_DETAIL_URL =
@@ -57,15 +57,9 @@ class CardRepository extends BaseRepositoryWithDio {
57
57
58
58
factory CardRepository .getInstance () => _instance;
59
59
60
- /// Log in before calling any method in this repository.
61
- Future <void > init (PersonInfo ? info) async {
62
- _info = info;
63
- await UISLoginTool .loginUIS (dio, _LOGIN_URL , cookieJar! , _info, true );
64
- }
65
-
66
- Future <String ?> getName () async {
67
- return (await loadCardInfo (- 1 ))? .name;
68
- }
60
+ Future <String ?> getName (PersonInfo ? info) async =>
61
+ UISLoginTool .tryAsyncWithAuth (dio, _LOGIN_URL , cookieJar! , info,
62
+ () async => (await _loadCardInfo (- 1 ))? .name);
69
63
70
64
Future <Iterable <CardRecord >> _loadOnePageCardRecord (
71
65
Map <String , String ?> requestData, int pageNum) async {
@@ -94,9 +88,14 @@ class CardRepository extends BaseRepositoryWithDio {
94
88
/// If [logDays] > 0, it will return records of recent [logDays] days;
95
89
/// If [logDays] = 0, it will return the latest records;
96
90
/// If [logDays] < 0, it will return null.
97
- Future <List <CardRecord >?> loadCardRecord (int logDays) async {
91
+ Future <List <CardRecord >?> loadCardRecord (
92
+ PersonInfo ? info, int logDays) async =>
93
+ UISLoginTool .tryAsyncWithAuth (
94
+ dio, _LOGIN_URL , cookieJar! , info, () => _loadCardRecord (logDays));
95
+
96
+ Future <List <CardRecord >?> _loadCardRecord (int logDays) async {
98
97
if (logDays < 0 ) return null ;
99
- //Get csrf id.
98
+ // Get csrf id.
100
99
Response <String > consumeCsrfPageResponse =
101
100
await dio.get (_CONSUME_DETAIL_CSRF_URL );
102
101
BeautifulSoup consumeCsrfPageSoup =
@@ -141,10 +140,19 @@ class CardRepository extends BaseRepositoryWithDio {
141
140
return list;
142
141
}
143
142
144
- Future <CardInfo ?> loadCardInfo (int logDays) async {
143
+ Future <CardInfo ?> loadCardInfo (PersonInfo ? info, int logDays) async =>
144
+ UISLoginTool .tryAsyncWithAuth (
145
+ dio, _LOGIN_URL , cookieJar! , info, () => _loadCardInfo (logDays));
146
+
147
+ Future <CardInfo ?> _loadCardInfo (int logDays) async {
145
148
var cardInfo = CardInfo ();
146
149
147
- //获取用户页面信息
150
+ // 2024-11-18 (@w568w): The ECard system is a little bit tricky, we need to
151
+ // obtain the ticket first. During this process, the correct JSESSIONID will be set.
152
+ final res = await dio.get (_LOGIN_URL ,
153
+ options: DioUtils .NON_REDIRECT_OPTION_WITH_FORM_TYPE );
154
+ await DioUtils .processRedirect (dio, res);
155
+
148
156
var userPageResponse = await dio.get (_USER_DETAIL_URL );
149
157
var soup = BeautifulSoup (userPageResponse.data.toString ());
150
158
@@ -153,13 +161,13 @@ class CardRepository extends BaseRepositoryWithDio {
153
161
cardInfo.cash = cashElement? .text.trim ();
154
162
cardInfo.name = nameElement? .text.between ("您好," , "!" )? .trim ();
155
163
List <CardRecord >? records =
156
- await Retrier .runAsyncWithRetry (() => loadCardRecord (logDays));
164
+ await Retrier .runAsyncWithRetry (() => _loadCardRecord (logDays));
157
165
cardInfo.records = records;
158
166
return cardInfo;
159
167
}
160
168
161
169
@override
162
- String get linkHost => "ecard. fudan.edu.cn" ;
170
+ String get linkHost => "fudan.edu.cn" ;
163
171
}
164
172
165
173
class CardInfo {
0 commit comments