解决中文编码错误
This commit is contained in:
@@ -26,6 +26,28 @@ function pickValueFromSources(sources, keys) {
|
||||
return ''
|
||||
}
|
||||
|
||||
/** 浏览器 XHR 要求请求头值为 ISO-8859-1,含中文等会触发 setRequestHeader 异常 */
|
||||
function isHeaderValueLatin1Safe(value) {
|
||||
if (value == null) return true
|
||||
const s = String(value)
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
if (s.charCodeAt(i) > 255) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/** 写入 XMLHttpRequest 前的业务头安全化;账号名常用中文展示,需回退到手机号等 ASCII */
|
||||
function coerceBusinessHeaderValueForXHR(headerKey, rawValue) {
|
||||
const v = rawValue == null ? '' : String(rawValue).trim()
|
||||
if (v === '') return ''
|
||||
if (isHeaderValueLatin1Safe(v)) return v
|
||||
if (headerKey === 'X-Account-Name') {
|
||||
const phone = store.getters.phone ? String(store.getters.phone).trim() : ''
|
||||
if (phone && isHeaderValueLatin1Safe(phone)) return phone
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
// create an axios instance
|
||||
const service = axios.create({
|
||||
baseURL: '/api', // 统一使用 /api 前缀
|
||||
@@ -51,7 +73,10 @@ service.interceptors.request.use(
|
||||
|
||||
const businessHeaders = getBusinessHeaders()
|
||||
Object.keys(businessHeaders).forEach((key) => {
|
||||
config.headers[key] = businessHeaders[key]
|
||||
const safe = coerceBusinessHeaderValueForXHR(key, businessHeaders[key])
|
||||
if (safe !== '') {
|
||||
config.headers[key] = safe
|
||||
}
|
||||
})
|
||||
return config
|
||||
},
|
||||
@@ -87,17 +112,33 @@ service.interceptors.response.use(
|
||||
),
|
||||
'X-Account-Name': pickValueFromSources(
|
||||
[response.headers, response.data?.data, response.data],
|
||||
['X-Account-Name', 'x-account-name', 'accountName', 'account_name', 'loginAccount', 'userName', 'username']
|
||||
[
|
||||
'X-Account-Name', 'x-account-name',
|
||||
'phone', 'mobile', 'telephone',
|
||||
'loginAccount', 'username',
|
||||
'accountName', 'account_name',
|
||||
'userName'
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
if (response.config?.url?.includes('/sys/auth/login')) {
|
||||
const loginPayload = response.data?.data || {}
|
||||
const normalizedFromHeader = {
|
||||
'X-Role-Name': pickHeaderCaseInsensitive(response.headers, 'X-Role-Name') || loginHeaderValues['X-Role-Name'],
|
||||
'X-Scenario': pickHeaderCaseInsensitive(response.headers, 'X-Scenario') || loginHeaderValues['X-Scenario'],
|
||||
'X-Tenant-Id': pickHeaderCaseInsensitive(response.headers, 'X-Tenant-Id') || loginHeaderValues['X-Tenant-Id'],
|
||||
'X-Account-Name': pickHeaderCaseInsensitive(response.headers, 'X-Account-Name') || loginHeaderValues['X-Account-Name']
|
||||
}
|
||||
if (!isHeaderValueLatin1Safe(normalizedFromHeader['X-Account-Name'])) {
|
||||
const asciiAccount = pickValueFromSources(
|
||||
[loginPayload],
|
||||
['phone', 'mobile', 'telephone', 'loginAccount', 'username']
|
||||
)
|
||||
if (asciiAccount && isHeaderValueLatin1Safe(asciiAccount)) {
|
||||
normalizedFromHeader['X-Account-Name'] = asciiAccount
|
||||
}
|
||||
}
|
||||
setBusinessHeaders(normalizedFromHeader)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user