liveh5-nuxt/app/api/http.js
xingyy 6f67273a9a refactor(store): 重构 home store 并改名为 goods store
- 将 home store 重命名为 goods store,以更准确地反映其用途
- 更新了相关文件中的导入路径和引用
-调整了首页布局和组件以适应新的 goods store 结构
- 新增了 goods store 中的 actionDetails 和 itemList 属性
2025-01-17 16:56:13 +08:00

58 lines
1.4 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()
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)
}
},
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
}