2025-12-25 10:48:35 +08:00
|
|
|
import axios from 'axios';
|
2026-03-06 14:39:52 +08:00
|
|
|
import { API_CONFIG } from '../config/api';
|
2026-03-29 10:06:32 +08:00
|
|
|
import secureStorage, { TOKEN_KEY } from '../utils/secureStorage';
|
2025-12-25 10:48:35 +08:00
|
|
|
|
|
|
|
|
const api = axios.create({
|
2026-03-06 14:39:52 +08:00
|
|
|
baseURL: API_CONFIG.baseURL,
|
|
|
|
|
timeout: API_CONFIG.timeout,
|
2025-12-25 10:48:35 +08:00
|
|
|
headers: {
|
2026-02-10 11:04:01 +08:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
2025-12-25 10:48:35 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
api.interceptors.request.use(
|
2026-02-10 11:04:01 +08:00
|
|
|
config => {
|
2026-03-29 10:06:32 +08:00
|
|
|
const token = secureStorage.get(TOKEN_KEY);
|
2025-12-25 10:48:35 +08:00
|
|
|
if (token) {
|
|
|
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
|
|
|
}
|
2026-02-10 11:04:01 +08:00
|
|
|
|
2026-01-29 16:53:29 +08:00
|
|
|
// 开发环境下安全日志:过滤敏感字段
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
const sensitiveFields = ['password', 'oldPassword', 'newPassword', 'confirmPassword'];
|
|
|
|
|
const safeData = config.data ? { ...config.data } : null;
|
|
|
|
|
if (safeData) {
|
|
|
|
|
sensitiveFields.forEach(field => {
|
|
|
|
|
if (safeData[field]) safeData[field] = '***';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
console.log(`[API] ${config.method?.toUpperCase()} ${config.url}`, safeData || '');
|
|
|
|
|
}
|
2026-02-10 11:04:01 +08:00
|
|
|
|
2025-12-25 10:48:35 +08:00
|
|
|
return config;
|
|
|
|
|
},
|
2026-02-10 11:04:01 +08:00
|
|
|
error => {
|
2025-12-25 10:48:35 +08:00
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
api.interceptors.response.use(
|
2026-02-10 11:04:01 +08:00
|
|
|
response => {
|
2025-12-25 10:48:35 +08:00
|
|
|
return response.data;
|
|
|
|
|
},
|
2026-02-10 11:04:01 +08:00
|
|
|
error => {
|
2025-12-25 10:48:35 +08:00
|
|
|
if (error.response) {
|
|
|
|
|
const { status, data } = error.response;
|
2026-02-10 11:04:01 +08:00
|
|
|
|
2025-12-25 10:48:35 +08:00
|
|
|
if (status === 401) {
|
|
|
|
|
const currentPath = window.location.pathname;
|
2026-02-10 11:04:01 +08:00
|
|
|
|
2025-12-25 10:48:35 +08:00
|
|
|
if (!currentPath.startsWith('/login')) {
|
2026-03-29 10:06:32 +08:00
|
|
|
secureStorage.remove(TOKEN_KEY);
|
|
|
|
|
secureStorage.remove('user');
|
2025-12-25 10:48:35 +08:00
|
|
|
window.location.href = '/login';
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-10 11:04:01 +08:00
|
|
|
|
2025-12-25 10:48:35 +08:00
|
|
|
return Promise.reject(data.message || '请求失败');
|
|
|
|
|
}
|
2026-02-10 11:04:01 +08:00
|
|
|
|
2025-12-25 10:48:35 +08:00
|
|
|
if (error.code === 'ECONNABORTED') {
|
|
|
|
|
return Promise.reject('请求超时,请稍后重试');
|
|
|
|
|
}
|
2026-02-10 11:04:01 +08:00
|
|
|
|
2025-12-25 10:48:35 +08:00
|
|
|
return Promise.reject('网络错误,请检查网络连接');
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const authAPI = {
|
|
|
|
|
checkAdmin: () => api.get('/auth/check-admin'),
|
2026-02-10 11:04:01 +08:00
|
|
|
register: data => api.post('/auth/register', data),
|
|
|
|
|
login: data => api.post('/auth/login', data),
|
|
|
|
|
unlock: data => api.post('/auth/unlock', data),
|
2025-12-25 10:48:35 +08:00
|
|
|
getProfile: () => api.get('/auth/profile'),
|
2026-02-10 11:04:01 +08:00
|
|
|
updateProfile: data => api.put('/auth/profile', data),
|
|
|
|
|
changePassword: data => api.put('/auth/password', data),
|
2025-12-25 10:48:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const userAPI = {
|
2026-02-10 11:04:01 +08:00
|
|
|
list: params => api.get('/users', { params }),
|
2025-12-25 10:48:35 +08:00
|
|
|
all: () => api.get('/users/all'),
|
2026-02-10 11:04:01 +08:00
|
|
|
get: userId => api.get(`/users/${userId}`),
|
|
|
|
|
create: data => api.post('/users', data),
|
2025-12-25 10:48:35 +08:00
|
|
|
update: (userId, data) => api.put(`/users/${userId}`, data),
|
|
|
|
|
resetPassword: (userId, data) => api.put(`/users/${userId}/password`, data),
|
2026-02-10 11:04:01 +08:00
|
|
|
delete: userId => api.delete(`/users/${userId}`),
|
2025-12-25 10:48:35 +08:00
|
|
|
uploadAvatar: (userId, file) => {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('avatar', file);
|
|
|
|
|
return api.post(`/users/${userId}/avatar`, formData, {
|
2026-02-10 11:04:01 +08:00
|
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
2025-12-25 10:48:35 +08:00
|
|
|
});
|
|
|
|
|
},
|
2026-02-10 11:04:01 +08:00
|
|
|
deleteAvatar: userId => api.delete(`/users/${userId}/avatar`),
|
|
|
|
|
approve: userId => api.put(`/users/${userId}/approve`),
|
|
|
|
|
reject: userId => api.put(`/users/${userId}/reject`),
|
2025-12-25 10:48:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const roleAPI = {
|
2026-02-10 11:04:01 +08:00
|
|
|
list: params => api.get('/roles', { params }),
|
2025-12-25 10:48:35 +08:00
|
|
|
all: () => api.get('/roles/all'),
|
2026-02-10 11:04:01 +08:00
|
|
|
get: roleId => api.get(`/roles/${roleId}`),
|
|
|
|
|
create: data => api.post('/roles', data),
|
2025-12-25 10:48:35 +08:00
|
|
|
update: (roleId, data) => api.put(`/roles/${roleId}`, data),
|
2026-02-10 11:04:01 +08:00
|
|
|
delete: roleId => api.delete(`/roles/${roleId}`),
|
|
|
|
|
initRoles: () => api.post('/roles/init-roles'),
|
2025-12-25 10:48:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const loginHistoryAPI = {
|
2026-02-10 11:04:01 +08:00
|
|
|
list: params => api.get('/login-history', { params }),
|
2025-12-25 10:48:35 +08:00
|
|
|
getByUser: (userId, params) => api.get(`/login-history/user/${userId}`, { params }),
|
2026-02-10 11:04:01 +08:00
|
|
|
delete: id => api.delete(`/login-history/${id}`),
|
|
|
|
|
clear: data => api.delete('/login-history', { data }),
|
2025-12-25 10:48:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const operationLogAPI = {
|
2026-02-10 11:04:01 +08:00
|
|
|
list: params => api.get('/operation-logs', { params }),
|
2025-12-25 10:48:35 +08:00
|
|
|
getActions: () => api.get('/operation-logs/actions'),
|
|
|
|
|
getModules: () => api.get('/operation-logs/modules'),
|
2026-02-10 11:04:01 +08:00
|
|
|
delete: id => api.delete(`/operation-logs/${id}`),
|
|
|
|
|
clear: data => api.delete('/operation-logs', { data }),
|
2025-12-25 10:48:35 +08:00
|
|
|
};
|
|
|
|
|
|
2026-02-12 13:27:32 +08:00
|
|
|
export const deviceAPI = {
|
|
|
|
|
list: params => api.get('/devices', { params }),
|
|
|
|
|
get: deviceId => api.get(`/devices/${deviceId}`),
|
|
|
|
|
create: data => api.post('/devices', data),
|
|
|
|
|
update: (deviceId, data) => api.put(`/devices/${deviceId}`, data),
|
|
|
|
|
delete: deviceId => api.delete(`/devices/${deviceId}`),
|
|
|
|
|
getTickets: (deviceId, params) => api.get(`/devices/${deviceId}/tickets`, { params }),
|
2026-03-26 14:35:08 +08:00
|
|
|
checkPosition: (rackId, params) => api.get(`/devices/check-position/${rackId}`, { params }),
|
2026-02-12 13:27:32 +08:00
|
|
|
};
|
|
|
|
|
|
2025-12-25 16:49:00 +08:00
|
|
|
export const ticketAPI = {
|
2026-02-10 11:04:01 +08:00
|
|
|
list: params => api.get('/tickets', { params }),
|
|
|
|
|
get: ticketId => api.get(`/tickets/${ticketId}`),
|
|
|
|
|
create: data => api.post('/tickets', data),
|
2025-12-25 16:49:00 +08:00
|
|
|
update: (ticketId, data) => api.put(`/tickets/${ticketId}`, data),
|
2026-02-10 11:04:01 +08:00
|
|
|
delete: ticketId => api.delete(`/tickets/${ticketId}`),
|
2025-12-25 16:49:00 +08:00
|
|
|
assign: (ticketId, data) => api.put(`/tickets/${ticketId}/assign`, data),
|
|
|
|
|
transfer: (ticketId, data) => api.put(`/tickets/${ticketId}/transfer`, data),
|
|
|
|
|
process: (ticketId, data) => api.put(`/tickets/${ticketId}/process`, data),
|
|
|
|
|
close: (ticketId, data) => api.put(`/tickets/${ticketId}/close`, data),
|
|
|
|
|
reopen: (ticketId, data) => api.put(`/tickets/${ticketId}/reopen`, data),
|
2026-02-10 11:04:01 +08:00
|
|
|
getOperations: ticketId => api.get(`/tickets/${ticketId}/operations`),
|
|
|
|
|
getStatistics: params => api.get('/tickets/statistics', { params }),
|
2025-12-25 16:49:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const ticketCategoryAPI = {
|
2026-02-10 11:04:01 +08:00
|
|
|
list: params => api.get('/ticket-categories', { params }),
|
|
|
|
|
get: code => api.get(`/ticket-categories/${code}`),
|
|
|
|
|
create: data => api.post('/ticket-categories', data),
|
2025-12-25 16:49:00 +08:00
|
|
|
update: (code, data) => api.put(`/ticket-categories/${code}`, data),
|
2026-02-10 11:04:01 +08:00
|
|
|
delete: code => api.delete(`/ticket-categories/${code}`),
|
2025-12-25 16:49:00 +08:00
|
|
|
tree: () => api.get('/ticket-categories/tree'),
|
2026-02-10 11:04:01 +08:00
|
|
|
init: () => api.post('/ticket-categories/init'),
|
2025-12-25 16:49:00 +08:00
|
|
|
};
|
|
|
|
|
|
2026-03-13 15:13:58 +08:00
|
|
|
export const backupAPI = {
|
|
|
|
|
list: () => api.get('/backup/list'),
|
|
|
|
|
create: (data = {}) => api.post('/backup', data),
|
|
|
|
|
validate: filename => api.get(`/backup/validate/${filename}`),
|
|
|
|
|
restore: (filename, options = {}) => api.post('/backup/restore', { filename, options }),
|
|
|
|
|
download: filename => `/api/backup/download/${filename}`,
|
|
|
|
|
delete: filename => api.delete(`/backup/${filename}`),
|
|
|
|
|
upload: file => {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('backup', file);
|
|
|
|
|
return api.post('/backup/upload', formData, {
|
|
|
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
info: () => api.get('/backup/info'),
|
2026-03-16 17:07:20 +08:00
|
|
|
getAutoStatus: () => api.get('/backup/auto/status'),
|
|
|
|
|
updateAutoSettings: data => api.post('/backup/auto/settings', data),
|
|
|
|
|
executeNow: data => api.post('/backup/auto/execute', data),
|
|
|
|
|
testCron: data => api.post('/backup/auto/test-cron', data),
|
|
|
|
|
getLogs: params => api.get('/backup/logs', { params }),
|
|
|
|
|
getLogDetail: id => api.get(`/backup/logs/${id}`),
|
|
|
|
|
cleanOldLogs: days => api.delete('/backup/logs/clean', { params: { days } }),
|
2026-03-13 15:13:58 +08:00
|
|
|
};
|
|
|
|
|
|
2025-12-25 10:48:35 +08:00
|
|
|
export default api;
|