莳松-行政管理系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ss-oa-manage-web/src/permission.js

92 lines
2.8 KiB

11 months ago
import router from './router'
import { isRelogin } from '@/config/axios/service'
4 weeks ago
import { getAccessToken } from '@/utils/auth'
11 months ago
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'
1 month ago
import { useAppStoreWithOut } from '@/store/modules/app'
import { getTenantId, getAppId, setTenantId, setAppId } from '@/utils/auth'
11 months ago
const { start, done } = useNProgress()
const { loadStart, loadDone } = usePageLoading()
// 路由不重定向白名单
const whiteList = ['/login', '/social-login', '/auth-redirect', '/bind', '/register', '/swagger']
// 路由加载前
router.beforeEach(async (to, from, next) => {
start()
loadStart()
4 weeks ago
if (getAccessToken()) {
if (to.path === '/login') {
if (to.query?.tenantId && to.query?.appId) {
setApp(to.query.tenantId, to.query.appId)
await waitTime(1500)
11 months ago
} else {
4 weeks ago
next({ path: '/' })
11 months ago
}
} else {
4 weeks ago
// 获取所有字典
const dictStore = useDictStoreWithOut()
const userStore = useUserStoreWithOut()
const permissionStore = usePermissionStoreWithOut()
if (!dictStore.getIsSetDict) {
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 {
11 months ago
next()
4 weeks ago
}
}
} else {
if (whiteList.indexOf(to.path) !== -1) {
next()
} else {
const tenantId = getTenantId()
const appId = getAppId()
if (tenantId && appId) {
next(`/oa/login?tenantId=${tenantId}&appId=${appId}&redirect=${to.fullPath}`) // 否则全部重定向到登录页
11 months ago
} else {
4 weeks ago
next(`/oa/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
11 months ago
}
}
}
})
1 month ago
async function waitTime(seconds) {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, seconds)
})
}
1 month ago
function setApp(tenantId, appId) {
setTenantId(tenantId)
setAppId(appId)
const appStore = useAppStoreWithOut()
appStore.setAppInfo(appId)
}
11 months ago
router.afterEach((to) => {
useTitle(to?.meta?.title)
done() // 结束Progress
loadDone()
})