a
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// 认证相关 API
|
||||
import api from './config'
|
||||
|
||||
export const authApi = {
|
||||
// 登录
|
||||
login: (username, password) => {
|
||||
return api.post('/auth/login', { username, password })
|
||||
},
|
||||
|
||||
// 退出登录(同时清除后端 SSO Cookie)
|
||||
logout: () => {
|
||||
return api.post('/auth/logout')
|
||||
},
|
||||
|
||||
// 获取当前用户
|
||||
getMe: () => {
|
||||
return api.get('/auth/me')
|
||||
},
|
||||
|
||||
// 修改密码
|
||||
changePassword: (oldPassword, newPassword) => {
|
||||
return api.post('/auth/change-password', {
|
||||
old_password: oldPassword,
|
||||
new_password: newPassword
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// 分类相关 API
|
||||
import api from './config'
|
||||
|
||||
export const categoriesApi = {
|
||||
getCategories: () => api.get('/categories'),
|
||||
createCategory: (data) => api.post('/categories', data),
|
||||
updateCategory: (id, data) => api.put(`/categories/${id}`, data),
|
||||
deleteCategory: (id) => api.delete(`/categories/${id}`)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import axios from 'axios'
|
||||
|
||||
// API基础配置 - 使用绝对URL确保跨域访问
|
||||
// 强制使用后端端口 8889
|
||||
export const API_BASE_URL = 'http://129.211.173.87:8889/api'
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
timeout: 10000,
|
||||
withCredentials: true, // 允许跨域携带 Cookie(SSO 必需)
|
||||
})
|
||||
|
||||
// 请求拦截器 - 添加Token
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = localStorage.getItem('token')
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
},
|
||||
(error) => Promise.reject(error)
|
||||
)
|
||||
|
||||
// 响应拦截器 - 处理错误
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
localStorage.removeItem('token')
|
||||
// 避免在登录页本身触发跳转导致死循环刷新
|
||||
if (window.location.pathname !== '/login') {
|
||||
window.location.href = '/login'
|
||||
}
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export default api
|
||||
@@ -0,0 +1,34 @@
|
||||
// 文章相关 API
|
||||
import api from './config'
|
||||
|
||||
export const postsApi = {
|
||||
// 获取文章列表
|
||||
getPosts: (params = {}) => {
|
||||
return api.get('/posts', { params })
|
||||
},
|
||||
|
||||
// 获取单篇文章
|
||||
getPost: (id) => {
|
||||
return api.get(`/posts/${id}`)
|
||||
},
|
||||
|
||||
// 创建文章
|
||||
createPost: (data) => {
|
||||
return api.post('/posts', data)
|
||||
},
|
||||
|
||||
// 更新文章
|
||||
updatePost: (id, data) => {
|
||||
return api.put(`/posts/${id}`, data)
|
||||
},
|
||||
|
||||
// 删除文章
|
||||
deletePost: (id) => {
|
||||
return api.delete(`/posts/${id}`)
|
||||
},
|
||||
|
||||
// 获取统计
|
||||
getStats: () => {
|
||||
return api.get('/posts/stats/overview')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import api from './config'
|
||||
|
||||
/**
|
||||
* 获取博客设置(需要登录)
|
||||
*/
|
||||
export async function getSettings() {
|
||||
const res = await api.get('/settings')
|
||||
return res.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新博客设置(需要登录)
|
||||
*/
|
||||
export async function updateSettings(data) {
|
||||
const res = await api.put('/settings', data)
|
||||
return res.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 公开获取博客设置(无需登录,用于主题预加载)
|
||||
*/
|
||||
export async function getPublicSettings() {
|
||||
const res = await fetch(`${api.defaults.baseURL}/settings/public`, {
|
||||
method: 'GET',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
if (!res.ok) throw new Error('获取公开设置失败')
|
||||
return res.json()
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// 标签相关 API
|
||||
import api from './config'
|
||||
|
||||
export const tagsApi = {
|
||||
getTags: () => api.get('/tags/'),
|
||||
createTag: (data) => api.post('/tags/', data),
|
||||
deleteTag: (id) => api.delete(`/tags/${id}`)
|
||||
}
|
||||
Reference in New Issue
Block a user