import type { QuestionBankOption, QuestionEntity, QuestionPageQuery } from '#/views/x/tms/question/types';
|
|
import { defHttp } from '#/api/request';
|
|
/** 正式路径:/api/tms/question/** */
|
const prefix = '/api/tms/question';
|
|
/** defHttp 成功时返回整包 ActionResult,页面需要裸 data */
|
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 getQuestionBanks() {
|
return unwrapData<QuestionBankOption[]>(defHttp.get({ url: `${prefix}/banks` }));
|
}
|
|
export function getQuestionAdmins() {
|
return unwrapData<{ id: string; fullName: string }[]>(defHttp.get({ url: `${prefix}/admins` }));
|
}
|
|
export function getQuestionList(params: QuestionPageQuery) {
|
return unwrapData<{ list: QuestionEntity[]; pagination: Record<string, any> }>(
|
defHttp.get({ url: `${prefix}/list`, params }),
|
);
|
}
|
|
export function getQuestionInfo(id: string) {
|
return unwrapData<QuestionEntity>(defHttp.get({ url: `${prefix}/${id}` }));
|
}
|
|
export function createQuestion(data: QuestionEntity) {
|
return unwrapData<string>(defHttp.post({ url: prefix, data }));
|
}
|
|
export function updateQuestion(data: QuestionEntity) {
|
return unwrapData(defHttp.put({ url: `${prefix}/${data.id}`, data }));
|
}
|
|
/** 废弃 = 逻辑删除 */
|
export function invalidateQuestion(id: string) {
|
return unwrapData(defHttp.put({ url: `${prefix}/${id}/invalidate` }));
|
}
|
|
/** 开放/不开放:open / closed */
|
export function setQuestionStatus(id: string, bizStatus: 'open' | 'closed') {
|
return unwrapData(defHttp.put({ url: `${prefix}/${id}/status`, data: { bizStatus } }));
|
}
|
|
/** 导出试题 Excel */
|
export function exportQuestions(data: {
|
ids?: string[];
|
bankId?: string;
|
questionType?: string;
|
bizStatus?: string;
|
keyword?: string;
|
}) {
|
return defHttp.post(
|
{
|
url: `${prefix}/export`,
|
data,
|
responseType: 'blob',
|
},
|
{
|
isReturnNativeResponse: true,
|
isTransformResponse: false,
|
},
|
);
|
}
|
|
/** 下载导入模板 */
|
export function downloadQuestionImportTemplate() {
|
return defHttp.get(
|
{
|
url: `${prefix}/import/template`,
|
responseType: 'blob',
|
},
|
{
|
isReturnNativeResponse: true,
|
isTransformResponse: false,
|
},
|
);
|
}
|
|
export interface QuestionImportResult {
|
successCount: number;
|
failCount: number;
|
errors?: string[];
|
}
|
|
/** 批量导入试题 */
|
export function importQuestions(file: File) {
|
const formData = new FormData();
|
formData.append('file', file);
|
return unwrapData<QuestionImportResult>(
|
defHttp.post({
|
url: `${prefix}/import`,
|
data: formData,
|
headers: { 'Content-Type': 'multipart/form-data' },
|
}),
|
);
|
}
|