refactor: 进度条由请求时调整为切换路由显示

This commit is contained in:
2024-11-18 20:37:00 +08:00
parent 2b7c09966f
commit f702e03124
2 changed files with 12 additions and 11 deletions

View File

@@ -1,8 +1,12 @@
import { Button, Message, Notification, Space } from '@arco-design/web-vue' import { Button, Message, Notification, Space } from '@arco-design/web-vue'
import NProgress from 'nprogress'
import router from '@/router' import router from '@/router'
import { useRouteStore, useUserStore } from '@/stores' import { useRouteStore, useUserStore } from '@/stores'
import { getToken } from '@/utils/auth' import { getToken } from '@/utils/auth'
import { isHttp } from '@/utils/validate' import { isHttp } from '@/utils/validate'
import 'nprogress/nprogress.css'
NProgress.configure({ showSpinner: false }) // NProgress Configuration
// 版本更新 // 版本更新
let versionTag: string | null = null // 版本标识 let versionTag: string | null = null // 版本标识
@@ -68,12 +72,13 @@ export const resetHasRouteFlag = () => {
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
const userStore = useUserStore() const userStore = useUserStore()
const routeStore = useRouteStore() const routeStore = useRouteStore()
NProgress.start()
// 判断该用户是否登录 // 判断该用户是否登录
if (getToken()) { if (getToken()) {
if (to.path === '/login') { if (to.path === '/login') {
// 如果已经登录,并准备进入 Login 页面,则重定向到主页 // 如果已经登录,并准备进入 Login 页面,则重定向到主页
next() next()
NProgress.done()
} else { } else {
if (!hasRouteFlag) { if (!hasRouteFlag) {
try { try {
@@ -81,6 +86,7 @@ router.beforeEach(async (to, from, next) => {
if (userStore.userInfo.pwdExpired && to.path !== '/pwdExpired') { if (userStore.userInfo.pwdExpired && to.path !== '/pwdExpired') {
Message.warning('密码已过期,请修改密码') Message.warning('密码已过期,请修改密码')
next('/pwdExpired') next('/pwdExpired')
NProgress.done()
} }
const accessRoutes = await routeStore.generateRoutes() const accessRoutes = await routeStore.generateRoutes()
accessRoutes.forEach((route) => { accessRoutes.forEach((route) => {
@@ -92,13 +98,16 @@ router.beforeEach(async (to, from, next) => {
// 确保添加路由已完成 // 确保添加路由已完成
// 设置 replace: true, 因此导航将不会留下历史记录 // 设置 replace: true, 因此导航将不会留下历史记录
next({ ...to, replace: true }) next({ ...to, replace: true })
NProgress.done()
} catch (error: any) { } catch (error: any) {
// 过程中发生任何错误,都直接重置 Token并重定向到登录页面 // 过程中发生任何错误,都直接重置 Token并重定向到登录页面
await userStore.logoutCallBack() await userStore.logoutCallBack()
next(`/login?redirect=${to.path}`) next(`/login?redirect=${to.path}`)
NProgress.done()
} }
} else { } else {
next() next()
NProgress.done()
} }
} }
} else { } else {
@@ -106,9 +115,11 @@ router.beforeEach(async (to, from, next) => {
if (whiteList.includes(to.path)) { if (whiteList.includes(to.path)) {
// 如果在免登录的白名单中,则直接进入 // 如果在免登录的白名单中,则直接进入
next() next()
NProgress.done()
} else { } else {
// 其他没有访问权限的页面将被重定向到登录页面 // 其他没有访问权限的页面将被重定向到登录页面
next('/login') next('/login')
NProgress.done()
} }
} }

View File

@@ -1,17 +1,13 @@
import axios from 'axios' import axios from 'axios'
import qs from 'query-string' import qs from 'query-string'
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios' import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
import NProgress from 'nprogress'
import { useUserStore } from '@/stores' import { useUserStore } from '@/stores'
import { getToken } from '@/utils/auth' import { getToken } from '@/utils/auth'
import modalErrorWrapper from '@/utils/modal-error-wrapper' import modalErrorWrapper from '@/utils/modal-error-wrapper'
import messageErrorWrapper from '@/utils/message-error-wrapper' import messageErrorWrapper from '@/utils/message-error-wrapper'
import notificationErrorWrapper from '@/utils/notification-error-wrapper' import notificationErrorWrapper from '@/utils/notification-error-wrapper'
import 'nprogress/nprogress.css'
import router from '@/router' import router from '@/router'
NProgress.configure({ showSpinner: false }) // NProgress Configuration
interface ICodeMessage { interface ICodeMessage {
[propName: number]: string [propName: number]: string
} }
@@ -41,7 +37,6 @@ const http: AxiosInstance = axios.create({
// 请求拦截器 // 请求拦截器
http.interceptors.request.use( http.interceptors.request.use(
(config: AxiosRequestConfig) => { (config: AxiosRequestConfig) => {
NProgress.start() // 进度条
const token = getToken() const token = getToken()
if (token) { if (token) {
if (!config.headers) { if (!config.headers) {
@@ -62,12 +57,10 @@ http.interceptors.response.use(
const { data } = response const { data } = response
const { success, code, msg } = data const { success, code, msg } = data
if (response.request.responseType === 'blob') { if (response.request.responseType === 'blob') {
NProgress.done()
return response return response
} }
// 成功 // 成功
if (success) { if (success) {
NProgress.done()
return response return response
} }
@@ -80,14 +73,12 @@ http.interceptors.response.use(
escToClose: false, escToClose: false,
okText: '重新登录', okText: '重新登录',
async onOk() { async onOk() {
NProgress.done()
const userStore = useUserStore() const userStore = useUserStore()
await userStore.logoutCallBack() await userStore.logoutCallBack()
await router.replace('/login') await router.replace('/login')
}, },
}) })
} else { } else {
NProgress.done()
// 如果错误信息长度过长,使用 Notification 进行提示 // 如果错误信息长度过长,使用 Notification 进行提示
if (msg.length <= 15) { if (msg.length <= 15) {
messageErrorWrapper({ messageErrorWrapper({
@@ -101,7 +92,6 @@ http.interceptors.response.use(
return Promise.reject(new Error(msg || '服务器端错误')) return Promise.reject(new Error(msg || '服务器端错误'))
}, },
(error) => { (error) => {
NProgress.done()
const response = Object.assign({}, error.response) const response = Object.assign({}, error.response)
response response
&& messageErrorWrapper({ && messageErrorWrapper({