更新:登录功能
This commit is contained in:
32
api/interceptors/request.ts
Normal file
32
api/interceptors/request.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
// 片段示例 - 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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user