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

87 lines
2.3 KiB
TypeScript
Raw Normal View History

import { useRouter } from 'vue-router';
2022-01-05 01:35:32 +08:00
import type { RouteLocationRaw } from 'vue-router';
import { router as globalRouter, routeName } from '@/router';
2022-03-05 23:20:00 +08:00
import type { LoginModuleKey } from '@/interface';
2022-01-05 01:35:32 +08:00
/**
*
* @param inSetup - vue页面/setup里面调用axios里面无法使用useRouter和useRoute
*/
export function useRouterPush(inSetup = true) {
2022-01-05 01:35:32 +08:00
const router = inSetup ? useRouter() : globalRouter;
const route = globalRouter.currentRoute;
2022-01-05 01:35:32 +08:00
/**
*
* @param to -
* @param newTab - Tab标签打开
*/
function routerPush(to: RouteLocationRaw, newTab = false) {
if (newTab) {
const routerData = router.resolve(to);
window.open(routerData.href, '_blank');
} else {
router.push(to);
}
}
/** 返回上一级路由 */
function routerBack() {
router.go(-1);
}
/**
*
* @param newTab -
*/
function toHome(newTab = false) {
routerPush({ name: routeName('root') }, newTab);
2022-01-05 01:35:32 +08:00
}
/**
*
* @param loginModule -
* @param redirectUrl - (),undefined表示取当前地址为重定向地址
*/
function toLogin(loginModule?: LoginModuleKey, redirectUrl?: string) {
const module: LoginModuleKey = loginModule || 'pwd-login';
const routeLocation: RouteLocationRaw = {
name: routeName('login'),
params: { module },
2022-01-05 01:35:32 +08:00
};
const redirect = redirectUrl || route.value.fullPath;
2022-01-05 01:35:32 +08:00
Object.assign(routeLocation, { query: { redirect } });
routerPush(routeLocation);
}
/**
*
* @param module -
*/
function toLoginModule(module: LoginModuleKey) {
const { query } = route.value;
routerPush({ name: routeName('login'), params: { module }, query });
2022-01-05 01:35:32 +08:00
}
/**
*
*/
function toLoginRedirect() {
const { query } = route.value;
if (query?.redirect) {
routerPush(query.redirect as string);
2022-01-05 01:35:32 +08:00
} else {
toHome();
}
}
return {
routerPush,
routerBack,
toHome,
toLogin,
toLoginModule,
toLoginRedirect,
2022-01-05 01:35:32 +08:00
};
}