Skip to content

Commit 62b83da

Browse files
authored
Merge pull request #436 from DanXi-Dev/fix-426
Fix 426
2 parents 65c6ec3 + 5843ced commit 62b83da

23 files changed

+130
-262
lines changed

lib/feature/bus_feature.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class BusFeature extends Feature {
6666
_loadBusList(PersonInfo? personInfo) async {
6767
_status = ConnectionStatus.CONNECTING;
6868
_busList = await FudanBusRepository.getInstance()
69-
.loadBusList(personInfo, holiday: isHoliday);
69+
.loadBusList(personInfo, holiday: isHoliday!);
7070
_status = ConnectionStatus.DONE;
7171
notifyUpdate();
7272
}

lib/feature/ecard_balance_feature.dart

+1-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ class EcardBalanceFeature extends Feature {
4646

4747
Future<void> _loadCard(PersonInfo? info) async {
4848
_status = ConnectionStatus.CONNECTING;
49-
_cardInfo = await Retrier.tryAsyncWithFix(
50-
() => CardRepository.getInstance().loadCardInfo(0),
51-
(exception) => CardRepository.getInstance().init(info));
49+
_cardInfo = await CardRepository.getInstance().loadCardInfo(info, 0);
5250
_balance = _cardInfo!.cash;
5351

5452
// If there's any transaction, we'll show it in the subtitle

lib/page/dashboard/card_detail.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import 'package:dan_xi/common/constant.dart';
1919
import 'package:dan_xi/generated/l10n.dart';
20+
import 'package:dan_xi/provider/state_provider.dart';
2021
import 'package:dan_xi/repository/fdu/ecard_repository.dart';
2122
import 'package:dan_xi/util/platform_universal.dart';
2223
import 'package:dan_xi/widget/libraries/platform_app_bar_ex.dart';
@@ -102,7 +103,7 @@ class CardDetailPageState extends State<CardDetailPage> {
102103
_selectable = false;
103104
});
104105
_cardInfo!.records = await CardRepository.getInstance()
105-
.loadCardRecord(_tagDays[index]);
106+
.loadCardRecord(StateProvider.personInfo.value, _tagDays[index]);
106107
setState(() {
107108
tag.checkedIcon = PlatformX.isMaterial(context)
108109
? Icons.check

lib/repository/base_repository.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ import 'package:flutter/foundation.dart';
2828
abstract class BaseRepositoryWithDio {
2929
/// The host that the implementation works with.
3030
///
31-
/// Should not contain scheme and/or path. e.g. www.jwc.fudan.edu.cn
31+
/// Should not contain scheme and/or path. e.g. fudan.edu.cn
32+
///
33+
/// This is used to create a separate [Dio] instance (and [CookieJar]) for each host. That is to say,
34+
/// the cookies which can be shared among multiple domains should come with the same [linkHost].
3235
String get linkHost;
3336

3437
@protected

lib/repository/fdu/aao_repository.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class FudanAAORepository extends BaseRepositoryWithDio {
6767
.then((value) => true, onError: (e) => false);
6868

6969
@override
70-
String get linkHost => "jwc.fudan.edu.cn";
70+
String get linkHost => "fudan.edu.cn";
7171
}
7272

7373
class NotConnectedToLANError implements Exception {}

lib/repository/fdu/bus_repository.dart

+3-7
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@ class FudanBusRepository extends BaseRepositoryWithDio {
4040
factory FudanBusRepository.getInstance() => _instance;
4141

4242
Future<List<BusScheduleItem>?> loadBusList(PersonInfo? info,
43-
{bool? holiday = false}) {
44-
return Retrier.tryAsyncWithFix(
45-
() => _loadBusList(holiday: holiday!),
46-
(exception) => UISLoginTool.fixByLoginUIS(
47-
dio, _LOGIN_URL, cookieJar!, info, true));
48-
}
43+
{bool holiday = false}) => UISLoginTool.tryAsyncWithAuth(
44+
dio, _LOGIN_URL, cookieJar!, info, () => _loadBusList(holiday: holiday));
4945

5046
Future<List<BusScheduleItem>?> _loadBusList({bool holiday = false}) async {
5147
List<BusScheduleItem> items = [];
@@ -63,7 +59,7 @@ class FudanBusRepository extends BaseRepositoryWithDio {
6359
}
6460

6561
@override
66-
String get linkHost => "zlapp.fudan.edu.cn";
62+
String get linkHost => "fudan.edu.cn";
6763
}
6864

6965
class BusScheduleItem implements Comparable<BusScheduleItem> {

lib/repository/fdu/data_center_repository.dart

+6-8
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,11 @@ class DataCenterRepository extends BaseRepositoryWithDio {
8484
}
8585

8686
Future<Map<String, TrafficInfo>?> getCrowdednessInfo(
87-
PersonInfo? info, int areaCode) async =>
88-
Retrier.tryAsyncWithFix(() => _getCrowdednessInfo(areaCode),
89-
(exception) async {
90-
if (exception is! UnsuitableTimeException) {
91-
await UISLoginTool.loginUIS(dio, LOGIN_URL, cookieJar!, info, true);
92-
}
93-
});
87+
PersonInfo? info, int areaCode) async {
88+
return UISLoginTool.tryAsyncWithAuth(
89+
dio, LOGIN_URL, cookieJar!, info, () => _getCrowdednessInfo(areaCode),
90+
isFatalError: (e) => e is UnsuitableTimeException);
91+
}
9492

9593
Future<Map<String, TrafficInfo>?> _getCrowdednessInfo(int areaCode) async {
9694
var result = <String, TrafficInfo>{};
@@ -159,7 +157,7 @@ class DataCenterRepository extends BaseRepositoryWithDio {
159157
}
160158

161159
@override
162-
String get linkHost => "my.fudan.edu.cn";
160+
String get linkHost => "fudan.edu.cn";
163161
}
164162

165163
class UnsuitableTimeException implements Exception {}

lib/repository/fdu/dorm_repository.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class FudanDormRepository extends BaseRepositoryWithDio {
6363
}
6464

6565
@override
66-
String get linkHost => "zlapp.fudan.edu.cn";
66+
String get linkHost => "fudan.edu.cn";
6767
}
6868

6969
/// This class represents the electricity info of a dorm. It's an interface for

lib/repository/fdu/ecard_repository.dart

+24-16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'package:collection/collection.dart' show IterableExtension;
2020
import 'package:dan_xi/model/person.dart';
2121
import 'package:dan_xi/repository/base_repository.dart';
2222
import 'package:dan_xi/repository/fdu/uis_login_tool.dart';
23+
import 'package:dan_xi/util/io/dio_utils.dart';
2324
import 'package:dan_xi/util/public_extension_methods.dart';
2425
import 'package:dan_xi/util/retrier.dart';
2526
import 'package:dio/dio.dart';
@@ -28,7 +29,6 @@ import 'package:html/dom.dart';
2829
import 'package:intl/intl.dart';
2930

3031
class CardRepository extends BaseRepositoryWithDio {
31-
PersonInfo? _info;
3232
static const String _LOGIN_URL =
3333
"https://uis.fudan.edu.cn/authserver/login?service=https%3A%2F%2Fecard.fudan.edu.cn%2Fepay%2Fj_spring_cas_security_check";
3434
static const String _USER_DETAIL_URL =
@@ -57,15 +57,9 @@ class CardRepository extends BaseRepositoryWithDio {
5757

5858
factory CardRepository.getInstance() => _instance;
5959

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);
6963

7064
Future<Iterable<CardRecord>> _loadOnePageCardRecord(
7165
Map<String, String?> requestData, int pageNum) async {
@@ -94,9 +88,14 @@ class CardRepository extends BaseRepositoryWithDio {
9488
/// If [logDays] > 0, it will return records of recent [logDays] days;
9589
/// If [logDays] = 0, it will return the latest records;
9690
/// 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 {
9897
if (logDays < 0) return null;
99-
//Get csrf id.
98+
// Get csrf id.
10099
Response<String> consumeCsrfPageResponse =
101100
await dio.get(_CONSUME_DETAIL_CSRF_URL);
102101
BeautifulSoup consumeCsrfPageSoup =
@@ -141,10 +140,19 @@ class CardRepository extends BaseRepositoryWithDio {
141140
return list;
142141
}
143142

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 {
145148
var cardInfo = CardInfo();
146149

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+
148156
var userPageResponse = await dio.get(_USER_DETAIL_URL);
149157
var soup = BeautifulSoup(userPageResponse.data.toString());
150158

@@ -153,13 +161,13 @@ class CardRepository extends BaseRepositoryWithDio {
153161
cardInfo.cash = cashElement?.text.trim();
154162
cardInfo.name = nameElement?.text.between("您好,", "!")?.trim();
155163
List<CardRecord>? records =
156-
await Retrier.runAsyncWithRetry(() => loadCardRecord(logDays));
164+
await Retrier.runAsyncWithRetry(() => _loadCardRecord(logDays));
157165
cardInfo.records = records;
158166
return cardInfo;
159167
}
160168

161169
@override
162-
String get linkHost => "ecard.fudan.edu.cn";
170+
String get linkHost => "fudan.edu.cn";
163171
}
164172

165173
class CardInfo {

lib/repository/fdu/edu_service_repository.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class EduServiceRepository extends BaseRepositoryWithDio {
5656
};
5757

5858
@override
59-
String get linkHost => "jwfw.fudan.edu.cn";
59+
String get linkHost => "fudan.edu.cn";
6060

6161
EduServiceRepository._();
6262

lib/repository/fdu/ehall_repository.dart

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
*/
1717

18-
1918
import 'package:dan_xi/model/person.dart';
2019
import 'package:dan_xi/repository/base_repository.dart';
2120
import 'package:dan_xi/repository/fdu/uis_login_tool.dart';
@@ -33,10 +32,9 @@ class FudanEhallRepository extends BaseRepositoryWithDio {
3332

3433
factory FudanEhallRepository.getInstance() => _instance;
3534

36-
Future<StudentInfo> getStudentInfo(PersonInfo info) async {
37-
await UISLoginTool.loginUIS(dio, _LOGIN_URL, cookieJar!, info, true);
38-
return _getStudentInfo();
39-
}
35+
Future<StudentInfo> getStudentInfo(PersonInfo info) async =>
36+
UISLoginTool.tryAsyncWithAuth(
37+
dio, _LOGIN_URL, cookieJar!, info, _getStudentInfo);
4038

4139
Future<StudentInfo> _getStudentInfo() async {
4240
Response<Map<String, dynamic>> rep = await dio.get(_INFO_URL);
@@ -49,7 +47,7 @@ class FudanEhallRepository extends BaseRepositoryWithDio {
4947
}
5048

5149
@override
52-
String get linkHost => "ehall.fudan.edu.cn";
50+
String get linkHost => "fudan.edu.cn";
5351
}
5452

5553
class StudentInfo {

lib/repository/fdu/empty_classroom_repository.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class EhallEmptyClassroomRepository extends BaseRepositoryWithDio {
148148
}
149149

150150
@override
151-
String get linkHost => "zlapp.fudan.edu.cn";
151+
String get linkHost => "fudan.edu.cn";
152152
}
153153

154154
class RoomInfo {

lib/repository/fdu/library_repository.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ class FudanLibraryRepository extends BaseRepositoryWithDio {
4343
}
4444

4545
@override
46-
String get linkHost => "mlibrary.fudan.edu.cn";
46+
String get linkHost => "fudan.edu.cn";
4747
}

lib/repository/fdu/pe_repository.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class FudanPERepository extends BaseRepositoryWithDio {
4040
Future<List<ExerciseItem>?> _loadExerciseRecords(PersonInfo? info) async {
4141
// PE system request a token from UIS to log in.
4242
String token = "";
43-
await UISLoginTool.loginUIS(dio, _LOGIN_URL, cookieJar!, info, true)
43+
await UISLoginTool.loginUIS(dio, _LOGIN_URL, cookieJar!, info)
4444
.catchError((e) {
4545
if (e is DioException && e.type == DioExceptionType.badResponse) {
4646
String url = e.response!.requestOptions.path;
@@ -65,7 +65,7 @@ class FudanPERepository extends BaseRepositoryWithDio {
6565
}
6666

6767
@override
68-
String get linkHost => "fdtyjw.fudan.edu.cn";
68+
String get linkHost => "fdtyjw.fudan.edu.cn"; // uses a separate host here, since we are excepting an error response from server
6969
}
7070

7171
class ExerciseItem {

lib/repository/fdu/postgraduate_timetable_repository.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class PostgraduateTimetableRepository extends BaseRepositoryWithDio {
136136
}
137137

138138
@override
139-
String get linkHost => "yjsxk.fudan.edu.cn";
139+
String get linkHost => "fudan.edu.cn";
140140
}
141141

142142
typedef OnCaptchaCallback = Future<String> Function(String imageUrl);

lib/repository/fdu/qr_code_repository.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ class QRCodeRepository extends BaseRepositoryWithDio {
5252
}
5353

5454
@override
55-
String get linkHost => "workflow1.fudan.edu.cn";
55+
String get linkHost => "fudan.edu.cn";
5656
}

lib/repository/fdu/sports_reserve_repository.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class SportsReserveRepository extends BaseRepositoryWithDio {
110110
}
111111

112112
@override
113-
String get linkHost => "elife.fudan.edu.cn";
113+
String get linkHost => "fudan.edu.cn";
114114
}
115115

116116
class StadiumData {

lib/repository/fdu/time_table_repository.dart

+11-11
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ class TimeTableRepository extends BaseRepositoryWithDio {
5959
UISLoginTool.tryAsyncWithAuth(dio, LOGIN_URL, cookieJar!, info,
6060
() => _loadTimeTableRemotely(startTime: startTime));
6161

62-
Future<String?> getDefaultSemesterId(PersonInfo? info) =>
63-
Retrier.tryAsyncWithFix(() async {
64-
await dio.get(ID_URL);
65-
return (await cookieJar!.loadForRequest(Uri.parse(HOST)))
66-
.firstWhere((element) => element.name == "semester.id")
67-
.value;
68-
},
69-
(exception) => UISLoginTool.fixByLoginUIS(
70-
dio, LOGIN_URL, cookieJar!, info, true));
62+
Future<String?> getDefaultSemesterId(PersonInfo? info) {
63+
return UISLoginTool.tryAsyncWithAuth(dio, LOGIN_URL, cookieJar!, info,
64+
() async {
65+
await dio.get(ID_URL);
66+
return (await cookieJar!.loadForRequest(Uri.parse(HOST)))
67+
.firstWhere((element) => element.name == "semester.id")
68+
.value;
69+
});
70+
}
7171

7272
Future<TimeTable?> _loadTimeTableRemotely({DateTime? startTime}) async {
7373
Future<String?> getAppropriateSemesterId() async {
@@ -100,7 +100,7 @@ class TimeTableRepository extends BaseRepositoryWithDio {
100100
tablePage.data!);
101101
for (var course in timetable.courses!) {
102102
for (var weekday in course.times!) {
103-
if (weekday.weekDay == 6) {
103+
if (weekday.weekDay == 6) {
104104
for (int i = 0; i < course.availableWeeks!.length; i++) {
105105
course.availableWeeks![i] = course.availableWeeks![i] - 1;
106106
}
@@ -132,7 +132,7 @@ class TimeTableRepository extends BaseRepositoryWithDio {
132132
}
133133

134134
@override
135-
String get linkHost => "jwfw.fudan.edu.cn";
135+
String get linkHost => "fudan.edu.cn";
136136
}
137137

138138
class Test {

0 commit comments

Comments
 (0)