Skip to content

Commit 3aba35f

Browse files
committed
feat(clients): 添加考试安排获取
1 parent fc2dedb commit 3aba35f

File tree

5 files changed

+465
-165
lines changed

5 files changed

+465
-165
lines changed

auth/cas.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,33 @@ export default class CASAuth {
362362
}
363363
}
364364

365+
async loginMHUB() {
366+
const url = 'http://mhub.hust.edu.cn/';
367+
const isLogin = await this.checkLoginStatus();
368+
369+
if (!isLogin) {
370+
return false;
371+
}
372+
373+
try {
374+
let response = await this.axios.get(url);
375+
376+
if (
377+
response.status === 302 &&
378+
response.headers.location.includes('login')
379+
) {
380+
response = await this.axios.get(
381+
`${CASAuth.CAS_URL}/login?service=${encodeURIComponent('https://mhub.hust.edu.cn/cas/login')}`,
382+
);
383+
384+
response = await followRedirect(response, this.axios);
385+
}
386+
return response.status === 200;
387+
} catch (e) {
388+
return false;
389+
}
390+
}
391+
365392
async loginONE() {
366393
const url = 'https://one.hust.edu.cn/dcp/';
367394
const isLogin = await this.checkLoginStatus();

clients/course-client.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import {
77
getPhysicsCourseSchedule as getPhysicsCourseSchedule,
88
} from './course/physics';
99
import {
10+
getExaminationArrangements,
1011
getNormalCourseGrades,
1112
getNormalCourseSchedule,
1213
} from './course/course';
14+
import { ExaminationType } from '@/types/clients/course/course';
1315

1416
export default class CourseClient {
1517
protected readonly axios: AxiosInstance;
@@ -71,4 +73,33 @@ export default class CourseClient {
7173
getNormalCourseGrades(this.axios, semesterId, pageNumber, pageSize),
7274
);
7375
}
76+
77+
async getExaminationArrangements(
78+
semesterId: string,
79+
{
80+
pageIndex,
81+
pageSize,
82+
keyword,
83+
type,
84+
}: {
85+
pageIndex: number;
86+
pageSize: number;
87+
keyword: string;
88+
type: ExaminationType;
89+
} = {
90+
pageIndex: 1,
91+
pageSize: 100,
92+
keyword: '',
93+
type: ExaminationType.NORMAL,
94+
},
95+
) {
96+
return await this.hust.handleRequest(() =>
97+
getExaminationArrangements(this.axios, semesterId, {
98+
pageIndex,
99+
pageSize,
100+
keyword,
101+
type,
102+
}),
103+
);
104+
}
74105
}

clients/course/course.ts

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import type {
2-
NormalCourseGrade,
3-
NormalCourseGradesResponse,
4-
NormalCourseSchedule,
5-
NormalCourseScheduleResponse,
1+
import {
2+
ExaminationType,
3+
type ExaminationArrangement,
4+
type ExaminationArrangementsResponse,
5+
type NormalCourseGrade,
6+
type NormalCourseGradesResponse,
7+
type NormalCourseSchedule,
8+
type NormalCourseScheduleResponse,
69
} from '@/types/clients/course/course';
710
import type { AxiosInstance } from 'axios';
811
import type { Dayjs } from 'dayjs';
@@ -200,3 +203,67 @@ export async function getNormalCourseGrades(
200203
throw e;
201204
}
202205
}
206+
207+
export async function getExaminationArrangements(
208+
axios: AxiosInstance,
209+
semesterId: string,
210+
{
211+
pageIndex,
212+
pageSize,
213+
keyword,
214+
type,
215+
}: {
216+
pageIndex: number;
217+
pageSize: number;
218+
keyword: string | null;
219+
type: ExaminationType;
220+
} = {
221+
pageIndex: 1,
222+
pageSize: 100,
223+
keyword: null,
224+
type: ExaminationType.NORMAL,
225+
},
226+
): Promise<ExaminationArrangement[]> {
227+
const url = 'https://mhub.hust.edu.cn/ksapController/getStuKsxx';
228+
229+
try {
230+
const response = await axios.post(url, {
231+
xqh: semesterId,
232+
pageIndex: pageIndex,
233+
pageSize: pageSize,
234+
kcmc: keyword,
235+
kslx: type,
236+
});
237+
if (response.status !== 200) {
238+
throw new Error(
239+
`Error fetching examination arrangements for semester ${semesterId}.`,
240+
);
241+
}
242+
const data: ExaminationArrangementsResponse = response.data;
243+
244+
console.log(data);
245+
246+
return data.list.map((item) => ({
247+
scheduleId: item.SCHEDULEID,
248+
departmentName: item.DWMC,
249+
departmentId: item.PKDW,
250+
classroomName: item.JSMC,
251+
courseId: item.KCBH,
252+
courseName: item.KCMC,
253+
examType: item.KSLX === '0' ? ExaminationType.RETAKE : ExaminationType.NORMAL,
254+
examDate: item.KSRQ,
255+
classId: item.KTBH,
256+
studentId: item.SFID,
257+
studentName: item.XM,
258+
semesterId: item.XQH,
259+
semesterName: item.XQMC,
260+
lastTeachingWeek: item.ZHSKZC,
261+
}));
262+
} catch (e) {
263+
console.error(
264+
'CourseClient.getExaminationArrangements error on fetching',
265+
e,
266+
);
267+
throw e;
268+
}
269+
}

index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export default class HUST {
159159
this.auth.loginPhysics(),
160160
this.auth.loginHUBM(),
161161
this.auth.loginHUBS(),
162+
this.auth.loginMHUB(),
162163
])
163164
).every((item) => item);
164165
default:
@@ -267,7 +268,7 @@ console.log('Initializing HUST SDK...');
267268
const res = await hust.news.getNewsList();
268269
console.log('News List:', res);
269270

270-
const res2 = await hust.course.getNormalCourseGrades('20241', { all: true });
271-
console.log('Course Grades:', res2);
271+
const res2 = await hust.course.getExaminationArrangements('20241');
272+
console.log('Examination Arrangements:', res2);
272273

273274
console.log(`initialized after ${new Date().getTime() - now.getTime()}ms`);

0 commit comments

Comments
 (0)