fix(types): 修复TS类型错误

This commit is contained in:
Soybean 2021-10-13 11:13:05 +08:00
parent 50c1d51e87
commit 45d31a0f56

View File

@ -40,18 +40,20 @@ export default class CustomAxiosInstance {
this.instance.interceptors.request.use( this.instance.interceptors.request.use(
async config => { async config => {
const handleConfig = { ...config }; const handleConfig = { ...config };
if (handleConfig.headers) {
// form类型转换 // form类型转换
if (handleConfig.headers['Content-Type'] === ContentType.formUrlencoded) { if (handleConfig.headers['Content-Type'] === ContentType.formUrlencoded) {
handleConfig.data = qs.stringify(handleConfig.data); handleConfig.data = qs.stringify(handleConfig.data);
} }
// 文件类型转换 // 文件类型转换
if (handleConfig.headers['Content-Type'] === ContentType.formData) { if (handleConfig?.headers['Content-Type'] === ContentType.formData) {
const key = Object.keys(handleConfig.data)[0]; const key = Object.keys(handleConfig.data)[0];
const file = handleConfig.data[key]; const file = handleConfig.data[key];
handleConfig.data = await transformFile(file, key); handleConfig.data = await transformFile(file, key);
} }
// 设置token // 设置token
handleConfig.headers.Authorization = getToken(); handleConfig.headers.Authorization = getToken();
}
return handleConfig; return handleConfig;
}, },
error => { error => {
@ -64,11 +66,12 @@ export default class CustomAxiosInstance {
const { status, data } = response; const { status, data } = response;
const { statusKey, msgKey, successCode } = statusConfig; const { statusKey, msgKey, successCode } = statusConfig;
if (status === 200 || status < 300 || status === 304) { if (status === 200 || status < 300 || status === 304) {
if (data[statusKey] === successCode) { const responseData = data as any;
return Promise.resolve(data.data); if (responseData[statusKey] === successCode) {
return Promise.resolve(responseData.data);
} }
window.$message?.error(data[msgKey]); window.$message?.error(responseData[msgKey]);
return Promise.reject(data[msgKey]); return Promise.reject(responseData[msgKey]);
} }
const error = { response }; const error = { response };
errorHandler(error); errorHandler(error);