mirror of
https://github.com/pure-admin/pure-admin-thin.git
synced 2025-04-25 07:57:18 +08:00
feat: axios新增请求的拦截处理
This commit is contained in:
parent
a021e7f9d5
commit
de5550e216
@ -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>;
|
||||||
};
|
};
|
||||||
|
@ -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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -81,18 +81,16 @@ 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) {
|
// 登录成功后 将token存储到sessionStorage中
|
||||||
// 登录成功后 将token存储到sessionStorage中
|
setTokenFromBackend(data);
|
||||||
setTokenFromBackend(res.data);
|
// 获取后端路由
|
||||||
// 获取后端路由
|
initRouter().then(() => {
|
||||||
initRouter().then(() => {
|
router.push(getTopMenu(true).path);
|
||||||
router.push(getTopMenu(true).path);
|
message("登录成功", { type: "success" });
|
||||||
message("登录成功", { type: "success" });
|
});
|
||||||
});
|
if (isRememberMe.value) {
|
||||||
if (isRememberMe.value) {
|
savePassword(ruleForm.password);
|
||||||
savePassword(ruleForm.password);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user