Files
ss-crm-manage-web/src/permission.js

105 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-04-28 16:20:45 +08:00
import router from './router'
import { isRelogin } from '@/config/axios/service'
2024-08-01 18:41:37 +08:00
import { getAccessToken, removeToken } from '@/utils/auth'
2024-04-28 16:20:45 +08:00
import { useTitle } from '@/hooks/web/useTitle'
import { useNProgress } from '@/hooks/web/useNProgress'
import { usePageLoading } from '@/hooks/web/usePageLoading'
import { useDictStoreWithOut } from '@/store/modules/dict'
import { useUserStoreWithOut } from '@/store/modules/user'
import { usePermissionStoreWithOut } from '@/store/modules/permission'
2025-05-19 11:41:27 +08:00
import { useAppStoreWithOut } from '@/store/modules/app'
import { getTenantId, getAppId, setTenantId, setAppId } from '@/utils/auth'
2024-08-01 18:41:37 +08:00
import cache from '@/plugins/cache'
2024-04-28 16:20:45 +08:00
const { start, done } = useNProgress()
const { loadStart, loadDone } = usePageLoading()
// 路由不重定向白名单
2024-08-28 10:09:23 +08:00
const whiteList = [
'/login',
'/mp-login',
2024-09-05 17:35:19 +08:00
'/nm-detail',
2024-08-28 10:09:23 +08:00
'/social-login',
'/auth-redirect',
'/bind',
'/register',
'/swagger'
]
2024-04-28 16:20:45 +08:00
// 路由加载前
router.beforeEach(async (to, from, next) => {
start()
loadStart()
2024-08-01 18:41:37 +08:00
if (getAppId() && to.query?.appId && getAppId() != to.query?.appId) {
removeToken()
cache?.local?.delete('appInfo')
cache?.local?.delete('roleRouters')
cache?.local?.delete('user')
cache?.local?.delete('App_ID')
next(`/login?tenantId=${to.query?.tenantId}&appId=${to.query?.appId}`)
} else {
if (getAccessToken()) {
if (to.path === '/login') {
2025-05-19 11:41:27 +08:00
if (to.query?.tenantId && to.query?.appId) {
setApp(to.query.tenantId, to.query.appId)
setTimeout(() => {
next({ path: '/' })
}, 1500)
} else {
next({ path: '/' })
}
2024-04-28 16:20:45 +08:00
} else {
2024-08-01 18:41:37 +08:00
// 获取所有字典
const dictStore = useDictStoreWithOut()
const userStore = useUserStoreWithOut()
const permissionStore = usePermissionStoreWithOut()
2025-05-19 11:41:27 +08:00
if (!dictStore.getIsSetDict) {
2024-08-01 18:41:37 +08:00
await dictStore.setDictMap()
}
if (!userStore.getIsSetUser) {
isRelogin.show = true
await userStore.setUserInfoAction()
isRelogin.show = false
// 后端过滤菜单
await permissionStore.generateRoutes()
permissionStore.getAddRouters.forEach((route) => {
router.addRoute(route) // 动态添加可访问路由表
})
const redirectPath = from.query.redirect || to.path
const redirect = decodeURIComponent(redirectPath)
const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }
next(nextData)
} else {
next()
}
2024-04-28 16:20:45 +08:00
}
} else {
2024-08-01 18:41:37 +08:00
if (whiteList.indexOf(to.path) !== -1) {
next()
2024-05-23 18:07:49 +08:00
} else {
2024-08-01 18:41:37 +08:00
const tenantId = getTenantId()
const appId = getAppId()
if (tenantId && appId) {
2025-05-19 11:41:27 +08:00
next(`/oa/login?tenantId=${tenantId}&appId=${appId}&redirect=${to.fullPath}`) // 否则全部重定向到登录页
2024-08-01 18:41:37 +08:00
} else {
2025-05-19 11:41:27 +08:00
next(`/oa/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
2024-08-01 18:41:37 +08:00
}
2024-05-23 18:07:49 +08:00
}
2024-04-28 16:20:45 +08:00
}
}
})
2025-05-19 11:41:27 +08:00
function setApp(tenantId, appId) {
setTenantId(tenantId)
setAppId(appId)
const appStore = useAppStoreWithOut()
appStore.setAppInfo(appId)
}
2024-04-28 16:20:45 +08:00
router.afterEach((to) => {
useTitle(to?.meta?.title)
done() // 结束Progress
loadDone()
})