0508修改
This commit is contained in:
@@ -14,6 +14,12 @@ const request = (option: any) => {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export interface DownloadResponseData {
|
||||
data: Blob
|
||||
fileName?: string
|
||||
}
|
||||
|
||||
export default {
|
||||
get: async <T = any>(option: any) => {
|
||||
const res = await request({ method: 'GET', ...option })
|
||||
@@ -35,9 +41,9 @@ export default {
|
||||
const res = await request({ method: 'PUT', ...option })
|
||||
return res.data as unknown as T
|
||||
},
|
||||
download: async <T = any>(option: any) => {
|
||||
download: async <T = DownloadResponseData>(option: any) => {
|
||||
const res = await request({ method: 'GET', responseType: 'blob', ...option })
|
||||
return res as unknown as Promise<T>
|
||||
return res as T
|
||||
},
|
||||
upload: async <T = any>(option: any) => {
|
||||
option.headersType = 'multipart/form-data'
|
||||
|
||||
@@ -20,6 +20,29 @@ import { ApiEncrypt } from '@/utils/encrypt'
|
||||
const tenantEnable = import.meta.env.VITE_APP_TENANT_ENABLE
|
||||
const { result_code, base_url, request_timeout } = config
|
||||
|
||||
const resolveDownloadFileName = (contentDisposition?: string) => {
|
||||
if (!contentDisposition) {
|
||||
return undefined
|
||||
}
|
||||
const decodeFileName = (value: string) => {
|
||||
const normalizedValue = value.trim().replace(/^"(.*)"$/, '$1').replace(/\+/g, '%20')
|
||||
try {
|
||||
return decodeURIComponent(normalizedValue)
|
||||
} catch (error) {
|
||||
return normalizedValue
|
||||
}
|
||||
}
|
||||
const filenameStarMatch = contentDisposition.match(/filename\*\s*=\s*([^;]+)/i)
|
||||
if (filenameStarMatch?.[1]) {
|
||||
return decodeFileName(filenameStarMatch[1].replace(/^UTF-8''/i, ''))
|
||||
}
|
||||
const filenameMatch = contentDisposition.match(/filename\s*=\s*([^;]+)/i)
|
||||
if (filenameMatch?.[1]) {
|
||||
return decodeFileName(filenameMatch[1])
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
// 需要忽略的提示。忽略后,自动 Promise.reject('error')
|
||||
const ignoreMsgs = [
|
||||
'无效的刷新令牌', // 刷新令牌被删除时,不用提示
|
||||
@@ -141,7 +164,10 @@ service.interceptors.response.use(
|
||||
) {
|
||||
// 注意:如果导出的响应为 json,说明可能失败了,不直接返回进行下载
|
||||
if (response.data.type !== 'application/json') {
|
||||
return response.data
|
||||
return {
|
||||
data: response.data,
|
||||
fileName: resolveDownloadFileName(response.headers['content-disposition'])
|
||||
}
|
||||
}
|
||||
data = await new Response(response.data).json()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user