index.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { App } from 'vue';
  2. import {
  3. createRouter,
  4. createWebHistory,
  5. LocationQuery,
  6. LocationQueryRaw,
  7. LocationQueryValue,
  8. Router
  9. } from 'vue-router';
  10. import { constantRoutes } from './routes/index';
  11. import { AesEncryption } from '@/utils/cipher';
  12. import { createRouterGuards } from './router-guards';
  13. const aes = new AesEncryption();
  14. /** Used as references for various `Number` constants. */
  15. const MAX_SAFE_INTEGER = 9007199254740991;
  16. function isLength(value: any) {
  17. return (
  18. typeof value === 'number' &&
  19. value > -1 &&
  20. value % 1 == 0 &&
  21. value <= MAX_SAFE_INTEGER
  22. );
  23. }
  24. function isUndefined(value: any) {
  25. return value === undefined;
  26. }
  27. function isArray(value: any) {
  28. return value != null && typeof value !== 'function' && isLength(value.length);
  29. }
  30. function isNull(value: any) {
  31. return value === null;
  32. }
  33. /**
  34. *
  35. * @description 加密:反序列化字符串参数
  36. */
  37. export function stringifyQuery(obj: LocationQueryRaw): string {
  38. if (!obj) return '';
  39. const result = Object.keys(obj)
  40. .map(key => {
  41. const value: any = obj[key];
  42. if (isUndefined(value)) return '';
  43. if (isNull(value)) return key;
  44. if (Array.isArray(value)) {
  45. const resArray: string[] = [];
  46. value.forEach((item: string) => {
  47. if (isUndefined(item)) return;
  48. if (isNull(item)) {
  49. resArray.push(key);
  50. } else {
  51. resArray.push(key + '=' + item);
  52. }
  53. });
  54. return resArray.join('&');
  55. }
  56. return `${key}=${value}`;
  57. })
  58. .filter(x => x.length > 0)
  59. .join('&');
  60. return result ? `?${aes.encryptByAES(result)}` : '';
  61. }
  62. /**
  63. *
  64. * @description 解密:反序列化字符串参数
  65. */
  66. export function parseQuery(query: string): LocationQuery {
  67. const res: LocationQuery = {};
  68. query = query.trim().replace(/^(\?|#|&)/, '');
  69. if (!query) return res;
  70. query = aes.decryptByAES(query);
  71. query.split('&').forEach(param => {
  72. const parts = param.replace(/\+/g, ' ').split('=');
  73. const key: any = parts.shift();
  74. const val = parts.length > 0 ? parts.join('=') : null;
  75. if (!isUndefined(key)) {
  76. if (isUndefined(res[key])) {
  77. res[key] = val;
  78. } else if (isArray(res[key])) {
  79. (res[key] as LocationQueryValue[]).push(val);
  80. } else {
  81. res[key] = [res[key] as LocationQueryValue, val];
  82. }
  83. }
  84. });
  85. return res;
  86. }
  87. const router: Router = createRouter({
  88. history: createWebHistory('/classroom'),
  89. routes: [...constantRoutes],
  90. stringifyQuery,
  91. parseQuery,
  92. scrollBehavior: () => ({ top: 0 })
  93. });
  94. export function setupRouter(app: App) {
  95. app.use(router);
  96. // 创建路由守卫
  97. createRouterGuards(router);
  98. }
  99. export default router;