| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import AdapterUniapp from '@alova/adapter-uniapp'
- import { createAlova } from 'alova'
- import vueHook from 'alova/vue'
- import { handleAlovaError, handleAlovaResponse } from './handlers'
- import { BASE_URL } from '@/config'
- export const alovaInstance = createAlova({
- baseURL: BASE_URL,
- ...AdapterUniapp(),
- statesHook: vueHook,
- beforeRequest: (method) => {
- // Add content type for POST/PUT/PATCH requests
- if (['POST', 'PUT', 'PATCH'].includes(method.type)) {
- method.config.headers['Content-Type'] = 'application/json'
- }
- const { token } = useUserStore()
- method.config.headers.Authorization = token || 'Basic c21xamgtYXBwbGV0OjEyMzQ1Ng=='
- const { tenantCode } = storeToRefs(useSysStore())
- const code = tenantCode.value || 'zswl'
- method.config.headers['X-Tenant-code'] = code
- console.log(code, 'tenantCode======================')
- if (method.type === 'GET' && CommonUtil.isObj(method.config.params)) {
- method.config.params._t = Date.now()
- }
- // Log request in development
- if (import.meta.env.MODE === 'development') {
- console.log(
- `[Alova Request] ${method.type} ${method.url}`,
- method.data || method.config.params,
- )
- }
- console.log(
- BASE_URL,
- '=================所连服务器==========================',
- )
- },
- // Response handlers
- responded: {
- // Success handler
- onSuccess: handleAlovaResponse,
- // Error handler
- onError: handleAlovaError,
- // Complete handler - runs after success or error
- },
- // We'll use the middleware in the hooks
- // middleware is not directly supported in createAlova options
- // Default request timeout (10 seconds)
- timeout: 60000,
- // 设置为null即可全局关闭全部请求缓存
- cacheFor: null,
- })
- export default alovaInstance
|