From 4a1cf186dc0c6bd42735b520ce43a49e4c7cc49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=B5=E5=AE=AA=E7=91=9E?= <9198107+min-xianrui@user.noreply.gitee.com> Date: Tue, 7 Jan 2025 22:24:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=B7=E6=B1=82=E5=86=85?= =?UTF-8?q?=E5=AE=B9~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 7 ++- src/composables/{http.ts => adminRequest.ts} | 9 ++- src/composables/env.ts | 1 - src/composables/frontRequest.ts | 59 ++++++++++++++++++++ src/plugins/router.ts | 4 -- 5 files changed, 68 insertions(+), 12 deletions(-) rename src/composables/{http.ts => adminRequest.ts} (86%) create mode 100644 src/composables/frontRequest.ts diff --git a/.env b/.env index 6a39d83..1d4542e 100644 --- a/.env +++ b/.env @@ -1,7 +1,10 @@ # 通用环境变量 -# api baseURL -VITE_API_BASE_URL = /api +# 前端接口 +VITE_API_FRONT_BASE_URL = http://localhost:8081 + +#后端接口 +VITE_ADMIN_API_BASE_URL = http://localhost:18080 # 标题 VITE_APP_TITLE = 后台 diff --git a/src/composables/http.ts b/src/composables/adminRequest.ts similarity index 86% rename from src/composables/http.ts rename to src/composables/adminRequest.ts index 483624b..0413e19 100644 --- a/src/composables/http.ts +++ b/src/composables/adminRequest.ts @@ -1,11 +1,11 @@ import axios from 'axios' -export const http = axios.create({ - baseURL: import.meta.env.VITE_API_BASE_URL, +export const frontRequest = axios.create({ + baseURL: import.meta.env.VITE_API_FRONT_BASE_URL, }) // 添加请求拦截器 -http.interceptors.request.use( +frontRequest.interceptors.request.use( function (config) { // 在发送请求之前做些什么 return config @@ -16,9 +16,8 @@ http.interceptors.request.use( return Promise.reject(error) }, ) - // 添加响应拦截器 -http.interceptors.response.use( +frontRequest.interceptors.response.use( function (response) { // 2xx 范围内的状态码都会触发该函数。 // 对响应数据进行格式化 diff --git a/src/composables/env.ts b/src/composables/env.ts index 96a242e..68a98a1 100644 --- a/src/composables/env.ts +++ b/src/composables/env.ts @@ -9,7 +9,6 @@ * ``` */ export const IN_DEV = import.meta.env.DEV - /** * 是否在生产环境 * @example diff --git a/src/composables/frontRequest.ts b/src/composables/frontRequest.ts new file mode 100644 index 0000000..ef7b5d7 --- /dev/null +++ b/src/composables/frontRequest.ts @@ -0,0 +1,59 @@ +import axios from 'axios' + +export const frontRequest = axios.create({ + baseURL: import.meta.env.VITE_API_ADMIN_BASE_URL, +}) + +// 添加请求拦截器 +frontRequest.interceptors.request.use( + function (config) { + // 在发送请求之前做些什么 + return config + }, + function (error) { + toast.warning(error.message ?? '未知请求错误') + // 对请求错误做些什么 + return Promise.reject(error) + }, +) +// 添加响应拦截器 +frontRequest.interceptors.response.use( + function (response) { + // 2xx 范围内的状态码都会触发该函数。 + // 对响应数据进行格式化 + if (response.data) { + return response.data + } + return response + }, + function (error) { + const status = error.response?.status + let { msg, message } = error.response?.data ?? {} + + if (!msg && message) { + msg = message + } + + if (!msg) { + switch (status) { + case 400: + msg = '参数错误' + break + case 500: + msg = '服务端错误' + break + case 404: + msg = '路由未找到' + break + default: + msg = error.message ?? '未知响应错误' + break + } + } + + toast.warning(msg) + // 超出 2xx 范围的状态码都会触发该函数。 + // 对响应错误做点什么 + return Promise.reject(error) + }, +) diff --git a/src/plugins/router.ts b/src/plugins/router.ts index fa98db3..33e5f0d 100644 --- a/src/plugins/router.ts +++ b/src/plugins/router.ts @@ -1,19 +1,16 @@ import { setupLayouts } from 'virtual:meta-layouts' import { createRouter, createWebHistory } from 'vue-router' import { routes as fileRoutes } from 'vue-router/auto-routes' - declare module 'vue-router' {} // 重定向 BASE_URL fileRoutes.flat(Infinity).forEach((route) => { route.path = safeResolve(route.path) }) - export const router = createRouter({ history: createWebHistory(), routes: setupLayouts(fileRoutes), }) - // 路由拦截 router.beforeEach((to, from, next) => { // 在这里编写拦截逻辑 @@ -21,7 +18,6 @@ router.beforeEach((to, from, next) => { const isLogin = userStore().isLogin const authList=["/"] if (authList.includes(to.fullPath) && !isLogin) { - toast.warning("未登录或者登录过期~") // 如果用户未认证,则跳转到登录页面 next('/login'); } else {