Skip to content

Commit

Permalink
[timetable] fixed importing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Sep 2, 2024
1 parent e5bd155 commit 37a7c77
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
12 changes: 6 additions & 6 deletions lib/init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ class Init {
}
final uaForSchoolServer = await getUserAgentForSchoolServer();
schoolDio = Dio(BaseOptions(
connectTimeout: const Duration(milliseconds: 8000),
receiveTimeout: const Duration(milliseconds: 8000),
sendTimeout: const Duration(milliseconds: 8000),
connectTimeout: const Duration(milliseconds: 16000),
receiveTimeout: const Duration(milliseconds: 16000),
sendTimeout: const Duration(milliseconds: 16000),
headers: {
"User-Agent": uaForSchoolServer,
},
)).withCookieJar(schoolCookieJar).withDebugging();

dioNoCookie = Dio(BaseOptions(
connectTimeout: const Duration(milliseconds: 8000),
receiveTimeout: const Duration(milliseconds: 8000),
sendTimeout: const Duration(milliseconds: 8000),
connectTimeout: const Duration(milliseconds: 16000),
receiveTimeout: const Duration(milliseconds: 16000),
sendTimeout: const Duration(milliseconds: 16000),
headers: {
"User-Agent": uaForSchoolServer,
},
Expand Down
2 changes: 1 addition & 1 deletion lib/session/sso.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const _neededHeaders = {
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-User": "?1",
"Referer": "https://authserver.sit.edu.cn/authserver/login",
// "Referer": "https://authserver.sit.edu.cn/authserver/login",
};

final networkLogger = Logger(
Expand Down
41 changes: 41 additions & 0 deletions lib/session/ug_registration.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:mimir/credentials/entity/credential.dart';
import 'package:mimir/credentials/init.dart';
import 'package:mimir/init.dart';
import 'package:mimir/r.dart';
import 'package:mimir/school/entity/school.dart';

import 'package:mimir/session/sso.dart';
import 'package:mimir/utils/dio.dart';

/// jwxt.sit.edu.cn
/// Student registration system for undergraduate
Expand All @@ -15,6 +19,41 @@ class UgRegistrationSession {
required SsoSession ssoSession,
}) : _ssoSession = ssoSession;

Future<Response> importTimetable(String url, SemesterInfo info) async {
await Init.schoolCookieJar.deleteAll();
final loginRes = await _ssoSession.loginLocked(CredentialsInit.storage.oa.credentials!);
final response = await Init.schoolDio.request(
"http://jwxt.sit.edu.cn/sso/jziotlogin",
options: Options(
followRedirects: false,
validateStatus: (status) {
return status! < 400;
},
),
);
final debugDepths = <Response>[];
final finalResponse = await processRedirect(
Init.schoolDio,
response,
debugDepths: debugDepths,
);
print(debugDepths);
final timetableRes = await Init.schoolDio.get(
url,
options: Options(
method: "POST",
),
queryParameters: {'gnmkdm': 'N253508'},
data: FormData.fromMap({
// 学年名
'xnm': info.exactYear.toString(),
// 学期名
'xqm': info.semester.toUgRegFormField()
}),
);
return timetableRes;
}

Future<void> _refreshCookie() async {
await Init.schoolCookieJar.delete(R.ugRegUri, true);
await _ssoSession.request(
Expand Down Expand Up @@ -57,6 +96,7 @@ class UgRegistrationSession {
);
}

await _refreshCookie();
final response = await fetch();
if (_isRedirectedToLoginPage(response)) {
debugPrint('JwxtSession requires login');
Expand All @@ -69,6 +109,7 @@ class UgRegistrationSession {
Future<bool> checkConnectivity({
String url = 'http://jwxt.sit.edu.cn/',
}) async {
return true;
try {
await Init.dioNoCookie.request(
url,
Expand Down
27 changes: 14 additions & 13 deletions lib/timetable/service/school.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@ class TimetableService {

/// 获取本科生课表
Future<SitTimetable> fetchUgTimetable(SemesterInfo info) async {
final response = await _ugRegSession.request(
_undergraduateTimetableUrl,
options: Options(
method: "POST",
),
queryParameters: {'gnmkdm': 'N253508'},
data: {
// 学年名
'xnm': info.exactYear.toString(),
// 学期名
'xqm': info.semester.toUgRegFormField()
},
);
// final response = await _ugRegSession.request(
// _undergraduateTimetableUrl,
// options: Options(
// method: "POST",
// ),
// queryParameters: {'gnmkdm': 'N253508'},
// data: {
// // 学年名
// 'xnm': info.exactYear.toString(),
// // 学期名
// 'xqm': info.semester.toUgRegFormField()
// },
// );
final response = await _ugRegSession.importTimetable(_undergraduateTimetableUrl, info);
final json = response.data;
return parseUndergraduateTimetableFromRaw(
json,
Expand Down

0 comments on commit 37a7c77

Please sign in to comment.