feat: axios新增请求的拦截处理

This commit is contained in:
valarchie 2023-07-06 22:26:37 +08:00
parent a021e7f9d5
commit de5550e216
3 changed files with 24 additions and 14 deletions

View File

@ -36,7 +36,7 @@ export type TokenDTO = {
}; };
export type CurrentLoginUserDTO = { export type CurrentLoginUserDTO = {
userInfo: any; user: any;
roleKey: string; roleKey: string;
permissions: Set<string>; permissions: Set<string>;
}; };

View File

@ -12,6 +12,7 @@ import {
import { stringify } from "qs"; import { stringify } from "qs";
import NProgress from "../progress"; import NProgress from "../progress";
import { getToken, formatToken } from "@/utils/auth"; import { getToken, formatToken } from "@/utils/auth";
import { message } from "../message";
const { VITE_APP_BASE_API } = import.meta.env; const { VITE_APP_BASE_API } = import.meta.env;
// 相关配置请参考www.axios-js.com/zh-cn/docs/#axios-request-config-1 // 相关配置请参考www.axios-js.com/zh-cn/docs/#axios-request-config-1
@ -100,6 +101,9 @@ class PureHttp {
const instance = PureHttp.axiosInstance; const instance = PureHttp.axiosInstance;
instance.interceptors.response.use( instance.interceptors.response.use(
(response: PureHttpResponse) => { (response: PureHttpResponse) => {
if (response.data.code !== 0) {
message(response.data.msg, { type: "error" });
}
const $config = response.config; const $config = response.config;
// 关闭进度条动画 // 关闭进度条动画
NProgress.done(); NProgress.done();
@ -147,7 +151,15 @@ class PureHttp {
resolve(response); resolve(response);
}) })
.catch(error => { .catch(error => {
reject(error); if (error.response.status >= 500) {
message("网络异常", { type: "error" });
}
if (error.response.status >= 400 && error.response.status < 500) {
message("请求接口不存在", { type: "error" });
}
reject(error.response.statusText);
}); });
}); });
} }

View File

@ -81,10 +81,9 @@ const onLogin = async (formEl: FormInstance | undefined) => {
password: rsaEncrypt(ruleForm.password), password: rsaEncrypt(ruleForm.password),
captchaCode: ruleForm.captchaCode, captchaCode: ruleForm.captchaCode,
captchaCodeKey: ruleForm.captchaCodeKey captchaCodeKey: ruleForm.captchaCodeKey
}).then(res => { }).then(({ data }) => {
if (res.code === 0) {
// tokensessionStorage // tokensessionStorage
setTokenFromBackend(res.data); setTokenFromBackend(data);
// //
initRouter().then(() => { initRouter().then(() => {
router.push(getTopMenu(true).path); router.push(getTopMenu(true).path);
@ -93,7 +92,6 @@ const onLogin = async (formEl: FormInstance | undefined) => {
if (isRememberMe.value) { if (isRememberMe.value) {
savePassword(ruleForm.password); savePassword(ruleForm.password);
} }
}
}); });
} else { } else {
loading.value = false; loading.value = false;