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

67 lines
1.8 KiB
TypeScript
Raw Normal View History

import { unref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import type { RouteLocationRaw } from 'vue-router';
import { router as globalRouter, routeName } 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() : unref(globalRouter.currentRoute);
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 redirect - ()
2021-09-11 02:34:36 +08:00
*/
function toLogin(module: LoginModuleType = 'pwd-login', redirect: LoginRedirect = 'current') {
const routeLocation: RouteLocationRaw = {
name: routeName('login'),
params: { module }
};
if (redirect) {
let url = redirect;
if (redirect === 'current') {
url = router.currentRoute.value.fullPath;
}
Object.assign(routeLocation, { query: { redirect: url } });
}
router.push(routeLocation);
2021-09-11 02:34:36 +08:00
}
/**
* ()
* @param module -
* @param query -
*/
function toCurrentLogin(module: LoginModuleType) {
const { query } = route;
router.push({ name: routeName('login'), params: { module }, query: { ...query } });
}
/** 登录后跳转重定向的地址 */
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
};
}