功能:完善多店铺登录与首次部署SQL
This commit is contained in:
+133
-37
@@ -3,10 +3,9 @@ import router from '../router'
|
||||
import { message } from 'ant-design-vue'
|
||||
import { ADMIN_API_BASE_URL } from '../config/api'
|
||||
|
||||
// 防重复提示标志
|
||||
let lastErrorTime = 0
|
||||
let lastErrorMessage = ''
|
||||
const ERROR_DEBOUNCE_TIME = 1000 // 1秒内不重复显示相同错误
|
||||
const ERROR_DEBOUNCE_TIME = 1000
|
||||
|
||||
const adminApi = axios.create({
|
||||
baseURL: ADMIN_API_BASE_URL,
|
||||
@@ -16,10 +15,62 @@ const adminApi = axios.create({
|
||||
}
|
||||
})
|
||||
|
||||
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 = []
|
||||
}
|
||||
|
||||
const showError = (msg: string) => {
|
||||
const now = Date.now()
|
||||
if (now - lastErrorTime > ERROR_DEBOUNCE_TIME || msg !== lastErrorMessage) {
|
||||
lastErrorTime = now
|
||||
lastErrorMessage = msg
|
||||
message.error(msg)
|
||||
}
|
||||
}
|
||||
|
||||
const isAuthFreePath = (url?: string) => {
|
||||
return Boolean(url?.includes('/auth/login') || url?.includes('/auth/refresh'))
|
||||
}
|
||||
|
||||
const handleAdminTokenExpired = () => {
|
||||
localStorage.removeItem('adminAccessToken')
|
||||
localStorage.removeItem('adminRefreshToken')
|
||||
localStorage.removeItem('adminUserInfo')
|
||||
localStorage.removeItem('userPermissions')
|
||||
sessionStorage.removeItem('adminAccessToken')
|
||||
sessionStorage.removeItem('adminRefreshToken')
|
||||
|
||||
if (Date.now() - lastErrorTime > ERROR_DEBOUNCE_TIME || lastErrorMessage !== '登录已过期,请重新登录') {
|
||||
showError('登录已过期,请重新登录')
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
try {
|
||||
router.push('/admin/login').catch(() => {
|
||||
window.location.href = '/admin/login'
|
||||
})
|
||||
} catch {
|
||||
window.location.href = '/admin/login'
|
||||
}
|
||||
}, 300)
|
||||
}
|
||||
|
||||
adminApi.interceptors.request.use(
|
||||
config => {
|
||||
// 登录请求不需要携带Token
|
||||
if (!config.url?.includes('/auth/login')) {
|
||||
if (!isAuthFreePath(config.url)) {
|
||||
let token = localStorage.getItem('adminAccessToken')
|
||||
if (!token) {
|
||||
token = sessionStorage.getItem('adminAccessToken')
|
||||
@@ -30,9 +81,7 @@ adminApi.interceptors.request.use(
|
||||
}
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
error => Promise.reject(error)
|
||||
)
|
||||
|
||||
adminApi.interceptors.response.use(
|
||||
@@ -41,7 +90,7 @@ adminApi.interceptors.response.use(
|
||||
if (res.code !== 200) {
|
||||
const errorMsg = res.message || '请求失败'
|
||||
if (!errorMsg.includes('权限') && !errorMsg.includes('permission')) {
|
||||
message.error(errorMsg)
|
||||
showError(errorMsg)
|
||||
} else {
|
||||
console.error('权限不足:', errorMsg)
|
||||
}
|
||||
@@ -50,35 +99,56 @@ adminApi.interceptors.response.use(
|
||||
return res.data as any
|
||||
},
|
||||
async (error: AxiosError) => {
|
||||
// 防抖处理错误提示
|
||||
const showError = (msg: string) => {
|
||||
const now = Date.now()
|
||||
if (now - lastErrorTime > ERROR_DEBOUNCE_TIME || msg !== lastErrorMessage) {
|
||||
lastErrorTime = now
|
||||
lastErrorMessage = msg
|
||||
message.error(msg)
|
||||
}
|
||||
}
|
||||
const originalRequest = error.config as any
|
||||
|
||||
if (error.response?.status === 401) {
|
||||
// 登录过期,只提示一次
|
||||
if (Date.now() - lastErrorTime > ERROR_DEBOUNCE_TIME || lastErrorMessage !== '登录已过期,请重新登录') {
|
||||
localStorage.removeItem('adminAccessToken')
|
||||
localStorage.removeItem('adminRefreshToken')
|
||||
localStorage.removeItem('adminUserInfo')
|
||||
sessionStorage.removeItem('adminAccessToken')
|
||||
showError('登录已过期,请重新登录')
|
||||
try {
|
||||
router.push('/admin/login').catch(err => {
|
||||
console.error('跳转到登录页失败:', err)
|
||||
window.location.href = '/admin/login'
|
||||
if (!originalRequest?._retry && !isAuthFreePath(originalRequest?.url)) {
|
||||
if (isRefreshing) {
|
||||
return new Promise((resolve, reject) => {
|
||||
failedQueue.push({ resolve, reject })
|
||||
}).then(token => {
|
||||
if (token) {
|
||||
originalRequest.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return adminApi(originalRequest)
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('跳转到登录页异常:', err)
|
||||
window.location.href = '/admin/login'
|
||||
}
|
||||
|
||||
originalRequest._retry = true
|
||||
isRefreshing = true
|
||||
|
||||
try {
|
||||
let refreshToken = localStorage.getItem('adminRefreshToken')
|
||||
if (!refreshToken) {
|
||||
refreshToken = sessionStorage.getItem('adminRefreshToken')
|
||||
}
|
||||
if (!refreshToken) {
|
||||
throw new Error('No refresh token available')
|
||||
}
|
||||
|
||||
const response = await adminAuthApi.refreshToken(refreshToken)
|
||||
const newToken = (response as any).accessToken
|
||||
const newRefreshToken = (response as any).refreshToken
|
||||
|
||||
localStorage.setItem('adminAccessToken', newToken)
|
||||
localStorage.setItem('adminRefreshToken', newRefreshToken)
|
||||
sessionStorage.setItem('adminAccessToken', newToken)
|
||||
sessionStorage.setItem('adminRefreshToken', newRefreshToken)
|
||||
|
||||
processQueue(null, newToken)
|
||||
originalRequest.headers.Authorization = `Bearer ${newToken}`
|
||||
return adminApi(originalRequest)
|
||||
} catch (refreshError) {
|
||||
processQueue(refreshError, null)
|
||||
handleAdminTokenExpired()
|
||||
return Promise.reject(refreshError)
|
||||
} finally {
|
||||
isRefreshing = false
|
||||
}
|
||||
}
|
||||
} else if (error.response?.status !== 403) { // 403权限不足不提示
|
||||
|
||||
handleAdminTokenExpired()
|
||||
} else if (error.response?.status !== 403) {
|
||||
const errorMsg = (error.response?.data as any)?.message || error.message || '请求失败'
|
||||
showError(errorMsg)
|
||||
} else {
|
||||
@@ -102,10 +172,28 @@ export interface AdminLoginResponse {
|
||||
userInfo: any
|
||||
}
|
||||
|
||||
export interface LoginShopSelectionResponse {
|
||||
allowShoplessLogin: boolean
|
||||
message: string
|
||||
shops: Array<{
|
||||
id: number | string
|
||||
shopName: string
|
||||
}>
|
||||
}
|
||||
|
||||
export const adminAuthApi = {
|
||||
login: (data: AdminLoginRequest) => {
|
||||
return adminApi.post<AdminLoginResponse>('/auth/login', data)
|
||||
},
|
||||
getLoginShops: (account: string) => {
|
||||
return adminApi.get<LoginShopSelectionResponse>(`/auth/login-shops/${encodeURIComponent(account)}`)
|
||||
},
|
||||
logout: () => {
|
||||
return adminApi.post<boolean>('/auth/logout')
|
||||
},
|
||||
switchShop: (shopId?: string | number) => {
|
||||
return adminApi.post<AdminLoginResponse>('/auth/switch-shop', { shopId })
|
||||
},
|
||||
refreshToken: (refreshToken: string) => {
|
||||
return adminApi.post<{ accessToken: string; refreshToken: string }>('/auth/refresh', { refreshToken })
|
||||
}
|
||||
@@ -275,6 +363,7 @@ export interface Shop {
|
||||
description: string
|
||||
address: string
|
||||
businessHours: string
|
||||
status?: number
|
||||
coverImageFileId?: string
|
||||
coverImageUrls?: string[]
|
||||
imageFileIds?: string
|
||||
@@ -297,6 +386,7 @@ export interface CreateShopRequest {
|
||||
businessHours: string
|
||||
coverImageFileId?: string
|
||||
imageFileIds?: string
|
||||
status?: number
|
||||
}
|
||||
|
||||
export const adminShopApi = {
|
||||
@@ -312,6 +402,9 @@ export const adminShopApi = {
|
||||
updateShop: (id: number, data: CreateShopRequest) => {
|
||||
return adminApi.put<boolean>(`/shops/update/${id}`, data)
|
||||
},
|
||||
updateShopStatus: (id: number, status: number) => {
|
||||
return adminApi.put<boolean>(`/shops/status/${id}`, { status })
|
||||
},
|
||||
deleteShop: (id: number) => {
|
||||
return adminApi.delete<boolean>(`/shops/delete/${id}`)
|
||||
}
|
||||
@@ -321,6 +414,8 @@ export interface UserVO {
|
||||
id: number
|
||||
shopId?: number
|
||||
shopName?: string
|
||||
shopIds?: Array<number | string>
|
||||
shopNames?: string[]
|
||||
nickname: string
|
||||
account: string
|
||||
avatar: string
|
||||
@@ -429,7 +524,6 @@ export interface AdminUserVipOverrideRequest {
|
||||
remark?: string
|
||||
}
|
||||
|
||||
// 别名导出,兼容旧代码
|
||||
export const userApi = adminUserApi
|
||||
|
||||
export interface OrderVO {
|
||||
@@ -460,16 +554,18 @@ export const adminOrderApi = {
|
||||
}
|
||||
}
|
||||
|
||||
// 仪表盘相关接口
|
||||
export interface HotProductVO {
|
||||
productId: number
|
||||
productName: string
|
||||
orderCount: number
|
||||
id: number
|
||||
title: string
|
||||
price: number
|
||||
likeCount: number
|
||||
collectCount: number
|
||||
}
|
||||
|
||||
export interface RecentActivityVO {
|
||||
id: number
|
||||
type: string
|
||||
title?: string
|
||||
description: string
|
||||
time: string
|
||||
color?: string
|
||||
|
||||
Reference in New Issue
Block a user