docs: 更新项目文档,完善需求和技术细节
This commit is contained in:
124
admin-system/src/utils/api.js
Normal file
124
admin-system/src/utils/api.js
Normal file
@@ -0,0 +1,124 @@
|
||||
import axios from 'axios'
|
||||
|
||||
// 创建axios实例
|
||||
const api = axios.create({
|
||||
baseURL: 'http://localhost:3200/api/v1',
|
||||
timeout: 10000
|
||||
})
|
||||
|
||||
// 请求拦截器
|
||||
api.interceptors.request.use(
|
||||
config => {
|
||||
// 从localStorage获取token
|
||||
const token = localStorage.getItem('token')
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 响应拦截器
|
||||
api.interceptors.response.use(
|
||||
response => {
|
||||
// 直接返回数据部分
|
||||
return response.data
|
||||
},
|
||||
error => {
|
||||
if (error.response) {
|
||||
switch (error.response.status) {
|
||||
case 401:
|
||||
// 未授权,清除本地存储并跳转到登录页
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('userInfo')
|
||||
window.location.href = '/login'
|
||||
break
|
||||
case 403:
|
||||
console.error('权限不足')
|
||||
break
|
||||
case 500:
|
||||
console.error('服务器内部错误')
|
||||
break
|
||||
default:
|
||||
console.error('请求失败', error.response.data)
|
||||
}
|
||||
} else {
|
||||
console.error('网络错误', error.message)
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 认证相关API
|
||||
export const authAPI = {
|
||||
// 用户登录
|
||||
login: (data) => api.post('/auth/login', data),
|
||||
|
||||
// 用户注册
|
||||
register: (data) => api.post('/auth/register', data),
|
||||
|
||||
// 获取当前用户信息
|
||||
getCurrentUser: () => api.get('/users/me')
|
||||
}
|
||||
|
||||
// 用户管理API
|
||||
export const userAPI = {
|
||||
// 获取用户列表
|
||||
getUsers: (params) => api.get('/users', { params }),
|
||||
|
||||
// 获取用户详情
|
||||
getUser: (id) => api.get(`/users/${id}`),
|
||||
|
||||
// 更新用户信息
|
||||
updateUser: (id, data) => api.put(`/users/${id}`, data),
|
||||
|
||||
// 删除用户
|
||||
deleteUser: (id) => api.delete(`/users/${id}`)
|
||||
}
|
||||
|
||||
// 商品管理API
|
||||
export const productAPI = {
|
||||
// 获取商品列表
|
||||
getProducts: (params) => api.get('/products', { params }),
|
||||
|
||||
// 获取商品详情
|
||||
getProduct: (id) => api.get(`/products/${id}`),
|
||||
|
||||
// 创建商品
|
||||
createProduct: (data) => api.post('/products', data),
|
||||
|
||||
// 更新商品
|
||||
updateProduct: (id, data) => api.put(`/products/${id}`, data),
|
||||
|
||||
// 删除商品
|
||||
deleteProduct: (id) => api.delete(`/products/${id}`)
|
||||
}
|
||||
|
||||
// 订单管理API
|
||||
export const orderAPI = {
|
||||
// 获取订单列表
|
||||
getOrders: (params) => api.get('/orders', { params }),
|
||||
|
||||
// 获取订单详情
|
||||
getOrder: (id) => api.get(`/orders/${id}`),
|
||||
|
||||
// 更新订单状态
|
||||
updateOrderStatus: (id, data) => api.put(`/orders/${id}/status`, data)
|
||||
}
|
||||
|
||||
// 数据统计API
|
||||
export const statisticsAPI = {
|
||||
// 获取用户统计数据
|
||||
getUserStats: () => api.get('/admin/stats/users'),
|
||||
|
||||
// 获取订单统计数据
|
||||
getOrderStats: () => api.get('/admin/stats/orders'),
|
||||
|
||||
// 获取商品统计数据
|
||||
getProductStats: () => api.get('/admin/stats/products')
|
||||
}
|
||||
|
||||
export default api
|
||||
Reference in New Issue
Block a user