permission.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import router from './router'
  2. import type { RouteRecordRaw } from 'vue-router'
  3. import { isRelogin } from '@/config/axios/service'
  4. import { getAccessToken,removeToken } from '@/utils/auth'
  5. import { useTitle } from '@/hooks/web/useTitle'
  6. import { useNProgress } from '@/hooks/web/useNProgress'
  7. import { usePageLoading } from '@/hooks/web/usePageLoading'
  8. import { useDictStoreWithOut } from '@/store/modules/dict'
  9. import { useUserStoreWithOut,useUserStore } from '@/store/modules/user'
  10. import { usePermissionStoreWithOut } from '@/store/modules/permission'
  11. import {SocialUserVO} from "@/api/system/social/user";
  12. import {Local, Session} from "@/store";
  13. import {useCache,CACHE_KEY,deleteUserCache} from "@/hooks/web/useCache";
  14. import { useTagsViewStore } from '@/store/modules/tagsView'
  15. const { start, done } = useNProgress()
  16. const { loadStart, loadDone } = usePageLoading()
  17. const { wsCache } = useCache()
  18. const userStore = useUserStore()
  19. const tagsViewStore = useTagsViewStore()
  20. // 设置用户最后操作时间
  21. // let lastTimeUserOperate = new Date().getTime();
  22. // 设置用户长时间不操作则退出系统的时间阈值,单位毫秒
  23. const IDLE_TIME = 3 * 60 * 1000; // 这里设置为30分钟
  24. const timeOut = 60 * 60 * 1000; //设置超时时间: 15分
  25. const parseURL = (
  26. url: string | null | undefined
  27. ): { basePath: string; paramsObject: { [key: string]: string } } => {
  28. // 如果输入为 null 或 undefined,返回空字符串和空对象
  29. if (url == null) {
  30. return { basePath: '', paramsObject: {} }
  31. }
  32. // 找到问号 (?) 的位置,它之前是基础路径,之后是查询参数
  33. const questionMarkIndex = url.indexOf('?')
  34. let basePath = url
  35. const paramsObject: { [key: string]: string } = {}
  36. // 如果找到了问号,说明有查询参数
  37. if (questionMarkIndex !== -1) {
  38. // 获取 basePath
  39. basePath = url.substring(0, questionMarkIndex)
  40. // 从 URL 中获取查询字符串部分
  41. const queryString = url.substring(questionMarkIndex + 1)
  42. // 使用 URLSearchParams 遍历参数
  43. const searchParams = new URLSearchParams(queryString)
  44. searchParams.forEach((value, key) => {
  45. // 封装进 paramsObject 对象
  46. paramsObject[key] = value
  47. })
  48. }
  49. // 返回 basePath 和 paramsObject
  50. return { basePath, paramsObject }
  51. }
  52. // 路由不重定向白名单
  53. const whiteList = [
  54. '/login',
  55. '/social-login',
  56. '/auth-redirect',
  57. '/bind',
  58. '/register',
  59. '/oauthLogin/gitee'
  60. ]
  61. // 路由加载前
  62. router.beforeEach(async (to, from, next) => {
  63. start()
  64. loadStart()
  65. if (getAccessToken()) {
  66. if (to.path === '/login') {
  67. if(to.query.logout ){
  68. window.opener.postMessage(to.query.logout, '*');
  69. }else{
  70. next({ path: '/' })
  71. }
  72. } else {
  73. // 获取所有字典
  74. const dictStore = useDictStoreWithOut()
  75. const userStore = useUserStoreWithOut()
  76. const permissionStore = usePermissionStoreWithOut()
  77. if (!dictStore.getIsSetDict) {
  78. await dictStore.setDictMap()
  79. }
  80. if (!userStore.getIsSetUser) {
  81. isRelogin.show = true
  82. await userStore.setUserInfoAction()
  83. isRelogin.show = false
  84. // 后端过滤菜单
  85. await permissionStore.generateRoutes()
  86. permissionStore.getAddRouters.forEach((route) => {
  87. router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  88. })
  89. if (to.path === '/sso') {
  90. next()
  91. }else{
  92. const redirectPath = from.query.redirect || to.path
  93. // 修复跳转时不带参数的问题
  94. const redirect = decodeURIComponent(redirectPath as string)
  95. const { basePath, paramsObject: query } = parseURL(redirect)
  96. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
  97. next(nextData)
  98. }
  99. } else {
  100. next()
  101. }
  102. // }
  103. }
  104. } else {
  105. if (whiteList.indexOf(to.path) !== -1) {
  106. next()
  107. } else {
  108. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  109. }
  110. }
  111. })
  112. function updateActiveTime() {
  113. const newTime = new Date().getTime()
  114. console.log("updateActiveTime#####################"+newTime)
  115. wsCache.set(CACHE_KEY.LAST_ACTIVE_TIME,newTime + '')
  116. }
  117. window.onload = function () {
  118. window.document.onmousedown = updateActiveTime;
  119. };
  120. function clearInterval(checkActiveInterval) {
  121. wsCache.delete(CACHE_KEY.LAST_ACTIVE_TIME)
  122. }
  123. function closeAllWindow(){
  124. window.opener.postMessage('-1', '*');
  125. }
  126. let checkActiveInterval: any = null
  127. const startActiveCheck = () => {
  128. if(checkActiveInterval==null){
  129. const existActiveTime = wsCache.get(CACHE_KEY.LAST_ACTIVE_TIME);
  130. console.log("startActiveCheck checkActiveInterval**************"+existActiveTime)
  131. if(existActiveTime){
  132. console.log("startActiveCheck clean time **************"+existActiveTime)
  133. wsCache.delete(CACHE_KEY.LAST_ACTIVE_TIME)
  134. }
  135. }
  136. if (checkActiveInterval != null) return
  137. updateActiveTime();
  138. checkActiveInterval = setInterval(() => {
  139. const currentTime = new Date().getTime();
  140. const lastActiveTime = wsCache.get(CACHE_KEY.LAST_ACTIVE_TIME);
  141. console.log("get lastActiveTime#####################"+lastActiveTime)
  142. console.log("lastActiveTime#####################"+(currentTime - Number(lastActiveTime)))
  143. if (!lastActiveTime){
  144. return
  145. }
  146. if (currentTime - Number(lastActiveTime) > timeOut) {
  147. //
  148. userStore.loginOut()
  149. tagsViewStore.delAllViews()
  150. deleteUserCache()
  151. removeToken()
  152. wsCache.delete(CACHE_KEY.LAST_ACTIVE_TIME)
  153. checkActiveInterval = null
  154. router.push('/login?timeout=true')
  155. }
  156. }, 30000);
  157. }
  158. const endActiveCheck = () => {
  159. clearInterval(checkActiveInterval)
  160. checkActiveInterval = null
  161. }
  162. router.afterEach((to) => {
  163. useTitle(to?.meta?.title as string)
  164. done() // 结束Progress
  165. loadDone()
  166. if(to.path=='/login'){
  167. endActiveCheck()
  168. }else{
  169. startActiveCheck()
  170. }
  171. })