Files
taimed-international-app/api/interceptors/request.ts
2025-11-04 12:37:04 +08:00

33 lines
980 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 片段示例 - requestInterceptor 更稳健的写法
import type { IRequestOptions } from '../types'
import { useUserStore } from '@/stores/user'
import { APP_INFO } from '@/api/config'
export function requestInterceptor(options: IRequestOptions): IRequestOptions {
const headers = { ...(options.headers || {}) }
// 更明确地调用 useUserStore
let token = ''
try {
const userStore = typeof useUserStore === 'function' ? useUserStore() : null
token = userStore?.token || uni.getStorageSync('token') || ''
} catch (e) {
token = uni.getStorageSync('token') || ''
}
if (token) headers.token = token
// Content-Type只有在没有 files 时才默认为 application/json
if (!options.files && !headers['Content-Type']) {
headers['Content-Type'] = 'application/json;charset=UTF-8'
}
headers['appType'] = APP_INFO.TYPE
headers['version_code'] = APP_INFO.VERSION_CODE || '1.0.0'
return {
...options,
header: headers,
}
}