| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- 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,useUserStore } from '@/store/modules/user'
- import { usePermissionStoreWithOut } from '@/store/modules/permission'
- import {SocialUserVO} from "@/api/system/social/user";
- import {Local, Session} from "@/store";
- import {useCache,CACHE_KEY,deleteUserCache} from "@/hooks/web/useCache";
- import { useTagsViewStore } from '@/store/modules/tagsView'
- const { start, done } = useNProgress()
- const { loadStart, loadDone } = usePageLoading()
- const { wsCache } = useCache()
- const userStore = useUserStore()
- const tagsViewStore = useTagsViewStore()
- // 设置用户最后操作时间
- // let lastTimeUserOperate = new Date().getTime();
- // 设置用户长时间不操作则退出系统的时间阈值,单位毫秒
- const IDLE_TIME = 3 * 60 * 1000; // 这里设置为30分钟
- const timeOut = 60 * 60 * 1000; //设置超时时间: 15分
- 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(to.query.logout ){
- window.opener.postMessage(to.query.logout, '*');
- }else{
- next({ path: '/' })
- }
- } else {
- // 获取所有字典
- 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 as unknown as RouteRecordRaw) // 动态添加可访问路由表
- })
- if (to.path === '/sso') {
- next()
- }else{
- 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 {
- next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
- }
- }
- })
- function updateActiveTime() {
- const newTime = new Date().getTime()
- console.log("updateActiveTime#####################"+newTime)
- wsCache.set(CACHE_KEY.LAST_ACTIVE_TIME,newTime + '')
- }
- window.onload = function () {
- window.document.onmousedown = updateActiveTime;
- };
- function clearInterval(checkActiveInterval) {
- wsCache.delete(CACHE_KEY.LAST_ACTIVE_TIME)
- }
- function closeAllWindow(){
- window.opener.postMessage('-1', '*');
- }
- let checkActiveInterval: any = null
- const startActiveCheck = () => {
- if(checkActiveInterval==null){
- const existActiveTime = wsCache.get(CACHE_KEY.LAST_ACTIVE_TIME);
- console.log("startActiveCheck checkActiveInterval**************"+existActiveTime)
- if(existActiveTime){
- console.log("startActiveCheck clean time **************"+existActiveTime)
- wsCache.delete(CACHE_KEY.LAST_ACTIVE_TIME)
- }
- }
- if (checkActiveInterval != null) return
- updateActiveTime();
- checkActiveInterval = setInterval(() => {
- const currentTime = new Date().getTime();
- const lastActiveTime = wsCache.get(CACHE_KEY.LAST_ACTIVE_TIME);
- console.log("get lastActiveTime#####################"+lastActiveTime)
- console.log("lastActiveTime#####################"+(currentTime - Number(lastActiveTime)))
- if (!lastActiveTime){
- return
- }
- if (currentTime - Number(lastActiveTime) > timeOut) {
- //
- userStore.loginOut()
- tagsViewStore.delAllViews()
- deleteUserCache()
- removeToken()
- wsCache.delete(CACHE_KEY.LAST_ACTIVE_TIME)
- checkActiveInterval = null
- router.push('/login?timeout=true')
- }
- }, 30000);
- }
- const endActiveCheck = () => {
- clearInterval(checkActiveInterval)
- checkActiveInterval = null
- }
- router.afterEach((to) => {
- useTitle(to?.meta?.title as string)
- done() // 结束Progress
- loadDone()
- if(to.path=='/login'){
- endActiveCheck()
- }else{
- startActiveCheck()
- }
- })
|