type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' // 使用宽松签名,返回类型由调用方通过泛型指定 const rawFetch = $fetch as unknown as (url: string, opts?: { method?: Method, body?: unknown }) => Promise /** 统一的接口调用:自动弹出错误提示 */ export function useApi() { const toast = useToast() async function call(url: string, method: Method = 'GET', body?: unknown, opts: { silent?: boolean, title?: string } = {}): Promise { try { return await rawFetch(url, { method, body }) } catch (err) { if (!opts.silent) toast.add({ title: opts.title ?? '操作失败', description: apiErrorMessage(err), color: 'error', icon: 'i-lucide-circle-alert' }) throw err } } return { get: (url: string) => call(url, 'GET'), post: (url: string, body?: unknown, opts?: { silent?: boolean }) => call(url, 'POST', body ?? {}, opts), put: (url: string, body?: unknown) => call(url, 'PUT', body ?? {}), del: (url: string) => call(url, 'DELETE'), upload: (url: string, form: FormData) => call(url, 'POST', form, { title: '上传失败' }), toast } }