首个可运行的版本
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import api from './client'
|
||||
|
||||
export const adminApi = {
|
||||
login: (password: string) =>
|
||||
api.post('/admin/login', { password }),
|
||||
|
||||
getDashboard: () => api.get('/admin/dashboard'),
|
||||
|
||||
getStats: (metric: string, days = 7) =>
|
||||
api.get(`/admin/stats/${metric}`, { params: { days } }),
|
||||
|
||||
getUsers: (params?: { page?: number; page_size?: number; search?: string; status?: string }) =>
|
||||
api.get('/admin/users', { params }),
|
||||
|
||||
banUser: (userId: string, isBanned: boolean, reason?: string) =>
|
||||
api.put(`/admin/users/${userId}/ban`, { is_banned: isBanned, reason }),
|
||||
|
||||
deleteUser: (userId: string) =>
|
||||
api.delete(`/admin/users/${userId}`),
|
||||
|
||||
getMessages: (params?: Record<string, string>) =>
|
||||
api.get('/admin/messages', { params }),
|
||||
|
||||
getConfig: () => api.get('/admin/config'),
|
||||
|
||||
updateConfig: (configs: Record<string, string>) =>
|
||||
api.put('/admin/config', { configs }),
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import api from './client'
|
||||
|
||||
export const authApi = {
|
||||
login: (username: string, password: string) =>
|
||||
api.post('/auth/login', { username, password }),
|
||||
|
||||
register: (username: string, email: string, password: string) =>
|
||||
api.post('/auth/register', { username, email, password }),
|
||||
|
||||
refresh: (refreshToken: string) =>
|
||||
api.post('/auth/refresh', { refresh_token: refreshToken }),
|
||||
|
||||
getProfile: () => api.get('/users/me'),
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import api from './client'
|
||||
|
||||
export const chatApi = {
|
||||
getConversations: () => api.get('/conversations/'),
|
||||
|
||||
createPrivateConversation: (userId: string) =>
|
||||
api.post('/conversations/', { type: 'private', member_ids: [userId] }),
|
||||
|
||||
createGroup: (name: string, memberIds: string[], description?: string) =>
|
||||
api.post('/conversations/group', { name, member_ids: memberIds, description }),
|
||||
|
||||
getConversationDetail: (id: string) => api.get(`/conversations/${id}`),
|
||||
|
||||
getMessages: (conversationId: string, before?: string, limit = 50) => {
|
||||
const params: Record<string, any> = { limit }
|
||||
if (before) params.before = before
|
||||
return api.get(`/conversations/${conversationId}/messages`, { params })
|
||||
},
|
||||
|
||||
markAsRead: (conversationId: string, messageId: string) =>
|
||||
api.put(`/conversations/${conversationId}/messages/${messageId}/read`),
|
||||
|
||||
deleteMessage: (conversationId: string, messageId: string) =>
|
||||
api.delete(`/conversations/${conversationId}/messages/${messageId}`),
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import axios from 'axios'
|
||||
import type { AxiosInstance } from 'axios'
|
||||
|
||||
const API_BASE = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000'
|
||||
|
||||
const api: AxiosInstance = axios.create({
|
||||
baseURL: `${API_BASE}/api/v1`,
|
||||
timeout: 15000,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
|
||||
// 请求拦截器:附加 Token
|
||||
api.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('access_token')
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
// 响应拦截器:处理 401 自动刷新
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error) => {
|
||||
const originalRequest = error.config
|
||||
if (error.response?.status === 401 && !originalRequest._retry) {
|
||||
originalRequest._retry = true
|
||||
const refreshToken = localStorage.getItem('refresh_token')
|
||||
if (refreshToken) {
|
||||
try {
|
||||
const { data } = await axios.post(`${API_BASE}/api/v1/auth/refresh`, {
|
||||
refresh_token: refreshToken,
|
||||
})
|
||||
localStorage.setItem('access_token', data.access_token)
|
||||
localStorage.setItem('refresh_token', data.refresh_token)
|
||||
originalRequest.headers.Authorization = `Bearer ${data.access_token}`
|
||||
return api(originalRequest)
|
||||
} catch {
|
||||
localStorage.removeItem('access_token')
|
||||
localStorage.removeItem('refresh_token')
|
||||
window.location.href = '/login'
|
||||
}
|
||||
}
|
||||
}
|
||||
return Promise.reject(error)
|
||||
},
|
||||
)
|
||||
|
||||
export default api
|
||||
@@ -0,0 +1,19 @@
|
||||
import api from './client'
|
||||
|
||||
export const friendsApi = {
|
||||
getFriends: () => api.get('/friends/'),
|
||||
|
||||
getPendingRequests: () => api.get('/friends/requests'),
|
||||
|
||||
sendRequest: (toUserId: string, message?: string) =>
|
||||
api.post('/friends/request', { to_user_id: toUserId, message }),
|
||||
|
||||
acceptRequest: (requestId: string) =>
|
||||
api.put(`/friends/request/${requestId}/accept`),
|
||||
|
||||
rejectRequest: (requestId: string) =>
|
||||
api.put(`/friends/request/${requestId}/reject`),
|
||||
|
||||
removeFriend: (friendId: string) =>
|
||||
api.delete(`/friends/${friendId}`),
|
||||
}
|
||||
Reference in New Issue
Block a user