新增接单平台第一期闭环
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import axios from 'axios'
|
||||
import { clearAdminSession, getAdminToken } from '@/utils/admin-auth'
|
||||
import { clearWorkerSession, getWorkerToken } from '@/utils/worker-auth'
|
||||
|
||||
export interface ApiEnvelope<T> {
|
||||
code: number
|
||||
@@ -66,6 +67,22 @@ http.interceptors.response.use(
|
||||
}
|
||||
}
|
||||
|
||||
if (error?.config?.url && String(error.config.url).startsWith('/api/v1/worker')) {
|
||||
const status = Number(error?.response?.status || 0)
|
||||
const errorCode = String(error?.response?.data?.errorCode || '').trim()
|
||||
|
||||
if (status === 401 && shouldClearWorkerSession(errorCode)) {
|
||||
clearWorkerSession()
|
||||
|
||||
if (
|
||||
window.location.hash.startsWith('#/worker') &&
|
||||
!window.location.hash.startsWith('#/worker/login')
|
||||
) {
|
||||
window.location.hash = '#/worker/login'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject(normalizedError)
|
||||
},
|
||||
)
|
||||
@@ -79,6 +96,14 @@ http.interceptors.request.use((config) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (String(config.url || '').startsWith('/api/v1/worker')) {
|
||||
const token = getWorkerToken()
|
||||
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
}
|
||||
|
||||
return config
|
||||
})
|
||||
|
||||
@@ -110,6 +135,16 @@ function shouldClearAdminSession(errorCode: string) {
|
||||
].includes(errorCode)
|
||||
}
|
||||
|
||||
function shouldClearWorkerSession(errorCode: string) {
|
||||
return [
|
||||
'worker_auth_required',
|
||||
'worker_auth_invalid',
|
||||
'worker_auth_expired',
|
||||
'worker_auth_user_invalid',
|
||||
'worker_auth_stale',
|
||||
].includes(errorCode)
|
||||
}
|
||||
|
||||
function request<T>(config: Parameters<typeof http.request<ApiEnvelope<T>>>[0]) {
|
||||
return http.request<ApiEnvelope<T>, ApiEnvelope<T>>(config)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user