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 = {
|
||||
userInfo: any;
|
||||
user: any;
|
||||
roleKey: string;
|
||||
permissions: Set<string>;
|
||||
};
|
||||
|
@ -12,6 +12,7 @@ import {
|
||||
import { stringify } from "qs";
|
||||
import NProgress from "../progress";
|
||||
import { getToken, formatToken } from "@/utils/auth";
|
||||
import { message } from "../message";
|
||||
|
||||
const { VITE_APP_BASE_API } = import.meta.env;
|
||||
// 相关配置请参考:www.axios-js.com/zh-cn/docs/#axios-request-config-1
|
||||
@ -100,6 +101,9 @@ class PureHttp {
|
||||
const instance = PureHttp.axiosInstance;
|
||||
instance.interceptors.response.use(
|
||||
(response: PureHttpResponse) => {
|
||||
if (response.data.code !== 0) {
|
||||
message(response.data.msg, { type: "error" });
|
||||
}
|
||||
const $config = response.config;
|
||||
// 关闭进度条动画
|
||||
NProgress.done();
|
||||
@ -147,7 +151,15 @@ class PureHttp {
|
||||
resolve(response);
|
||||
})
|
||||
.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),
|
||||
captchaCode: ruleForm.captchaCode,
|
||||
captchaCodeKey: ruleForm.captchaCodeKey
|
||||
}).then(res => {
|
||||
if (res.code === 0) {
|
||||
// 登录成功后 将token存储到sessionStorage中
|
||||
setTokenFromBackend(res.data);
|
||||
// 获取后端路由
|
||||
initRouter().then(() => {
|
||||
router.push(getTopMenu(true).path);
|
||||
message("登录成功", { type: "success" });
|
||||
});
|
||||
if (isRememberMe.value) {
|
||||
savePassword(ruleForm.password);
|
||||
}
|
||||
}).then(({ data }) => {
|
||||
// 登录成功后 将token存储到sessionStorage中
|
||||
setTokenFromBackend(data);
|
||||
// 获取后端路由
|
||||
initRouter().then(() => {
|
||||
router.push(getTopMenu(true).path);
|
||||
message("登录成功", { type: "success" });
|
||||
});
|
||||
if (isRememberMe.value) {
|
||||
savePassword(ruleForm.password);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user