| | |
| | | import type { |
| | | MyPaperListItem, |
| | | MyPaperPageQuery, |
| | | OnlineExamDetail, |
| | | OnlineExamPaper, |
| | | OnlineExamSubmitResult, |
| | | } from '#/views/x/tms/onlineExam/types'; |
| | | |
| | | import { defHttp } from '#/api/request'; |
| | | import { |
| | | mockGetMyPaperDetail, |
| | | mockQueryMyPapers, |
| | | mockStartExam, |
| | | mockSubmitExam, |
| | | } from '#/views/x/tms/onlineExam/mock'; |
| | | |
| | | /** 后端未就绪前走 mock;联调后改为 false */ |
| | | const USE_MOCK = true; |
| | | const prefix = '/api/tms/online-exam'; |
| | | |
| | | async function unwrapData<T>(promise: Promise<any>): Promise<T> { |
| | | const res = await promise; |
| | | if (res && typeof res === 'object' && 'data' in res && ('code' in res || 'msg' in res)) { |
| | | return res.data as T; |
| | | } |
| | | return res as T; |
| | | } |
| | | |
| | | export function getMyPaperList(params: MyPaperPageQuery) { |
| | | if (USE_MOCK) return mockQueryMyPapers(params); |
| | | return defHttp.get({ url: `${prefix}/my-papers`, params }); |
| | | return unwrapData<{ list: MyPaperListItem[]; pagination: Record<string, any> }>( |
| | | defHttp.get({ url: `${prefix}/my-papers`, params }), |
| | | ); |
| | | } |
| | | |
| | | export function getMyPaperDetail(id: string) { |
| | | if (USE_MOCK) return mockGetMyPaperDetail(id); |
| | | return defHttp.get<OnlineExamDetail>({ url: `${prefix}/my-papers/${id}` }); |
| | | return unwrapData<OnlineExamDetail>(defHttp.get({ url: `${prefix}/my-papers/${id}` })); |
| | | } |
| | | |
| | | export function getExamReview(examId: string) { |
| | | return unwrapData<OnlineExamDetail>(defHttp.get({ url: `${prefix}/exams/${examId}` })); |
| | | } |
| | | |
| | | export function startOnlineExam(myPaperId: string) { |
| | | if (USE_MOCK) return mockStartExam(myPaperId); |
| | | return defHttp.post<OnlineExamPaper>({ url: `${prefix}/start`, data: { myPaperId } }); |
| | | return unwrapData<OnlineExamPaper>( |
| | | defHttp.post({ url: `${prefix}/start`, data: { myPaperId } }, { errorMessageMode: 'none' }), |
| | | ); |
| | | } |
| | | |
| | | export function submitOnlineExam(examId: string, data: { score: number; answers: Record<string, string | string[]> }) { |
| | | if (USE_MOCK) return mockSubmitExam(examId, data.score); |
| | | return defHttp.post({ url: `${prefix}/${examId}/submit`, data }); |
| | | export function saveOnlineExam(examId: string, answers: Record<string, string | string[]>) { |
| | | return unwrapData( |
| | | defHttp.post({ url: `${prefix}/${examId}/save`, data: { answers } }, { errorMessageMode: 'none' }), |
| | | ); |
| | | } |
| | | |
| | | export function submitOnlineExam(examId: string, answers: Record<string, string | string[]>) { |
| | | return unwrapData<OnlineExamSubmitResult>( |
| | | defHttp.post({ url: `${prefix}/${examId}/submit`, data: { answers } }, { errorMessageMode: 'none' }), |
| | | ); |
| | | } |