a
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
// API client configuration
|
||||
const API_BASE_URL = import.meta.env.VITE_API_URL || '';
|
||||
|
||||
class ApiClient {
|
||||
constructor(baseURL) {
|
||||
this.baseURL = baseURL;
|
||||
}
|
||||
|
||||
async request(url, options = {}, raw = false) {
|
||||
const token = localStorage.getItem('token');
|
||||
const isFormData = options.body instanceof FormData;
|
||||
const headers = {
|
||||
...(isFormData ? {} : { 'Content-Type': 'application/json' }),
|
||||
...(token && { 'Authorization': `Bearer ${token}` }),
|
||||
...options.headers,
|
||||
};
|
||||
|
||||
const response = await fetch(`${this.baseURL}${url}`, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
localStorage.removeItem('token');
|
||||
window.location.href = '/admin/login';
|
||||
}
|
||||
const error = await response.json().catch(() => ({ message: 'Request failed' }));
|
||||
throw new Error(error.detail || error.message || `HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
// API returns {code, data, message} format for list operations
|
||||
// But auth returns {code, token, admin} without data field
|
||||
if (raw) return result;
|
||||
return result.data !== undefined ? result.data : result;
|
||||
}
|
||||
|
||||
get(url, options = {}) {
|
||||
let fullUrl = url;
|
||||
const { params, ...fetchOptions } = options;
|
||||
if (params) {
|
||||
const queryString = new URLSearchParams(
|
||||
Object.fromEntries(
|
||||
Object.entries(params).filter(([, v]) => v !== undefined && v !== null && v !== '')
|
||||
)
|
||||
).toString();
|
||||
if (queryString) fullUrl = `${url}?${queryString}`;
|
||||
}
|
||||
return this.request(fullUrl, fetchOptions);
|
||||
}
|
||||
|
||||
post(url, data, raw = false) {
|
||||
// 如果是FormData,不设置Content-Type,让浏览器自动设置
|
||||
if (data instanceof FormData) {
|
||||
return this.request(url, {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
}, true); // raw=true 避免嵌套data字段
|
||||
}
|
||||
return this.request(url, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
}, raw);
|
||||
}
|
||||
|
||||
put(url, data) {
|
||||
return this.request(url, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
|
||||
delete(url) {
|
||||
return this.request(url, { method: 'DELETE' });
|
||||
}
|
||||
}
|
||||
|
||||
const api = new ApiClient(API_BASE_URL);
|
||||
export default api;
|
||||
|
||||
// Auth API - use raw=true to get full response including token
|
||||
export const authApi = {
|
||||
login: (username, password) => api.post('/api/auth/login', { username, password }, true),
|
||||
me: () => api.get('/api/auth/me'),
|
||||
changePassword: (oldPassword, newPassword) =>
|
||||
api.post('/api/auth/change-password', { old_password: oldPassword, new_password: newPassword }),
|
||||
};
|
||||
|
||||
// Home Modules API
|
||||
export const modulesApi = {
|
||||
getPublic: () => api.get('/api/home/modules'),
|
||||
getAll: () => api.get('/api/admin/home/modules'),
|
||||
getById: (id) => api.get(`/api/admin/home/modules/${id}`),
|
||||
update: (id, data) => api.put(`/api/admin/home/modules/${id}`, data),
|
||||
sort: (orders) => api.put('/api/admin/home/modules/sort', { orders }),
|
||||
toggle: (id) => api.put(`/api/admin/home/modules/${id}/toggle`),
|
||||
};
|
||||
|
||||
// Nav Menu API
|
||||
export const navApi = {
|
||||
getPublic: () => api.get('/api/nav'),
|
||||
getAll: () => api.get('/api/admin/nav'),
|
||||
create: (data) => api.post('/api/admin/nav', data),
|
||||
update: (id, data) => api.put(`/api/admin/nav/${id}`, data),
|
||||
delete: (id) => api.delete(`/api/admin/nav/${id}`),
|
||||
};
|
||||
|
||||
// Contact QR Code API
|
||||
export const contactQrApi = {
|
||||
getPublic: () => api.get('/api/contact-qrcodes'),
|
||||
getAll: () => api.get('/api/admin/contact-qrcodes'),
|
||||
create: (data) => api.post('/api/admin/contact-qrcodes', data),
|
||||
update: (id, data) => api.put(`/api/admin/contact-qrcodes/${id}`, data),
|
||||
delete: (id) => api.delete(`/api/admin/contact-qrcodes/${id}`),
|
||||
};
|
||||
|
||||
// Settings API
|
||||
export const settingsApi = {
|
||||
getPublic: () => api.get('/api/settings'),
|
||||
getAll: () => api.get('/api/admin/settings'),
|
||||
update: (settings) => api.put('/api/admin/settings', { settings }),
|
||||
};
|
||||
|
||||
// App Download API
|
||||
export const appDownloadsApi = {
|
||||
getPublic: () => api.get('/api/app-downloads'),
|
||||
getAll: () => api.get('/api/admin/app-downloads'),
|
||||
create: (data) => api.post('/api/admin/app-downloads', data),
|
||||
update: (id, data) => api.put(`/api/admin/app-downloads/${id}`, data),
|
||||
delete: (id) => api.delete(`/api/admin/app-downloads/${id}`),
|
||||
};
|
||||
|
||||
// Home Experts API
|
||||
export const homeExpertsApi = {
|
||||
getPublic: () => api.get('/api/home-experts'),
|
||||
getAll: () => api.get('/api/admin/home-experts'),
|
||||
getAvailablePosts: (keyword) => api.get('/api/admin/home-experts/available-posts', { params: { keyword } }),
|
||||
create: (data) => api.post('/api/admin/home-experts', data),
|
||||
update: (id, data) => api.put(`/api/admin/home-experts/${id}`, data),
|
||||
delete: (id) => api.delete(`/api/admin/home-experts/${id}`),
|
||||
};
|
||||
|
||||
// Home Partners API
|
||||
export const homePartnersApi = {
|
||||
getPublic: () => api.get('/api/home-partners'),
|
||||
getAll: () => api.get('/api/admin/home-partners'),
|
||||
create: (data) => api.post('/api/admin/home-partners', data),
|
||||
update: (id, data) => api.put(`/api/admin/home-partners/${id}`, data),
|
||||
delete: (id) => api.delete(`/api/admin/home-partners/${id}`),
|
||||
};
|
||||
|
||||
// Columns API
|
||||
export const columnsApi = {
|
||||
getAll: (tree = false) => api.get('/api/admin/columns', { params: { tree } }),
|
||||
getTree: () => api.get('/api/admin/columns/tree'),
|
||||
getById: (id) => api.get(`/api/admin/columns/${id}`),
|
||||
create: (data) => api.post('/api/admin/columns', data),
|
||||
update: (id, data) => api.put(`/api/admin/columns/${id}`, data),
|
||||
delete: (id, force = false) => api.delete(`/api/admin/columns/${id}?force=${force}`),
|
||||
};
|
||||
|
||||
// Articles API
|
||||
export const articlesApi = {
|
||||
getAll: (params = {}) => api.get('/api/admin/articles', { params }),
|
||||
getById: (id) => api.get(`/api/admin/articles/${id}`),
|
||||
create: (data) => api.post('/api/admin/articles', data),
|
||||
update: (id, data) => api.put(`/api/admin/articles/${id}`, data),
|
||||
delete: (id) => api.delete(`/api/admin/articles/${id}`),
|
||||
batchDelete: (ids) => api.post('/api/admin/articles/batch-delete', { ids }),
|
||||
batchMove: (ids, column_id) => api.post('/api/admin/articles/batch-move', { ids, column_id }),
|
||||
publish: (id, is_published) => api.post(`/api/admin/articles/${id}/publish`, { is_published }),
|
||||
rewritePublish: (id) => api.post(`/api/admin/articles/${id}/rewrite-publish`),
|
||||
};
|
||||
|
||||
// Upload API
|
||||
export const uploadApi = {
|
||||
uploadImage: (file, subdir = '') => {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
if (subdir) formData.append('subdir', subdir);
|
||||
return fetch(`${API_BASE_URL}/api/upload/image`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`,
|
||||
},
|
||||
body: formData,
|
||||
}).then(r => r.json());
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user