This commit is contained in:
李志鹏
2025-10-23 15:11:35 +08:00
8 changed files with 84 additions and 35 deletions

View File

@@ -1,11 +1,14 @@
import axios from 'axios'
import { showToast, showLoading, showConfirmDialog, closeToast } from 'vant'
import { showToast, showNotify, showConfirmDialog, closeToast } from 'vant'
import { useUserInfoStore } from '@/stores/modules/userInfo'
const store = useUserInfoStore()
import { getLocal } from '@/utils/local'
import router from '@/router/index'
import { useOverallStore } from '@/stores'
// 创建axios实例
console.log(import.meta.env.VITE_APP_URL)
console.log(import.meta.env.VITE_APP_URL,123)
const service = axios.create({
baseURL: import.meta.env.VITE_APP_URL, // api的base_url
// baseURL: import.meta.env.VITE_APP_URL, // api的base_url
@@ -57,7 +60,6 @@ service.interceptors.response.use(
if (response.config.loading) {
closeLoading()
}
const res = response.data
// 处理异常的情况
if (res.errCode != 0) {
@@ -66,19 +68,7 @@ service.interceptors.response.use(
type: 'fail',
duration: 5000
})
// 403:非法的token; 50012:其他客户端登录了; 401:Token 过期了;
if (res.errCode === 403 || res.errCode === 50012 || res.errCode === 401) {
showConfirmDialog({
title: '确定登出',
message: '你已被登出,可以取消继续留在该页面,或者重新登录',
confirmButtonText: '重新登录',
cancelButtonText: '取消'
}).then(() => {
store.loginOut().then(() => {
location.reload() // 为了重新实例化vue-router对象 避免bug
})
})
}
return Promise.reject(new Error('error'))
} else {
// 默认只返回data不返回状态码和message
@@ -92,6 +82,20 @@ service.interceptors.response.use(
}
},
(error) => {
if(error?.response?.status === 401){//如果是记录浏览器页面就不跳转login
// showConfirmDialog({
// title: '确定登出',
// message: '你已被登出,可以取消继续留在该页面,或者重新登录',
// confirmButtonText: '重新登录',
// cancelButtonText: '取消'
// }).then(() => {
// store.loginOut().then(() => {
// location.reload() // 为了重新实例化vue-router对象 避免bug
// })
// })
router.replace('/login')
return Promise.reject(error)
}
error.config && removePending(error.config)
// 关闭loading
if (error.config?.loading) {
@@ -149,23 +153,18 @@ function removePending(config: any) {
}
}
// ----------------------------------loading的函数-------------------------------
const LoadingInstance: { _target: any; _count: number } = {
_target: null, // 保存Loading实例
const LoadingInstance: { _count: number } = {
_count: 0
}
function openLoading(loadingDom: any) {
LoadingInstance._target = showLoading({
message: '数据正在加载中',
forbidClick: true,
background: 'rgba(25, 32, 53, 1)'
})
useOverallStore().setLoading(true)
}
function closeLoading() {
if (LoadingInstance._count > 0) LoadingInstance._count--
if (LoadingInstance._count === 0) {
LoadingInstance._target?.close()
LoadingInstance._target = null
useOverallStore().setLoading(false)
}
}
export default service