Skip to content

Commit 0dfd206

Browse files
committed
fix(登录): 在登录前重置登录上下文并改进鉴权错误处理
1 parent 2757a0f commit 0dfd206

5 files changed

Lines changed: 26 additions & 0 deletions

File tree

lib/api/auth/auth_api.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ Y/akVmYNtghKZzz6jwIDAQAB
4040
await loginWithEncrypted(account: account, encryptedPassword: encryptedPwd);
4141
}
4242

43+
Future<void> resetLoginContext({String? account}) async {
44+
if (account == null || account.trim().isEmpty) {
45+
_timetableLoginStates.clear();
46+
} else {
47+
_timetableLoginStates.remove(account.trim());
48+
}
49+
await _client.cookieJar.deleteAll();
50+
}
51+
4352
Future<void> ensureTimetableLogin({
4453
required String account,
4554
String? password,

lib/api/course/course_api.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ class CourseApi {
7777
allowReloginRetry: false,
7878
);
7979
}
80+
if (_looksLikeAuthError(parsed)) {
81+
throw Exception(_authErrorMessage(parsed));
82+
}
8083
return parsed;
8184
} catch (e) {
8285
if (!allowReloginRetry) rethrow;
@@ -116,4 +119,10 @@ class CourseApi {
116119
if (data.containsKey('code') || data.containsKey('msg')) return true;
117120
return false;
118121
}
122+
123+
String _authErrorMessage(Map<String, dynamic> data) {
124+
final msg = (data['msg'] ?? '').toString().trim();
125+
if (msg.isNotEmpty) return msg;
126+
return '课表鉴权失败,请重新登录';
127+
}
119128
}

lib/pages/ClassSchedule/controllers/schedule_controller.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ class ScheduleController {
274274
persistLastViewed: persistLastViewed,
275275
updateWidgetPins: shouldPinWidget,
276276
);
277+
final loadedWeek = (data.weekNum ?? '').trim();
278+
final loadedWeeks = data.weekList;
279+
if (loadedWeek.isEmpty || loadedWeeks == null || loadedWeeks.isEmpty) {
280+
throw Exception("课表鉴权失败,请重新登录");
281+
}
277282

278283
if (weekNum == null && yearTerm == null) {
279284
final now = DateTime.now();

lib/pages/Login/Login.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class _LoginPageState extends State<LoginPage> {
195195
setState(() => _isLoading = true);
196196

197197
try {
198+
await _authApi.resetLoginContext();
198199
if (useSavedPassword) {
199200
await _authApi.loginWithEncrypted(
200201
account: account,

lib/pages/Mine/mine_logout_dialog.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:cqut/api/auth/auth_api.dart';
12
import 'package:flutter/material.dart';
23
import 'package:shared_preferences/shared_preferences.dart';
34

@@ -17,6 +18,7 @@ Future<void> showMineLogoutDialog(BuildContext context) async {
1718
final prefs = await SharedPreferences.getInstance();
1819
await prefs.remove('account');
1920
await prefs.remove('encrypted_password');
21+
await AuthApi().resetLoginContext();
2022
if (context.mounted) {
2123
Navigator.pop(context);
2224
ScaffoldMessenger.of(

0 commit comments

Comments
 (0)