duidui_mini_program/config/question_api.js
2026-03-27 10:41:46 +08:00

145 lines
5.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import apiConfig from './api_config.js';
import request from '../utils/request.js';
// Fiber 后端请求函数
// 注意request 函数内部已经会拼接 baseURL所以这里只需要传入相对路径
function fiberRequest(options) {
return request({
url: options.url, // 只传入相对路径request 函数会拼接 baseURL
method: options.method || 'GET',
data: options.data,
header: options.header || {}
});
}
export const questionApi = {
/**
* 获取试卷详情(使用 Fiber 后端)
* @param {string} paperId 试卷ID
* @returns {Promise} 试卷数据
*/
getPaperDetail: (paperId) => {
return fiberRequest({
url: `/paper/detail?id=${paperId}`,
method: 'GET'
}).then(res => {
// 转换 Fiber 后端返回格式为前端期望的格式
if (res.success && res.paper) {
return {
success: true,
code: 200,
data: res.paper,
paper: res.paper // 兼容旧格式
};
}
return res;
});
},
/**
* 获取试卷(兼容旧接口,接受 paperId 和 taskId
* @param {string} paperId 试卷ID
* @param {string} taskId 任务ID可选
* @returns {Promise} 试卷数据
*/
getExamPaper: (paperId, taskId) => {
return questionApi.getPaperDetail(paperId);
},
/**
* 获取试卷详情(包含答案和解析,用于答题结果页面)
* @param {string} paperId 试卷ID
* @returns {Promise} 试卷数据(包含答案和解析)
*/
getPaperWithAnswers: (paperId) => {
return fiberRequest({
url: `/paper/result?id=${paperId}`,
method: 'GET'
}).then(res => {
// 转换 Fiber 后端返回格式为前端期望的格式
if (res.success && (res.paper || res.data)) {
const paper = res.paper || res.data;
return {
success: true,
code: 200,
data: paper,
paper: paper // 兼容旧格式
};
}
return res;
});
},
/**
* 创建答题记录(使用 Fiber 后端)
* @param {object} data 答题记录数据
* @param {string} data.user_id 用户ID
* @param {string} data.paper_id 试卷ID
* @param {number} data.start_time 开始时间Unix时间戳
* @param {number} data.end_time 结束时间Unix时间戳
* @param {array} data.answers 答案数组
* @returns {Promise} 创建结果
*/
createAnswerRecord: (data) => {
// 转换前端数据格式为 Fiber 后端期望的格式
const fiberData = {
user_id: String(data.user_id),
paper_id: String(data.paper_id),
task_id: data.task_id ? String(data.task_id) : '', // 打卡营任务ID用于不同打卡营的答题隔离
start_time: data.start_time || Math.floor(Date.now() / 1000),
end_time: data.end_time || Math.floor(Date.now() / 1000),
answers: (data.answers || []).map(answer => ({
question_id: String(answer.question_id),
user_answer: String(answer.user_answer || '')
}))
};
return fiberRequest({
url: `/answer_record/create`,
method: 'POST',
data: fiberData
}).then(res => {
// 转换 Fiber 后端返回格式为前端期望的格式
if (res.success) {
return {
success: true,
code: 200,
...res
};
}
return res;
});
},
/**
* 获取答题记录(使用 Fiber 后端)
* @param {string} userId 用户ID
* @param {string} paperId 试卷ID
* @param {number} page 页码
* @param {number} pageSize 每页数量
* @returns {Promise} 答题记录列表
*/
getAnswerRecord: (userId, paperId, page = 1, pageSize = 100, taskId = '') => {
let url = `/answer_record/detail?user_id=${userId}&paper_id=${paperId}&page=${page}&page_size=${pageSize}`;
if (taskId) {
url += `&task_id=${taskId}`;
}
return fiberRequest({
url: url,
method: 'GET'
}).then(res => {
// 转换 Fiber 后端返回格式为前端期望的格式
if (res.success) {
return {
success: true,
code: 200,
...res
};
}
return res;
});
}
};