index.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { App } from 'vue'
  2. import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
  3. import { RedirectRoute } from '@/router/base'
  4. import { PageEnum } from '@/enums/pageEnum'
  5. import { createRouterGuards } from './router-guards'
  6. import routes from './modules/dashboard'
  7. import { AesEncryption } from '@/utils/cipher'
  8. import { isArray, isNull, isUndefined } from 'lodash-es'
  9. import type { LocationQuery, LocationQueryRaw, LocationQueryValue } from 'vue-router'
  10. const aes = new AesEncryption()
  11. const routeModuleList: RouteRecordRaw[] = [...routes]
  12. function sortRoute(a: any, b: any) {
  13. return (a.meta?.sort || 0) - (b.meta?.sort || 0)
  14. }
  15. /**
  16. *
  17. * @description 加密:反序列化字符串参数
  18. */
  19. export function stringifyQuery(obj: LocationQueryRaw): string {
  20. if (!obj) return ''
  21. const result = Object.keys(obj)
  22. .map((key) => {
  23. const value = obj[key]
  24. if (isUndefined(value)) return ''
  25. if (isNull(value)) return key
  26. if (isArray(value)) {
  27. const resArray: string[] = []
  28. value.forEach((item) => {
  29. if (isUndefined(item)) return
  30. if (isNull(item)) {
  31. resArray.push(key)
  32. } else {
  33. resArray.push(key + '=' + item)
  34. }
  35. })
  36. return resArray.join('&')
  37. }
  38. return `${key}=${value}`
  39. })
  40. .filter((x) => x.length > 0)
  41. .join('&')
  42. return result ? `?${aes.encryptByAES(result)}` : ''
  43. }
  44. /**
  45. *
  46. * @description 解密:反序列化字符串参数
  47. */
  48. export function parseQuery(query: string): LocationQuery {
  49. const res: LocationQuery = {}
  50. query = query.trim().replace(/^(\?|#|&)/, '')
  51. if (!query) return res
  52. query = aes.decryptByAES(query)
  53. query.split('&').forEach((param) => {
  54. const parts = param.replace(/\+/g, ' ').split('=')
  55. const key = parts.shift()
  56. const val = parts.length > 0 ? parts.join('=') : null
  57. if (!isUndefined(key)) {
  58. if (isUndefined(res[key])) {
  59. res[key] = val
  60. } else if (isArray(res[key])) {
  61. ;(res[key] as LocationQueryValue[]).push(val)
  62. } else {
  63. res[key] = [res[key] as LocationQueryValue, val]
  64. }
  65. }
  66. })
  67. return res
  68. }
  69. routeModuleList.sort(sortRoute)
  70. export const RootRoute: RouteRecordRaw = {
  71. path: '/',
  72. name: 'Root',
  73. redirect: PageEnum.BASE_HOME,
  74. meta: {
  75. title: 'Root'
  76. }
  77. }
  78. export const LoginRoute: RouteRecordRaw = {
  79. path: '/login',
  80. name: 'Login',
  81. component: () => import('@/views/login/index.vue'),
  82. meta: {
  83. title: '登录'
  84. }
  85. }
  86. //需要验证权限
  87. export const asyncRoutes = []
  88. //普通路由 无需验证权限
  89. export const constantRouter: any[] = [LoginRoute, RootRoute, RedirectRoute, ...routeModuleList]
  90. const router = createRouter({
  91. history: createWebHashHistory(''),
  92. routes: constantRouter,
  93. strict: true,
  94. stringifyQuery,
  95. parseQuery,
  96. scrollBehavior: () => ({ left: 0, top: 0 })
  97. })
  98. /**
  99. *
  100. // stringifyQuery, // 序列化query参数
  101. // parseQuery,
  102. */
  103. export function setupRouter(app: App) {
  104. app.use(router)
  105. // 创建路由守卫
  106. createRouterGuards(router, app)
  107. }
  108. export default router