2021-09-11 02:34:36 +08:00
|
|
|
import { useRouter } from 'vue-router';
|
2021-09-16 13:58:11 +08:00
|
|
|
import { router as globalRouter, RouteNameMap } from '@/router';
|
2021-09-11 02:34:36 +08:00
|
|
|
import type { LoginModuleType } from '@/interface';
|
|
|
|
|
2021-09-16 13:58:11 +08:00
|
|
|
interface LoginRedirect {
|
|
|
|
/**
|
|
|
|
* 重定向类型
|
|
|
|
* - current: 取当前的地址作为重定向地址
|
|
|
|
* - custom: 自定义地址作为重定向地址
|
|
|
|
*/
|
|
|
|
type: 'current' | 'custom';
|
|
|
|
/** 自定义地址 */
|
|
|
|
url: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 路由跳转
|
|
|
|
* @param inSetup - 是否在vue页面/组件的setup里面调用
|
|
|
|
*/
|
|
|
|
export default function useRouterChange(inSetup: boolean = true) {
|
|
|
|
const router = inSetup ? useRouter() : globalRouter;
|
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 - 展示的登录模块
|
2021-09-16 13:58:11 +08:00
|
|
|
* @param redirect - 登录后重定向相关配置
|
2021-09-11 02:34:36 +08:00
|
|
|
*/
|
2021-09-16 13:58:11 +08:00
|
|
|
function toLogin(module: LoginModuleType = 'pwd-login', redirect: LoginRedirect = { type: 'current', url: '' }) {
|
|
|
|
const redirectUrl = redirect.type === 'current' ? window.location.href : redirect.url;
|
2021-09-11 02:34:36 +08:00
|
|
|
router.push({
|
|
|
|
name: RouteNameMap.get('login'),
|
2021-09-16 13:58:11 +08:00
|
|
|
params: { module },
|
|
|
|
query: { redirectUrl }
|
2021-09-11 02:34:36 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-09-14 01:31:29 +08:00
|
|
|
toHome,
|
2021-09-16 13:58:11 +08:00
|
|
|
toLogin
|
2021-09-11 02:34:36 +08:00
|
|
|
};
|
|
|
|
}
|