- 在 http.js 中添加了对 401 状态码的处理,重定向到登录页面 - 更新了 LiveRoom 组件,暂时注释掉了 initializePlayer 方法 - 在 goods store 中添加了 currentItem 和 artWorkDetail 两个状态 - 更新了 message 组件,添加了对错误信息的处理 -调整了首页布局,移除了多余的 transition 标签 - 更新了登录页面的默认验证码
62 lines
1.5 KiB
JavaScript
62 lines
1.5 KiB
JavaScript
|
|
import { useRuntimeConfig } from '#app'
|
|
import { ofetch } from 'ofetch'
|
|
import {message} from '@/components/x-message/useMessage.js'
|
|
import {authStore} from "@/stores/auth/index.js";
|
|
let httpStatusErrorHandler
|
|
|
|
let http
|
|
|
|
export function setupHttp() {
|
|
if (http)
|
|
return http
|
|
|
|
const config = useRuntimeConfig()
|
|
const baseURL = config.public.NUXT_PUBLIC_API_BASE
|
|
const {token}= authStore()
|
|
const router = useRouter()
|
|
http = ofetch.create({
|
|
baseURL,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
async onRequest({ options }) {
|
|
options.headers = {
|
|
...options.headers,
|
|
Authorization:token.value
|
|
}
|
|
},
|
|
async onResponse({ response }) {
|
|
if (response._data.status===1){
|
|
message.error(response._data.msg)
|
|
}
|
|
if (response._data.status===401){
|
|
router.replace('/login')
|
|
}
|
|
},
|
|
async onResponseError({ response }) {
|
|
const { message } = response._data
|
|
if (Array.isArray(message)) {
|
|
message.forEach((item) => {
|
|
httpStatusErrorHandler?.(item, response.status)
|
|
})
|
|
}
|
|
else {
|
|
httpStatusErrorHandler?.(message, response.status)
|
|
}
|
|
return Promise.reject(response._data)
|
|
},
|
|
retry: 3,
|
|
retryDelay: 1000,
|
|
})
|
|
}
|
|
|
|
export function injectHttpStatusErrorHandler(handler) {
|
|
httpStatusErrorHandler = handler
|
|
}
|
|
|
|
export function getHttp() {
|
|
if (!http) {
|
|
throw new Error('HTTP client not initialized. Call setupHttp first.')
|
|
}
|
|
return http
|
|
}
|