| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- import router from './router'
- import type { RouteRecordRaw } from 'vue-router'
- import { isRelogin } from '@/config/axios/service'
- import { getAccessToken, removeToken } from '@/utils/auth'
- 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'
- import * as authUtil from '@/utils/auth'
- import { useUserStore } from '@/store/modules/user'
- import { useSettingStore } from '@/store/modules/setting'
- const { start, done } = useNProgress()
- const { loadStart, loadDone } = usePageLoading()
- const userStore = useUserStore()
- const settingStore = useSettingStore()
- const parseURL = (
- url: string | null | undefined
- ): { basePath: string; paramsObject: { [key: string]: string } } => {
- // 如果输入为 null 或 undefined,返回空字符串和空对象
- if (url == null) {
- return { basePath: '', paramsObject: {} }
- }
- // 找到问号 (?) 的位置,它之前是基础路径,之后是查询参数
- const questionMarkIndex = url.indexOf('?')
- let basePath = url
- const paramsObject: { [key: string]: string } = {}
- // 如果找到了问号,说明有查询参数
- if (questionMarkIndex !== -1) {
- // 获取 basePath
- basePath = url.substring(0, questionMarkIndex)
- // 从 URL 中获取查询字符串部分
- const queryString = url.substring(questionMarkIndex + 1)
- // 使用 URLSearchParams 遍历参数
- const searchParams = new URLSearchParams(queryString)
- searchParams.forEach((value, key) => {
- // 封装进 paramsObject 对象
- paramsObject[key] = value
- })
- }
- // 返回 basePath 和 paramsObject
- return { basePath, paramsObject }
- }
- // 路由不重定向白名单
- const whiteList = [
- '/login',
- '/social-login',
- '/auth-redirect',
- '/bind',
- '/register',
- '/oauthLogin/gitee'
- ]
- // 路由加载前
- router.beforeEach(async (to, from, next) => {
- start()
- loadStart()
- if (getAccessToken()) {
- if (to.path === '/login') {
- if(userStore.getIsBusiness){
- next({path: '/home/data_index'})
- }else{
- next({ path: '/' })
- }
- } else {
- // 从h5跳转过来重新设置token和对应权限
- if(to.query && to.query.from && to.query.from === 'h5'){
- // 设置租户
- const data = JSON.parse(to.query.token)
- const tenantId = Number(data.tenantId)
- authUtil.setTenantId(tenantId)
- authUtil.setLoginForm({
- tenantName: data.tenantName,
- username: data.username
- })
- authUtil.setToken(data)
- }
- // 获取综合设置信息
- await settingStore.getParamsConfig()
- // 获取所有字典
- const dictStore = useDictStoreWithOut()
- const userStore = useUserStoreWithOut()
- const permissionStore = usePermissionStoreWithOut()
- if (!dictStore.getIsSetDict) {
- await dictStore.setDictMap()
- }
- if (!userStore.getIsSetUser) {
- isRelogin.show = true
- await userStore.setTenantPackages()
- await userStore.setUserInfoAction(to.query.from)
- isRelogin.show = false
- // 后端过滤菜单
- await permissionStore.generateRoutes()
- permissionStore.getAddRouters.forEach((route) => {
- router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
- })
- const redirectPath = from.query.redirect || to.path
- // 修复跳转时不带参数的问题
- const redirect = decodeURIComponent(redirectPath as string)
- const { basePath, paramsObject: query } = parseURL(redirect)
- const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
- next(nextData)
- } else {
- next()
- }
- }
- } else {
- if (whiteList.indexOf(to.path) !== -1) {
- next()
- } else {
- // 授权页面跳转过来的
- if(to.query && to.query.from && to.query.from === 'h5'){
- // 设置租户
- const data = JSON.parse(to.query.token)
- const tenantId = Number(data.tenantId)
- authUtil.setTenantId(tenantId)
- authUtil.setLoginForm({
- tenantName: data.tenantName,
- username: data.username
- })
- authUtil.setToken(data)
- // 获取综合设置信息
- await settingStore.getParamsConfig()
- // 获取租户套餐
- // 获取所有字典
- const dictStore = useDictStoreWithOut()
- const userStore = useUserStoreWithOut()
- const permissionStore = usePermissionStoreWithOut()
- let flag = false
- if (!dictStore.getIsSetDict) {
- try{
- await dictStore.setDictMap({form: 'h5'})
- }catch(err){
- if(err = '登录超时,请重新登录!'){
- removeToken()
- flag = true
- }
- }
- }
- if(flag){
- next(`/login?redirect=${to.path}?activeName=${to.query.activeName}&processId=${to.query.processId}&status=${to.query.status}&tenantId=${to.query.tenantId}&refresh=true`)// 否则全部重定向到登录页
- }else{
- if (!userStore.getIsSetUser) {
- isRelogin.show = true
- await userStore.setTenantPackages()
- await userStore.setUserInfoAction(to.query.from) // 获取菜单权限
- isRelogin.show = false
- // 后端过滤菜单
- await permissionStore.generateRoutes()
- permissionStore.getAddRouters.forEach((route) => {
- router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
- })
- const redirectPath = from.query.redirect || to.path
- // 修复跳转时不带参数的问题
- const redirect = decodeURIComponent(redirectPath as string)
- const { basePath, paramsObject: query } = parseURL(redirect)
- const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
- next(nextData)
- } else {
- next()
- }
- }
- }else{
- next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
- }
- console.log("to", to.path)
- // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
- }
- }
- })
- router.afterEach((to) => {
- useTitle(to?.meta?.title as string)
- done() // 结束Progress
- loadDone()
- })
|