新增
This commit is contained in:
+79
-3
@@ -2,6 +2,8 @@
|
||||
* 权限控制
|
||||
*/
|
||||
|
||||
import type { LocationQueryValue, RouteLocationNormalized } from 'vue-router'
|
||||
|
||||
import config from './config'
|
||||
import { PageEnum } from './enums/pageEnum'
|
||||
import router, { findFirstValidRoute } from './router'
|
||||
@@ -11,6 +13,33 @@ import useUserStore from './stores/modules/user'
|
||||
import { clearAuthInfo } from './utils/auth'
|
||||
import { isExternal } from './utils/validate'
|
||||
|
||||
function firstQueryString(q: LocationQueryValue | LocationQueryValue[] | undefined): string {
|
||||
if (q === undefined || q === null) {
|
||||
return ''
|
||||
}
|
||||
if (Array.isArray(q)) {
|
||||
const v = q[0]
|
||||
return v == null ? '' : String(v).trim()
|
||||
}
|
||||
return String(q).trim()
|
||||
}
|
||||
|
||||
/**
|
||||
* 企微扫码绑定 OAuth 回调:须放行当前页并保留 code,否则守卫会跳到 /bind-work-wechat 导致丢参、绑定失败。
|
||||
* Vue Router 的 query 值可能是 string | string[],仅用 typeof === 'string' 会误判,进而 next({ path }) 清掉整段 query。
|
||||
*/
|
||||
function isWorkWechatBindOAuthCallback(to: RouteLocationNormalized): boolean {
|
||||
const code = firstQueryString(to.query.code)
|
||||
if (!code) {
|
||||
return false
|
||||
}
|
||||
const state = firstQueryString(to.query.state)
|
||||
if (state === 'bind_wxwork' || state === 'admin_bind_wx') {
|
||||
return true
|
||||
}
|
||||
return firstQueryString(to.query.bind_wxwork) === '1'
|
||||
}
|
||||
|
||||
// 动态添加路由-使用递归进行调整-(fix: 修复之前超过3级菜单导致使用keep-alive功能无效问题
|
||||
const addRoutesRecursively = (routes: any, parentPath = '') => {
|
||||
try {
|
||||
@@ -50,13 +79,23 @@ const addRoutesRecursively = (routes: any, parentPath = '') => {
|
||||
const loginPath = PageEnum.LOGIN
|
||||
const defaultPath = PageEnum.INDEX
|
||||
const changePasswordPath = '/change-password'
|
||||
const bindWorkWechatPath = '/bind-work-wechat'
|
||||
// 免登录白名单
|
||||
const whiteList: string[] = [PageEnum.LOGIN, PageEnum.ERROR_403]
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
document.title = to.meta.title ?? config.title
|
||||
const userStore = useUserStore()
|
||||
const tabsStore = useTabsStore()
|
||||
|
||||
|
||||
if (to.path === bindWorkWechatPath) {
|
||||
if (!userStore.token) {
|
||||
next({ path: loginPath })
|
||||
return
|
||||
}
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
// 特殊处理:修改密码页面
|
||||
if (to.path === changePasswordPath) {
|
||||
if (!userStore.token) {
|
||||
@@ -82,7 +121,36 @@ router.beforeEach(async (to, from, next) => {
|
||||
next({ path: changePasswordPath })
|
||||
return
|
||||
}
|
||||
|
||||
if (userStore.userInfo?.need_bind_work_wechat) {
|
||||
if (to.path !== bindWorkWechatPath) {
|
||||
if (isWorkWechatBindOAuthCallback(to)) {
|
||||
next()
|
||||
return
|
||||
}
|
||||
next({ path: bindWorkWechatPath })
|
||||
return
|
||||
}
|
||||
}
|
||||
// 须先绑企微时未 addRoute;绑定后须补注册
|
||||
if (
|
||||
!userStore.userInfo?.need_bind_work_wechat &&
|
||||
userStore.routes?.length > 0 &&
|
||||
!router.hasRoute(INDEX_ROUTE_NAME)
|
||||
) {
|
||||
const routeName = findFirstValidRoute(userStore.routes)
|
||||
if (!routeName) {
|
||||
clearAuthInfo()
|
||||
next(PageEnum.ERROR_403)
|
||||
return
|
||||
}
|
||||
tabsStore.setRouteName(routeName!)
|
||||
INDEX_ROUTE.redirect = { name: routeName! }
|
||||
router.addRoute(INDEX_ROUTE)
|
||||
addRoutesRecursively(userStore.routes)
|
||||
next({ ...to, replace: true })
|
||||
return
|
||||
}
|
||||
|
||||
if (to.path === loginPath) {
|
||||
next({ path: defaultPath })
|
||||
} else {
|
||||
@@ -98,7 +166,15 @@ router.beforeEach(async (to, from, next) => {
|
||||
next({ path: changePasswordPath })
|
||||
return
|
||||
}
|
||||
|
||||
if (userStore.userInfo?.need_bind_work_wechat) {
|
||||
if (isWorkWechatBindOAuthCallback(to)) {
|
||||
next({ ...to, replace: true })
|
||||
return
|
||||
}
|
||||
next({ path: bindWorkWechatPath })
|
||||
return
|
||||
}
|
||||
|
||||
const routes = userStore.routes
|
||||
// 找到第一个有效路由
|
||||
const routeName = findFirstValidRoute(routes)
|
||||
|
||||
Reference in New Issue
Block a user