功能:完善订单流程和加固
This commit is contained in:
@@ -8,6 +8,7 @@ import { ADMIN_API_BASE_URL } from '../config/api'
|
||||
import type { IdType } from '@/utils/id'
|
||||
import { clearAdminSessionStorage } from '@/utils/admin-session'
|
||||
import { attachRequestSignature } from '@/utils/request-signature'
|
||||
import { isAdminApiAuthFreePath } from './auth-free-paths'
|
||||
|
||||
let lastErrorTime = 0
|
||||
let lastErrorMessage = ''
|
||||
@@ -38,6 +39,25 @@ const processQueue = (error: any, token: string | null = null) => {
|
||||
failedQueue = []
|
||||
}
|
||||
|
||||
const getStoredAdminAccessToken = () => {
|
||||
return localStorage.getItem('adminAccessToken') || sessionStorage.getItem('adminAccessToken')
|
||||
}
|
||||
|
||||
const getRequestToken = (request: any) => {
|
||||
if (request?._authToken) {
|
||||
return request._authToken as string
|
||||
}
|
||||
const authHeader = request?.headers?.Authorization || request?.headers?.authorization
|
||||
if (typeof authHeader !== 'string') {
|
||||
return null
|
||||
}
|
||||
return authHeader.startsWith('Bearer ') ? authHeader.slice('Bearer '.length) : null
|
||||
}
|
||||
|
||||
const isCurrentStoredAdminToken = (token: string | null) => {
|
||||
return Boolean(token && token === getStoredAdminAccessToken())
|
||||
}
|
||||
|
||||
const showError = (msg: string) => {
|
||||
const now = Date.now()
|
||||
if (now - lastErrorTime > ERROR_DEBOUNCE_TIME || msg !== lastErrorMessage) {
|
||||
@@ -48,10 +68,14 @@ const showError = (msg: string) => {
|
||||
}
|
||||
|
||||
const isAuthFreePath = (url?: string) => {
|
||||
return Boolean(url?.includes('/auth/login') || url?.includes('/auth/login-shops/') || url?.includes('/auth/refresh'))
|
||||
return isAdminApiAuthFreePath(url)
|
||||
}
|
||||
|
||||
const handleAdminTokenExpired = () => {
|
||||
const handleAdminTokenExpired = (failedToken: string | null = null) => {
|
||||
if (!isCurrentStoredAdminToken(failedToken)) {
|
||||
return
|
||||
}
|
||||
|
||||
clearAdminSessionStorage()
|
||||
|
||||
if (Date.now() - lastErrorTime > ERROR_DEBOUNCE_TIME || lastErrorMessage !== '登录已过期,请重新登录') {
|
||||
@@ -67,14 +91,12 @@ adminApi.interceptors.request.use(
|
||||
async config => {
|
||||
let token: string | null = null
|
||||
if (!isAuthFreePath(config.url)) {
|
||||
token = localStorage.getItem('adminAccessToken')
|
||||
if (!token) {
|
||||
token = sessionStorage.getItem('adminAccessToken')
|
||||
}
|
||||
token = getStoredAdminAccessToken()
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
}
|
||||
;(config as any)._authToken = token || null
|
||||
return attachRequestSignature(config, token, {
|
||||
authFree: isAuthFreePath(config.url)
|
||||
})
|
||||
@@ -100,6 +122,12 @@ adminApi.interceptors.response.use(
|
||||
const originalRequest = error.config as any
|
||||
|
||||
if (error.response?.status === 401) {
|
||||
const requestToken = getRequestToken(originalRequest)
|
||||
|
||||
if (!isCurrentStoredAdminToken(requestToken)) {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
if (!originalRequest?._retry && !isAuthFreePath(originalRequest?.url)) {
|
||||
if (isRefreshing) {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -138,14 +166,14 @@ adminApi.interceptors.response.use(
|
||||
return adminApi(originalRequest)
|
||||
} catch (refreshError) {
|
||||
processQueue(refreshError, null)
|
||||
handleAdminTokenExpired()
|
||||
handleAdminTokenExpired(requestToken)
|
||||
return Promise.reject(refreshError)
|
||||
} finally {
|
||||
isRefreshing = false
|
||||
}
|
||||
}
|
||||
|
||||
handleAdminTokenExpired()
|
||||
handleAdminTokenExpired(requestToken)
|
||||
} else if (error.response?.status !== 403) {
|
||||
const errorMsg = (error.response?.data as any)?.message || error.message || '请求失败'
|
||||
showError(errorMsg)
|
||||
@@ -728,14 +756,6 @@ export const adminShopApi = {
|
||||
*/
|
||||
updateShopStatus: (id: IdType, status: number) => {
|
||||
return adminApi.put<boolean>(`/shops/status/${id}`, { status })
|
||||
},
|
||||
/**
|
||||
* `deleteShop` API call. Use this method from the page script to invoke the backend endpoint tied to this feature.
|
||||
* Call guide: supply the parameters below, await the returned Promise, and handle loading / error UI in the caller.
|
||||
* @param id: IdType caller-supplied value for `id: IdType`
|
||||
*/
|
||||
deleteShop: (id: IdType) => {
|
||||
return adminApi.delete<boolean>(`/shops/delete/${id}`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user