mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
feat(projects): 1.0 beta
This commit is contained in:
101
src/hooks/common/router.ts
Normal file
101
src/hooks/common/router.ts
Normal file
@ -0,0 +1,101 @@
|
||||
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
|
||||
* @description 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;
|
||||
|
||||
interface RouterPushOptions {
|
||||
query?: Record<string, string>;
|
||||
params?: Record<string, string>;
|
||||
}
|
||||
|
||||
async function routerPushByKey(key: RouteKey, options?: RouterPushOptions) {
|
||||
const { query, params } = options || {};
|
||||
|
||||
const routeLocation: RouteLocationRaw = {
|
||||
name: key
|
||||
};
|
||||
|
||||
if (query) {
|
||||
routeLocation.query = query;
|
||||
}
|
||||
|
||||
if (params) {
|
||||
routeLocation.params = params;
|
||||
}
|
||||
|
||||
return routerPush(routeLocation);
|
||||
}
|
||||
|
||||
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: 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
|
||||
*/
|
||||
async function redirectFromLogin() {
|
||||
const redirect = route.value.query?.redirect as string;
|
||||
|
||||
if (redirect) {
|
||||
routerPush(redirect);
|
||||
} else {
|
||||
toHome();
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
route,
|
||||
routerPush,
|
||||
routerBack,
|
||||
routerPushByKey,
|
||||
toLogin,
|
||||
toggleLoginModule,
|
||||
redirectFromLogin
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user