refactor(projects): 代码优化

This commit is contained in:
Soybean
2022-07-16 00:13:19 +08:00
parent 872bb84502
commit 6143605297
24 changed files with 107 additions and 104 deletions

View File

@ -15,7 +15,7 @@ type ErrorStatus = keyof typeof ERROR_STATUS;
/**
* 处理axios请求失败的错误
* @param error - 错误
* @param axiosError - 错误
*/
export function handleAxiosError(axiosError: AxiosError) {
const error: Service.RequestError = {
@ -75,7 +75,7 @@ export function handleResponseError(response: AxiosResponse) {
// 请求成功的状态码非200的错误
const errorCode: ErrorStatus = response.status as ErrorStatus;
const msg = ERROR_STATUS[errorCode] || DEFAULT_REQUEST_ERROR_MSG;
Object.assign(error, { type: 'backend', code: errorCode, msg });
Object.assign(error, { type: 'http', code: errorCode, msg });
}
showErrorMsg(error);

View File

@ -18,15 +18,12 @@ function hasErrorMsg(error: Service.RequestError) {
* @param error
*/
export function showErrorMsg(error: Service.RequestError) {
if (!error.msg) return;
if (!NO_ERROR_MSG_CODE.includes(error.code)) {
if (!hasErrorMsg(error)) {
addErrorMsg(error);
window.console.warn(error.code, error.msg);
window.$message?.error(error.msg, { duration: ERROR_MSG_DURATION });
setTimeout(() => {
removeErrorMsg(error);
}, ERROR_MSG_DURATION);
}
}
if (!error.msg || NO_ERROR_MSG_CODE.includes(error.code) || hasErrorMsg(error)) return;
addErrorMsg(error);
window.console.warn(error.code, error.msg);
window.$message?.error(error.msg, { duration: ERROR_MSG_DURATION });
setTimeout(() => {
removeErrorMsg(error);
}, ERROR_MSG_DURATION);
}

View File

@ -56,6 +56,6 @@ async function transformFile(formData: FormData, key: string, file: File[] | Fil
);
} else {
// 单文件
await formData.append(key, file);
formData.append(key, file);
}
}