Files
mini-programs/src/services/evaluateService.ts
2025-09-30 14:08:21 +08:00

126 lines
3.1 KiB
TypeScript

import httpService from "./httpService";
import type { ApiResponse } from "./httpService";
// 单个选项类型
interface Option {
text: string;
score: number;
}
// 单个问题类型
export interface Question {
id: number;
question_title: string;
question_content: string;
options: Option[];
radar_mapping: string[];
}
// 单项能力分数
interface AbilityScore {
current_score: number;
max_score: number;
percentage: number;
}
// 雷达图数据
interface RadarData {
abilities: Record<string, AbilityScore>; // key 是能力名称,如 "正手球质"
summary: {
total_questions: number;
calculation_time: string; // ISO 字符串
};
}
// 单题答案
interface Answer {
question_id: number;
question_title: string;
answer_index: number;
selected_option: string;
score: number;
}
// 提交测试结果 对象类型
export interface TestResultData {
record_id: number;
total_score: number;
ntrp_level: string;
is_coverage: boolean;
old_ntrp_level: string;
level_description: string;
radar_data: RadarData;
answers: Answer[];
}
// 单条测试记录
interface TestRecord {
id: number;
total_score: number;
ntrp_level: string;
level_description: string;
test_duration: number; // 单位:秒
create_time: string; // 时间字符串
}
// 测试历史对象类型
export interface TestResultList {
count: number;
rows: TestRecord[];
}
// 单次测试结果
interface TestResult {
id: number;
total_score: number;
ntrp_level: string;
level_description: string;
radar_data: RadarData;
test_duration: number; // 单位秒
create_time: string; // 时间字符串
}
// data 对象
// 上一次测试结果
export interface LastTimeTestResult {
has_test_record: boolean;
has_ntrp_level: boolean;
user_ntrp_level: string;
last_test_result: TestResult;
}
class EvaluateService {
// 获取测试题目
async getQuestions(): Promise<ApiResponse<Question[]>> {
return httpService.post("/ntrp/questions", {}, { showLoading: true });
}
// 提交答案
async submit(req: { answers: { question_id: number, answer_index: number }[], test_duration: number }): Promise<ApiResponse<TestResultData>> {
return httpService.post("/ntrp/submit", req, { showLoading: true });
}
// 获取测试历史
async getResultList(): Promise<ApiResponse<TestResultList>> {
return httpService.post("/ntrp/history", {}, { showLoading: true });
}
// 获取测试详情
async getTestResult(req: { record_id: number }): Promise<ApiResponse<TestResultData>> {
return httpService.post("/ntrp/detail", req, { showLoading: true });
}
// 获取最后一次(最新)测试结果
async getLastResult(): Promise<ApiResponse<LastTimeTestResult>> {
return httpService.post("/ntrp/last_result", {}, { showLoading: true });
}
// 更新NTRP等级
async updateNtrp(req: { record_id: number, ntrp_level: string, update_type: string }): Promise<ApiResponse<any>> {
return httpService.post("/ntrp/update_user_level", req, { showLoading: true });
}
}
// 导出认证服务实例
export default new EvaluateService();