123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import { useRouter } from 'vue-router';
- import type { RouteLocationRaw } from 'vue-router';
- import type { RouteKey } from '@elegant-router/types';
- import { router as globalRouter } from '@/router';
- /**
- * Router push
- *
- * Jump to the specified route, it can replace function router.push
- *
- * @param inSetup Whether is in vue script setup
- */
- export function useRouterPush(inSetup = true) {
- const router = inSetup ? useRouter() : globalRouter;
- const route = globalRouter.currentRoute;
- const routerPush = router.push;
- const routerBack = router.back;
- async function routerPushByKey(key: RouteKey, options?: App.Global.RouterPushOptions) {
- const { query, params } = options || {};
- const routeLocation: RouteLocationRaw = {
- name: key
- };
- if (Object.keys(query || {}).length) {
- routeLocation.query = query;
- }
- if (Object.keys(params || {}).length) {
- routeLocation.params = params;
- }
- return routerPush(routeLocation);
- }
- function routerPushByKeyWithMetaQuery(key: RouteKey) {
- const allRoutes = router.getRoutes();
- const meta = allRoutes.find(item => item.name === key)?.meta || null;
- const query: Record<string, string> = {};
- meta?.query?.forEach(item => {
- query[item.key] = item.value;
- });
- return routerPushByKey(key, { query });
- }
- async function toHome() {
- return routerPushByKey('root');
- }
- /**
- * Navigate to login page
- *
- * @param loginModule The login module
- * @param redirectUrl The redirect url, if not specified, it will be the current route fullPath
- */
- async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) {
- const module = loginModule || 'pwd-login';
- const options: App.Global.RouterPushOptions = {
- params: {
- module
- }
- };
- const redirect = redirectUrl || route.value.fullPath;
- options.query = {
- redirect
- };
- return routerPushByKey('login', options);
- }
- /**
- * Toggle login module
- *
- * @param module
- */
- async function toggleLoginModule(module: UnionKey.LoginModule) {
- const query = route.value.query as Record<string, string>;
- return routerPushByKey('login', { query, params: { module } });
- }
- /**
- * Redirect from login
- *
- * @param [needRedirect=true] Whether to redirect after login. Default is `true`
- */
- async function redirectFromLogin(needRedirect = true) {
- const redirect = route.value.query?.redirect as string;
- console.log(needRedirect, redirect, '跳转至退出登录之前的页面');
- if (needRedirect && redirect) {
- await routerPush(redirect);
- } else {
- await toHome();
- }
- }
- return {
- routerPush,
- routerBack,
- routerPushByKey,
- routerPushByKeyWithMetaQuery,
- toLogin,
- toggleLoginModule,
- redirectFromLogin
- };
- }
|