重构云文件管理系统
This commit is contained in:
@@ -14,6 +14,9 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import com.filesystem.utils.CommonUtil;
|
||||||
|
import com.filesystem.utils.FileUtil;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|||||||
@@ -20,8 +20,25 @@ request.interceptors.response.use(
|
|||||||
response => {
|
response => {
|
||||||
const res = response.data
|
const res = response.data
|
||||||
|
|
||||||
// 如果是二进制数据(blob),直接返回
|
// 如果是二进制数据(blob),需要检查是否为错误响应
|
||||||
if (response.config.responseType === 'blob') {
|
if (response.config.responseType === 'blob') {
|
||||||
|
// 检查 Content-Type,如果是 JSON 说明是错误响应
|
||||||
|
const contentType = response.headers['content-type'] || ''
|
||||||
|
if (contentType.includes('application/json')) {
|
||||||
|
// 将 blob 转换为 JSON 并抛出错误
|
||||||
|
return res.text().then(text => {
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(text)
|
||||||
|
const error = new Error(json.message || '请求失败')
|
||||||
|
error.code = json.code
|
||||||
|
error.response = response
|
||||||
|
return Promise.reject(error)
|
||||||
|
} catch (e) {
|
||||||
|
return Promise.reject(new Error('请求失败'))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// 真正的二进制数据,直接返回
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,6 +64,20 @@ request.interceptors.response.use(
|
|||||||
error.code = data.code
|
error.code = data.code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理 blob 类型的错误响应
|
||||||
|
if (data instanceof Blob) {
|
||||||
|
return data.text().then(text => {
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(text)
|
||||||
|
error.message = json.message || '请求失败'
|
||||||
|
error.code = json.code
|
||||||
|
} catch (e) {
|
||||||
|
// 无法解析为 JSON
|
||||||
|
}
|
||||||
|
return Promise.reject(error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// 只有 401/403 才清理 token 并跳转登录页
|
// 只有 401/403 才清理 token 并跳转登录页
|
||||||
// 但在登录页时不跳转(避免死循环),登录接口的 401 也不跳转
|
// 但在登录页时不跳转(避免死循环),登录接口的 401 也不跳转
|
||||||
if (status === 401 || status === 403) {
|
if (status === 401 || status === 403) {
|
||||||
|
|||||||
Reference in New Issue
Block a user