router.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { useRouter } from 'vue-router';
  2. import type { RouteLocationRaw } from 'vue-router';
  3. import type { RouteKey } from '@elegant-router/types';
  4. import { router as globalRouter } from '@/router';
  5. /**
  6. * Router push
  7. *
  8. * Jump to the specified route, it can replace function router.push
  9. *
  10. * @param inSetup Whether is in vue script setup
  11. */
  12. export function useRouterPush(inSetup = true) {
  13. const router = inSetup ? useRouter() : globalRouter;
  14. const route = globalRouter.currentRoute;
  15. const routerPush = router.push;
  16. const routerBack = router.back;
  17. async function routerPushByKey(key: RouteKey, options?: App.Global.RouterPushOptions) {
  18. const { query, params } = options || {};
  19. const routeLocation: RouteLocationRaw = {
  20. name: key
  21. };
  22. if (Object.keys(query || {}).length) {
  23. routeLocation.query = query;
  24. }
  25. if (Object.keys(params || {}).length) {
  26. routeLocation.params = params;
  27. }
  28. return routerPush(routeLocation);
  29. }
  30. function routerPushByKeyWithMetaQuery(key: RouteKey) {
  31. const allRoutes = router.getRoutes();
  32. const meta = allRoutes.find(item => item.name === key)?.meta || null;
  33. const query: Record<string, string> = {};
  34. meta?.query?.forEach(item => {
  35. query[item.key] = item.value;
  36. });
  37. return routerPushByKey(key, { query });
  38. }
  39. async function toHome() {
  40. return routerPushByKey('root');
  41. }
  42. /**
  43. * Navigate to login page
  44. *
  45. * @param loginModule The login module
  46. * @param redirectUrl The redirect url, if not specified, it will be the current route fullPath
  47. */
  48. async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) {
  49. const module = loginModule || 'pwd-login';
  50. const options: App.Global.RouterPushOptions = {
  51. params: {
  52. module
  53. }
  54. };
  55. const redirect = redirectUrl || route.value.fullPath;
  56. options.query = {
  57. redirect
  58. };
  59. return routerPushByKey('login', options);
  60. }
  61. /**
  62. * Toggle login module
  63. *
  64. * @param module
  65. */
  66. async function toggleLoginModule(module: UnionKey.LoginModule) {
  67. const query = route.value.query as Record<string, string>;
  68. return routerPushByKey('login', { query, params: { module } });
  69. }
  70. /**
  71. * Redirect from login
  72. *
  73. * @param [needRedirect=true] Whether to redirect after login. Default is `true`
  74. */
  75. async function redirectFromLogin(needRedirect = true) {
  76. const redirect = route.value.query?.redirect as string;
  77. console.log(needRedirect, redirect, '跳转至退出登录之前的页面');
  78. if (needRedirect && redirect) {
  79. await routerPush(redirect);
  80. } else {
  81. await toHome();
  82. }
  83. }
  84. return {
  85. routerPush,
  86. routerBack,
  87. routerPushByKey,
  88. routerPushByKeyWithMetaQuery,
  89. toLogin,
  90. toggleLoginModule,
  91. redirectFromLogin
  92. };
  93. }