|
@@ -4,7 +4,9 @@ import NProgress from 'nprogress'
|
|
|
import 'nprogress/nprogress.css'
|
|
|
import { ElMessageBox } from 'element-plus'
|
|
|
import i18n from '@/lang'
|
|
|
-const whiteList = ['/redirect', '/login', '/401', '/404']
|
|
|
+import { UserStore } from '@/store/user'
|
|
|
+
|
|
|
+const whiteList = ['/redirect', '/login', '/401', '/404', '/route/loading']
|
|
|
NProgress.configure({ showSpinner: false }) // 进度条
|
|
|
const dontRedirectList = ['/login', '/', '/401', '/404']
|
|
|
|
|
@@ -18,84 +20,30 @@ export const registerBeforeRouter = async (router) => {
|
|
|
next()
|
|
|
return
|
|
|
}
|
|
|
- if (token) {
|
|
|
- NProgress.inc()
|
|
|
- if (to.path === '/login') next()
|
|
|
- NProgress.inc()
|
|
|
- try {
|
|
|
- const { menus, errcode, errmsg } = await getUserMeta(token)
|
|
|
- // 登录信息有问题
|
|
|
- if (errcode !== 0) {
|
|
|
- if (errcode.includes('FRAMEERROR_401')) {
|
|
|
- await ElMessageBox.alert(errmsg, i18n.global.t('common.user_confirm'), {
|
|
|
- confirmButtonText: i18n.global.t('common.re_login'),
|
|
|
- type: 'error'
|
|
|
- })
|
|
|
- const fp = from.path
|
|
|
- const noneed = dontRedirectList.includes(fp)
|
|
|
- if (noneed) next('/login')
|
|
|
- else next(`/login?redirect=${from.fullPath}`)
|
|
|
- return
|
|
|
- } else {
|
|
|
- await ElMessageBox.alert(errmsg, i18n.global.t('common.user_confirm'), {
|
|
|
- confirmButtonText: i18n.global.t('common.re_login'),
|
|
|
- type: 'error'
|
|
|
- })
|
|
|
- // location.reload()
|
|
|
- const fp = from.path
|
|
|
- const noneed = dontRedirectList.includes(fp)
|
|
|
- if (noneed) next('/login')
|
|
|
- else next(`/login?redirect=${from.fullPath}`)
|
|
|
- }
|
|
|
- }
|
|
|
- // 菜单格式不正确
|
|
|
- if (!menus) {
|
|
|
- next('/401')
|
|
|
- return
|
|
|
- }
|
|
|
- // 检查目的地路由是否注册
|
|
|
- const hasRoute = hasNecessaryRoute(to, router)
|
|
|
- NProgress.inc()
|
|
|
- if (hasRoute || to.meta.hidden) {
|
|
|
- // 注册了直接进入
|
|
|
- if (get(from, 'query.redirect')) {
|
|
|
- const redirect = get(from, 'query.redirect')
|
|
|
- from.query = {}
|
|
|
- next(redirect)
|
|
|
- return
|
|
|
- } else {
|
|
|
- next()
|
|
|
- return
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 没注册就先注册再重定向进入直到进入为止
|
|
|
- await addUserRoutes(menus, router)
|
|
|
- NProgress.inc()
|
|
|
- if (get(from, 'query.redirect')) {
|
|
|
- const redirect = get(from, 'query.redirect')
|
|
|
- from.query = {}
|
|
|
- next(redirect)
|
|
|
- } else {
|
|
|
- await next(to)
|
|
|
- }
|
|
|
-
|
|
|
- // next({ ...to, replace: true })
|
|
|
- }
|
|
|
- } catch (error) {
|
|
|
- await ElMessageBox.alert(i18n.global.t('common.token_error'), i18n.global.t('common.user_confirm'), {
|
|
|
- confirmButtonText: i18n.global.t('common.re_login'),
|
|
|
- type: 'error'
|
|
|
- })
|
|
|
- const fp = from.path
|
|
|
- const noneed = dontRedirectList.includes(fp)
|
|
|
- if (noneed) next('/login')
|
|
|
- else next(`/login?redirect=${from.fullPath}`)
|
|
|
- }
|
|
|
+ if (!token) {
|
|
|
+ // 没有登录信息,返回login,并记录当前路由
|
|
|
+ console.log('no token & not in whiteList')
|
|
|
+ const redirectPath = to.fullPath
|
|
|
+ next({ path: '/login', query: { redirectPath } })
|
|
|
+ return
|
|
|
} else {
|
|
|
- const fp = from.path
|
|
|
- const noneed = dontRedirectList.includes(fp)
|
|
|
- if (noneed) next('/login')
|
|
|
- else next(`/login?redirect=${from.fullPath}`)
|
|
|
+ const userStore = UserStore()
|
|
|
+ const userMenus = toRaw(userStore.menus)
|
|
|
+ if (!userMenus || userMenus.length <= 0) {
|
|
|
+ console.log('has token but no menus')
|
|
|
+ // 没有菜单,说明路由没有注册,需要注册路由,记录当前去哪里再跳转至路由
|
|
|
+ let redirectPath = to.fullPath
|
|
|
+ next({ path: '/route/loading', query: { redirectPath } })
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ // 路由注册了,直接gogogo
|
|
|
+ // 检查有没有redirectPath
|
|
|
+ console.log('continue')
|
|
|
+ let redirectPath = get(to, 'query.redirectPath')
|
|
|
+ if (redirectPath) next(redirectPath)
|
|
|
+ next()
|
|
|
+ return
|
|
|
+ }
|
|
|
}
|
|
|
})
|
|
|
}
|