添加请求内容~
This commit is contained in:
parent
dc5ff563ee
commit
4a1cf186dc
7
.env
7
.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 = 后台
|
||||
|
@ -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 范围内的状态码都会触发该函数。
|
||||
// 对响应数据进行格式化
|
@ -9,7 +9,6 @@
|
||||
* ```
|
||||
*/
|
||||
export const IN_DEV = import.meta.env.DEV
|
||||
|
||||
/**
|
||||
* 是否在生产环境
|
||||
* @example
|
||||
|
59
src/composables/frontRequest.ts
Normal file
59
src/composables/frontRequest.ts
Normal file
@ -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)
|
||||
},
|
||||
)
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user