ruoyi-plus-soybean/src/hooks/common/useRouterChange.ts

76 lines
2.0 KiB
TypeScript
Raw Normal View History

import { useRouter, useRoute } from 'vue-router';
import type { RouteLocationRaw } from 'vue-router';
2021-09-17 19:50:24 +08:00
import { EnumRoutePath } from '@/enum';
import { router as globalRouter, RouteNameMap } from '@/router';
2021-09-11 02:34:36 +08:00
import type { LoginModuleType } from '@/interface';
interface LoginRedirect {
/**
*
* - current: 取当前的地址作为重定向地址
* - custom: 自定义地址作为重定向地址
*/
type: 'current' | 'custom' | 'no';
/** 自定义地址 */
url: string;
}
/**
*
* @param inSetup - vue页面/setup里面调用
*/
export default function useRouterChange(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('/');
}
2021-09-11 02:34:36 +08:00
/**
* (vue路由)
* @param module -
* @param addRedirect -
* @param redirect -
2021-09-11 02:34:36 +08:00
*/
function toLogin(
module: LoginModuleType = 'pwd-login',
addRedirect: boolean = false,
redirect: LoginRedirect = { type: 'current', url: '' }
) {
const redirectUrl = redirect.type === 'current' ? window.location.href : redirect.url;
const routeLocation: RouteLocationRaw = {
2021-09-11 02:34:36 +08:00
name: RouteNameMap.get('login'),
params: { module }
};
if (addRedirect) {
routeLocation.query = { redirectUrl };
}
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({ name: RouteNameMap.get('login'), params: { module }, query });
}
}
2021-09-17 19:50:24 +08:00
function toReload(redirectUrl: string) {
router.push({ path: EnumRoutePath.reload, query: { redirectUrl } });
}
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,
toReload
2021-09-11 02:34:36 +08:00
};
}