From de5550e216af103a5f8b8dadca11850a894a8fae Mon Sep 17 00:00:00 2001 From: valarchie <343928303@qq.com> Date: Thu, 6 Jul 2023 22:26:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20axios=E6=96=B0=E5=A2=9E=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=9A=84=E6=8B=A6=E6=88=AA=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/common.ts | 2 +- src/utils/http/index.ts | 14 +++++++++++++- src/views/login/index.vue | 22 ++++++++++------------ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/api/common.ts b/src/api/common.ts index 26ee593..b7a7172 100644 --- a/src/api/common.ts +++ b/src/api/common.ts @@ -36,7 +36,7 @@ export type TokenDTO = { }; export type CurrentLoginUserDTO = { - userInfo: any; + user: any; roleKey: string; permissions: Set; }; diff --git a/src/utils/http/index.ts b/src/utils/http/index.ts index c07c52c..068d793 100644 --- a/src/utils/http/index.ts +++ b/src/utils/http/index.ts @@ -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); }); }); } diff --git a/src/views/login/index.vue b/src/views/login/index.vue index e5986c1..50c5856 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -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 {