恢复修改的数据
This commit is contained in:
@@ -7,6 +7,16 @@ import { storage } from '@/utils/storage'
|
||||
// 鉴权头 Authorization: Bearer <token> 已与后端 sa-token.token-prefix=Bearer 对齐,无需改动。
|
||||
const SUCCESS_CODE = 200
|
||||
|
||||
/**
|
||||
* PC 端 clientId(sys_client 种子数据,grantType 含 password)。
|
||||
*
|
||||
* 后端 SecurityConfig 对每个已登录请求做多端隔离校验:
|
||||
* header / query 里的 clientid 必须与登录时写入 token 的 clientid 一致,
|
||||
* 否则抛 NotLoginException「客户端ID与Token不匹配」。
|
||||
* 所以此值必须与登录、注册请求体的 clientId 完全同源。
|
||||
*/
|
||||
export const CLIENT_ID = 'e5cd7e4891bf95d1d19206ce24a7b32e'
|
||||
|
||||
/** 全项目唯一 axios 实例 */
|
||||
const service = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_BASE_URL,
|
||||
@@ -18,6 +28,8 @@ service.interceptors.request.use((config) => {
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
// 带 token 的请求必须回传 clientid,否则后端 SecurityConfig:97-106 校验不通过
|
||||
config.headers.clientid = CLIENT_ID
|
||||
return config
|
||||
})
|
||||
|
||||
@@ -26,6 +38,35 @@ service.interceptors.response.use(undefined, (error) => {
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
/**
|
||||
* 查询参数序列化(按 Spring MVC WebDataBinder 的绑定约定)。
|
||||
*
|
||||
* <p>axios 默认把嵌套对象序列化成 `scales[0][lowerLimit]`,而 Spring 只认点号路径
|
||||
* `scales[0].lowerLimit`——实测用默认形式后端直接 HTTP 500,故需要序列化的接口
|
||||
* 统一走本函数(`paramsSerializer: { serialize: serializeQuery }`)。</p>
|
||||
*/
|
||||
export function serializeQuery(params: Record<string, unknown>): string {
|
||||
const parts: string[] = []
|
||||
|
||||
function append(key: string, value: unknown): void {
|
||||
if (value === undefined || value === null || value === '') return
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((item, index) => append(`${key}[${index}]`, item))
|
||||
return
|
||||
}
|
||||
if (typeof value === 'object') {
|
||||
for (const [subKey, subValue] of Object.entries(value as Record<string, unknown>)) {
|
||||
append(`${key}.${subKey}`, subValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(params)) append(key, value)
|
||||
return parts.join('&')
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一请求:自动解包 ApiResponse<T>,失败统一提示并 reject
|
||||
* 页面/组件禁止直接 import axios,一律经 store 调用本函数
|
||||
|
||||
Reference in New Issue
Block a user