import { useRuntimeConfig } from '#app' import { ofetch } from 'ofetch' import {message} from '@/components/x-message/useMessage.js' let httpStatusErrorHandler let http export function setupHttp() { if (http) return http const config = useRuntimeConfig() const baseURL = config.public.NUXT_PUBLIC_API_BASE http = ofetch.create({ baseURL, headers: { 'Content-Type': 'application/json' }, async onRequest({ options }) { const token = localStorage.getItem('token') options.headers = { ...options.headers, ...(token && { Authorization: token }), } }, async onResponse({ response }) { if (response._data.status===1){ message.warning(response._data.msg) } }, async onResponseError({ response }) { console.log('error错误') 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 }