ruoyi-plus-soybean/src/composables/common/router.ts

70 lines
1.8 KiB
TypeScript
Raw Normal View History

import { useRouter, useRoute } from 'vue-router';
import type { RouteLocationRaw } from 'vue-router';
import { router as globalRouter, routePath } from '@/router';
2021-09-11 02:34:36 +08:00
import type { LoginModuleType } from '@/interface';
/**
*
* @param inSetup - vue页面/setup里面调用
*/
export function useRouterPush(inSetup: boolean = true) {
const router = inSetup ? useRouter() : globalRouter;
const route = inSetup ? useRoute() : null;
2021-09-11 02:34:36 +08:00
2021-09-14 01:31:29 +08:00
/** 跳转首页 */
function toHome() {
router.push('/');
}
/**
*
* - current: 取当前的path作为重定向地址
*/
type LoginRedirect = 'current' | string;
2021-09-11 02:34:36 +08:00
/**
* (vue路由)
* @param module -
* @param redirectUrl -
2021-09-11 02:34:36 +08:00
*/
function toLogin(module: LoginModuleType = 'pwd-login', redirectUrl: LoginRedirect = 'current') {
const routeLocation: RouteLocationRaw = {
path: routePath('login'),
query: { module }
};
if (redirectUrl) {
let url = redirectUrl;
if (redirectUrl === 'current') {
url = router.currentRoute.value.fullPath;
}
routeLocation.query!.redirectUrl = url;
}
router.push(routeLocation);
2021-09-11 02:34:36 +08:00
}
/**
*
* @param module -
* @param query -
*/
function toCurrentLogin(module: LoginModuleType) {
if (route) {
const { query } = route;
router.push({ path: routePath('login'), query: { ...query, module } });
} else {
throw Error('该函数必须在setup里面调用');
}
}
/** 登录后跳转重定向的地址 */
function toLoginRedirectUrl(path: string) {
router.push(path);
2021-09-17 19:50:24 +08:00
}
2021-09-11 02:34:36 +08:00
return {
2021-09-14 01:31:29 +08:00
toHome,
toLogin,
2021-09-17 19:50:24 +08:00
toCurrentLogin,
toLoginRedirectUrl
2021-09-11 02:34:36 +08:00
};
}