From 37a7c77ea12aa2cbd76cabe90034be9c9078f959 Mon Sep 17 00:00:00 2001 From: Liplum Date: Mon, 2 Sep 2024 09:33:20 +0800 Subject: [PATCH] [timetable] fixed importing issue --- lib/init.dart | 12 ++++----- lib/session/sso.dart | 2 +- lib/session/ug_registration.dart | 41 +++++++++++++++++++++++++++++++ lib/timetable/service/school.dart | 27 ++++++++++---------- 4 files changed, 62 insertions(+), 20 deletions(-) diff --git a/lib/init.dart b/lib/init.dart index 84ab8da75..cf820215e 100644 --- a/lib/init.dart +++ b/lib/init.dart @@ -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, }, diff --git a/lib/session/sso.dart b/lib/session/sso.dart index c10b29a94..78186a16b 100644 --- a/lib/session/sso.dart +++ b/lib/session/sso.dart @@ -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( diff --git a/lib/session/ug_registration.dart b/lib/session/ug_registration.dart index 1d7478996..80e483651 100644 --- a/lib/session/ug_registration.dart +++ b/lib/session/ug_registration.dart @@ -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 @@ -15,6 +19,41 @@ class UgRegistrationSession { required SsoSession ssoSession, }) : _ssoSession = ssoSession; + Future 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 = []; + 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 _refreshCookie() async { await Init.schoolCookieJar.delete(R.ugRegUri, true); await _ssoSession.request( @@ -57,6 +96,7 @@ class UgRegistrationSession { ); } + await _refreshCookie(); final response = await fetch(); if (_isRedirectedToLoginPage(response)) { debugPrint('JwxtSession requires login'); @@ -69,6 +109,7 @@ class UgRegistrationSession { Future checkConnectivity({ String url = 'http://jwxt.sit.edu.cn/', }) async { + return true; try { await Init.dioNoCookie.request( url, diff --git a/lib/timetable/service/school.dart b/lib/timetable/service/school.dart index d9b6bae46..40a315a4b 100644 --- a/lib/timetable/service/school.dart +++ b/lib/timetable/service/school.dart @@ -30,19 +30,20 @@ class TimetableService { /// 获取本科生课表 Future 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,