标签功能初版实现
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import api from './index'
|
||||
|
||||
export interface LoginRequest {
|
||||
account: string
|
||||
password: string
|
||||
rememberMe?: boolean
|
||||
}
|
||||
|
||||
export interface RegisterRequest {
|
||||
account: string
|
||||
password: string
|
||||
nickname: string
|
||||
shopId?: number
|
||||
}
|
||||
|
||||
export interface LoginResponse {
|
||||
accessToken: string
|
||||
refreshToken: string
|
||||
userInfo: UserInfo
|
||||
}
|
||||
|
||||
export interface UserInfo {
|
||||
id: number
|
||||
account: string
|
||||
nickname: string
|
||||
avatar?: string
|
||||
createTime: string
|
||||
}
|
||||
|
||||
export interface RefreshTokenRequest {
|
||||
refreshToken: string
|
||||
}
|
||||
|
||||
export interface RefreshTokenResponse {
|
||||
accessToken: string
|
||||
refreshToken: string
|
||||
}
|
||||
|
||||
export const authApi = {
|
||||
login: (data: LoginRequest) => {
|
||||
return api.post<LoginResponse>('/user/login', data)
|
||||
},
|
||||
|
||||
register: (data: RegisterRequest) => {
|
||||
return api.post('/user/register', data)
|
||||
},
|
||||
|
||||
refreshToken: (data: RefreshTokenRequest) => {
|
||||
return api.post<RefreshTokenResponse>('/user/refresh-token', data)
|
||||
},
|
||||
|
||||
logout: () => {
|
||||
return api.post('/user/logout')
|
||||
},
|
||||
|
||||
getUserInfo: () => {
|
||||
return api.get<UserInfo>('/user/info')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import axios, { AxiosError } from 'axios'
|
||||
import { authApi } from './auth'
|
||||
import router from '../router'
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: 'http://localhost:8088/api',
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
|
||||
let isRefreshing = false
|
||||
let failedQueue: Array<{
|
||||
resolve: (value?: any) => void
|
||||
reject: (reason?: any) => void
|
||||
}> = []
|
||||
|
||||
const processQueue = (error: any, token: string | null = null) => {
|
||||
failedQueue.forEach(prom => {
|
||||
if (error) {
|
||||
prom.reject(error)
|
||||
} else {
|
||||
prom.resolve(token)
|
||||
}
|
||||
})
|
||||
failedQueue = []
|
||||
}
|
||||
|
||||
api.interceptors.request.use(
|
||||
config => {
|
||||
let token = localStorage.getItem('accessToken')
|
||||
if (!token) {
|
||||
token = sessionStorage.getItem('accessToken')
|
||||
}
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
api.interceptors.response.use(
|
||||
response => {
|
||||
return response.data.data
|
||||
},
|
||||
async (error: AxiosError) => {
|
||||
const originalRequest = error.config as any
|
||||
|
||||
if (error.response?.status === 401 && !originalRequest._retry) {
|
||||
if (isRefreshing) {
|
||||
return new Promise((resolve, reject) => {
|
||||
failedQueue.push({ resolve, reject })
|
||||
}).then(token => {
|
||||
if (token) {
|
||||
originalRequest.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return api(originalRequest)
|
||||
}).catch(err => {
|
||||
return Promise.reject(err)
|
||||
})
|
||||
}
|
||||
|
||||
originalRequest._retry = true
|
||||
isRefreshing = true
|
||||
|
||||
try {
|
||||
const refreshToken = localStorage.getItem('refreshToken')
|
||||
if (!refreshToken) {
|
||||
throw new Error('No refresh token available')
|
||||
}
|
||||
|
||||
const response = await authApi.refreshToken({ refreshToken })
|
||||
const newToken = response.data.accessToken
|
||||
const newRefreshToken = response.data.refreshToken
|
||||
|
||||
localStorage.setItem('accessToken', newToken)
|
||||
localStorage.setItem('refreshToken', newRefreshToken)
|
||||
sessionStorage.setItem('accessToken', newToken)
|
||||
|
||||
processQueue(null, newToken)
|
||||
|
||||
originalRequest.headers.Authorization = `Bearer ${newToken}`
|
||||
return api(originalRequest)
|
||||
} catch (refreshError) {
|
||||
processQueue(refreshError, null)
|
||||
localStorage.removeItem('accessToken')
|
||||
localStorage.removeItem('refreshToken')
|
||||
sessionStorage.removeItem('accessToken')
|
||||
router.push('/auth')
|
||||
return Promise.reject(refreshError)
|
||||
} finally {
|
||||
isRefreshing = false
|
||||
}
|
||||
}
|
||||
|
||||
console.error('API Error:', error)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export default api
|
||||
@@ -0,0 +1,59 @@
|
||||
import api from './index'
|
||||
|
||||
export interface ProductVO {
|
||||
id: number
|
||||
shopId: number
|
||||
shopName: string
|
||||
title: string
|
||||
description: string
|
||||
imageFileIds: string
|
||||
imageUrls: string[]
|
||||
price: number
|
||||
tags: string
|
||||
tagList: string[]
|
||||
likeCount: number
|
||||
collectCount: number
|
||||
approximateDuration: string
|
||||
bookableArtists: string
|
||||
availableCoupons: string
|
||||
createTime: string
|
||||
updateTime: string
|
||||
}
|
||||
|
||||
export interface ProductDTO {
|
||||
shopId: number
|
||||
title: string
|
||||
description: string
|
||||
imageFileIds: string
|
||||
price: number
|
||||
tags: string
|
||||
approximateDuration: string
|
||||
bookableArtists: string
|
||||
availableCoupons: string
|
||||
}
|
||||
|
||||
export const productApi = {
|
||||
getAllProducts: () => {
|
||||
return api.get<ProductVO[]>('/product/list/all')
|
||||
},
|
||||
|
||||
getProductById: (id: number) => {
|
||||
return api.get<ProductVO>(`/product/get/${id}`)
|
||||
},
|
||||
|
||||
getProductsByTag: (tag: string) => {
|
||||
return api.get<ProductVO[]>(`/product/list/tag/${tag}`)
|
||||
},
|
||||
|
||||
createProduct: (data: ProductDTO) => {
|
||||
return api.post<ProductVO>('/product/add', data)
|
||||
},
|
||||
|
||||
updateProduct: (id: number, data: ProductDTO) => {
|
||||
return api.put<ProductVO>(`/product/update/${id}`, data)
|
||||
},
|
||||
|
||||
deleteProduct: (id: number) => {
|
||||
return api.delete(`/product/delete/${id}`)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import api from './index'
|
||||
|
||||
export interface ShopVO {
|
||||
id: number
|
||||
shopName: string
|
||||
address: string
|
||||
phone: string
|
||||
businessHours: string
|
||||
description: string
|
||||
createTime: string
|
||||
updateTime: string
|
||||
}
|
||||
|
||||
export interface ShopSimpleVO {
|
||||
id: number
|
||||
shopName: string
|
||||
}
|
||||
|
||||
export const shopApi = {
|
||||
getAllShops: () => {
|
||||
return api.get<ShopVO[]>('/shop/list/all')
|
||||
},
|
||||
|
||||
getShopsSimple: () => {
|
||||
return api.get<ShopSimpleVO[]>('/shop/list/simple')
|
||||
},
|
||||
|
||||
getShopById: (id: number) => {
|
||||
return api.get<ShopVO>(`/shop/get/${id}`)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import api from './index'
|
||||
|
||||
export interface TagVO {
|
||||
id: number
|
||||
tagName: string
|
||||
textColor: string
|
||||
bgColor: string
|
||||
borderColor: string
|
||||
createTime: string
|
||||
updateTime: string
|
||||
}
|
||||
|
||||
export interface TagDTO {
|
||||
tagName: string
|
||||
textColor: string
|
||||
bgColor: string
|
||||
borderColor: string
|
||||
}
|
||||
|
||||
export const tagApi = {
|
||||
getAllTags: () => {
|
||||
return api.get<TagVO[]>('/tag/list/all')
|
||||
},
|
||||
|
||||
getTagById: (id: number) => {
|
||||
return api.get<TagVO>(`/tag/get/${id}`)
|
||||
},
|
||||
|
||||
createTag: (data: TagDTO) => {
|
||||
return api.post<TagVO>('/tag/add', data)
|
||||
},
|
||||
|
||||
updateTag: (id: number, data: TagDTO) => {
|
||||
return api.put<TagVO>(`/tag/update/${id}`, data)
|
||||
},
|
||||
|
||||
deleteTag: (id: number) => {
|
||||
return api.delete(`/tag/delete/${id}`)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user