| | |
| | | import type { CourseItem, CoursePageQuery, PaperOption } from '#/views/x/tms/course/types'; |
| | | |
| | | import { defHttp } from '#/api/request'; |
| | | import { |
| | | mockDeleteCourse, |
| | | mockGetCourse, |
| | | mockPaperOptions, |
| | | mockQueryCourses, |
| | | mockSaveCourse, |
| | | mockSetCourseEnabled, |
| | | } from '#/views/x/tms/course/mock'; |
| | | |
| | | const USE_MOCK = true; |
| | | /** 正式路径:/api/tms/course/** */ |
| | | const prefix = '/api/tms/course'; |
| | | |
| | | 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 getCourseList(params: CoursePageQuery) { |
| | | if (USE_MOCK) return mockQueryCourses(params); |
| | | return defHttp.get({ url: `${prefix}/list`, params }); |
| | | return unwrapData<{ list: CourseItem[]; pagination: Record<string, any> }>( |
| | | defHttp.get({ url: `${prefix}/list`, params }), |
| | | ); |
| | | } |
| | | |
| | | export function getCourseInfo(id: string) { |
| | | if (USE_MOCK) return mockGetCourse(id); |
| | | return defHttp.get<CourseItem>({ url: `${prefix}/${id}` }); |
| | | return unwrapData<CourseItem>(defHttp.get({ url: `${prefix}/${id}` })); |
| | | } |
| | | |
| | | export function getCoursePaperOptions() { |
| | | if (USE_MOCK) return mockPaperOptions(); |
| | | return defHttp.get<PaperOption[]>({ url: `${prefix}/paper-options` }); |
| | | return unwrapData<PaperOption[]>(defHttp.get({ url: `${prefix}/paper-options` })); |
| | | } |
| | | |
| | | export function createCourse(data: Partial<CourseItem>) { |
| | | if (USE_MOCK) return mockSaveCourse(data); |
| | | return defHttp.post({ url: prefix, data }); |
| | | return unwrapData<string>(defHttp.post({ url: prefix, data })); |
| | | } |
| | | |
| | | export function updateCourse(data: Partial<CourseItem> & { id: string }) { |
| | | if (USE_MOCK) return mockSaveCourse(data); |
| | | return defHttp.put({ url: `${prefix}/${data.id}`, data }); |
| | | return unwrapData(defHttp.put({ url: `${prefix}/${data.id}`, data })); |
| | | } |
| | | |
| | | export function setCourseEnabled(id: string, enabled: '0' | '1') { |
| | | if (USE_MOCK) return mockSetCourseEnabled(id, enabled); |
| | | return defHttp.put({ url: `${prefix}/${id}/enabled`, data: { enabled } }); |
| | | return unwrapData(defHttp.put({ url: `${prefix}/${id}/enabled`, data: { enabled } })); |
| | | } |
| | | |
| | | export function deleteCourse(id: string) { |
| | | if (USE_MOCK) return mockDeleteCourse(id); |
| | | return defHttp.delete({ url: `${prefix}/${id}` }); |
| | | return unwrapData(defHttp.delete({ url: `${prefix}/${id}` })); |
| | | } |