第一版提交,答题功能OK,题库管理待完善
This commit is contained in:
115
src/services/api.ts
Normal file
115
src/services/api.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import axios from 'axios';
|
||||
|
||||
const API_BASE_URL = '/api';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// 请求拦截器
|
||||
api.interceptors.request.use(
|
||||
(config: any) => {
|
||||
// 添加管理员token
|
||||
if (typeof window !== 'undefined') {
|
||||
const adminToken = localStorage.getItem('survey_admin');
|
||||
if (adminToken) {
|
||||
const { token } = JSON.parse(adminToken);
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error: any) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// 响应拦截器
|
||||
api.interceptors.response.use(
|
||||
(response: any) => {
|
||||
// 如果响应类型是 blob,直接返回原始响应
|
||||
if (response.config.responseType === 'blob') {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
const { data } = response;
|
||||
if (data.success) {
|
||||
return data;
|
||||
} else {
|
||||
throw new Error(data.message || '请求失败');
|
||||
}
|
||||
},
|
||||
(error: any) => {
|
||||
if (error.response?.status === 401) {
|
||||
// 未授权,清除管理员信息
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.removeItem('survey_admin');
|
||||
window.location.href = '/admin/login';
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// 用户相关API
|
||||
export const userAPI = {
|
||||
createUser: (data: { name: string; phone: string; password?: string }) => api.post('/users', data),
|
||||
getUser: (id: string) => api.get(`/users/${id}`),
|
||||
validateUserInfo: (data: { name: string; phone: string }) => api.post('/users/validate', data),
|
||||
};
|
||||
|
||||
// 题目相关API
|
||||
export const questionAPI = {
|
||||
getQuestions: (params?: { type?: string; page?: number; limit?: number }) =>
|
||||
api.get('/questions', { params }),
|
||||
getQuestion: (id: string) => api.get(`/questions/${id}`),
|
||||
createQuestion: (data: any) => api.post('/questions', data),
|
||||
updateQuestion: (id: string, data: any) => api.put(`/questions/${id}`, data),
|
||||
deleteQuestion: (id: string) => api.delete(`/questions/${id}`),
|
||||
importQuestions: (file: File) => {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
return api.post('/questions/import', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
});
|
||||
},
|
||||
exportQuestions: (params?: { type?: string; category?: string }) =>
|
||||
api.get('/questions/export', {
|
||||
params,
|
||||
responseType: 'blob',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
// 答题相关API
|
||||
export const quizAPI = {
|
||||
generateQuiz: (userId: string, subjectId?: string, taskId?: string) =>
|
||||
api.post('/quiz/generate', { userId, subjectId, taskId }),
|
||||
submitQuiz: (data: { userId: string; subjectId?: string; taskId?: string; answers: any[] }) =>
|
||||
api.post('/quiz/submit', data),
|
||||
getUserRecords: (userId: string, params?: { page?: number; limit?: number }) =>
|
||||
api.get(`/quiz/records/${userId}`, { params }),
|
||||
getRecordDetail: (recordId: string) => api.get(`/quiz/records/detail/${recordId}`),
|
||||
getAllRecords: (params?: { page?: number; limit?: number }) =>
|
||||
api.get('/quiz/records', { params }),
|
||||
};
|
||||
|
||||
// 管理员相关API
|
||||
export const adminAPI = {
|
||||
login: (data: { username: string; password: string }) => api.post('/admin/login', data),
|
||||
getQuizConfig: () => api.get('/admin/config'),
|
||||
updateQuizConfig: (data: any) => api.put('/admin/config', data),
|
||||
getStatistics: () => api.get('/admin/statistics'),
|
||||
updatePassword: (data: { username: string; oldPassword: string; newPassword: string }) =>
|
||||
api.put('/admin/password', data),
|
||||
};
|
||||
|
||||
export default api;
|
||||
Reference in New Issue
Block a user