longz
2026-04-27 25df88d0ab9d7444ed1d162b9efa1259ac2f6f66
yuyueMiniProgram/src/api/core/instance.ts
@@ -3,17 +3,22 @@
import vueHook from 'alova/vue'
import mockAdapter from '../mock/mockAdapter'
import { handleAlovaError, handleAlovaResponse } from './handlers'
import { getPersistedToken } from '@/store/auth'
export const alovaInstance = createAlova({
  baseURL: import.meta.env.VITE_API_BASE_URL || 'https://petstore3.swagger.io/api/v3',
  baseURL: import.meta.env.VITE_API_BASE_URL,
  ...AdapterUniapp({
    mockRequest: mockAdapter,
  }),
  statesHook: vueHook,
  beforeRequest: (method) => {
    method.config.headers = method.config.headers || {}
    ;(method.config as Record<string, any>).header = (method.config as Record<string, any>).header || {}
    // Add content type for POST/PUT/PATCH requests
    if (['POST', 'PUT', 'PATCH'].includes(method.type)) {
      method.config.headers['Content-Type'] = 'application/json'
      ;(method.config as Record<string, any>).header['Content-Type'] = 'application/json'
    }
    // Add timestamp to prevent caching for GET requests
@@ -21,6 +26,12 @@
      method.config.params._t = Date.now()
    }
    const token = getPersistedToken()
    if (token) {
      method.config.headers['Mini-Authorization'] = token
      ;(method.config as Record<string, any>).header['Mini-Authorization'] = token
    }
    // Log request in development
    if (import.meta.env.MODE === 'development') {
      console.log(`[Alova Request] ${method.type} ${method.url}`, method.data || method.config.params)