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

81 lines
2.2 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, ROUTE_HOME } 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
/**
*
* @param to -
* @param newTab -
*/
function routerPush(to: RouteLocationRaw, newTab = false) {
if (newTab) {
const routerData = router.resolve(to);
window.open(routerData.href, '_blank');
return;
}
router.push(to);
}
/**
*
* @param newTab -
*/
function toHome(newTab = false) {
routerPush(ROUTE_HOME.path, newTab);
2021-09-14 01:31:29 +08:00
}
/**
*
* - current: 取当前的path作为重定向地址
*/
type LoginRedirect = 'current' | string;
2021-09-11 02:34:36 +08:00
/**
* (vue路由)
* @param module -
* @param redirect - ()
* @param newTab -
2021-09-11 02:34:36 +08:00
*/
function toLogin(module: LoginModuleType = 'code-login', redirect: LoginRedirect = 'current', newTab = false) {
const routeLocation: RouteLocationRaw = {
name: routeName('login'),
params: { module }
};
if (redirect) {
let url = redirect;
if (redirect === 'current') {
url = route.fullPath;
}
Object.assign(routeLocation, { query: { redirect: url } });
}
routerPush(routeLocation, newTab);
2021-09-11 02:34:36 +08:00
}
/**
* ()
* @param module -
* @param newTab -
*/
function toCurrentLogin(module: LoginModuleType, newTab = false) {
const { query } = route;
routerPush({ name: routeName('login'), params: { module }, query: { ...query } }, newTab);
2021-09-17 19:50:24 +08:00
}
2021-09-11 02:34:36 +08:00
return {
routerPush,
2021-09-14 01:31:29 +08:00
toHome,
toLogin,
toCurrentLogin
2021-09-11 02:34:36 +08:00
};
}