2021-09-16 20:11:45 +08:00
|
|
|
import { useRouter, useRoute } from 'vue-router';
|
2021-09-16 18:43:52 +08:00
|
|
|
import type { RouteLocationRaw } from 'vue-router';
|
2021-09-17 19:50:24 +08:00
|
|
|
import { EnumRoutePath } from '@/enum';
|
2021-10-20 16:38:43 +08:00
|
|
|
import { router as globalRouter } from '@/router';
|
2021-09-11 02:34:36 +08:00
|
|
|
import type { LoginModuleType } from '@/interface';
|
|
|
|
|
2021-10-18 18:01:14 +08:00
|
|
|
/**
|
|
|
|
* 重定向地址
|
|
|
|
* - current: 取当前的path作为重定向地址
|
|
|
|
*/
|
|
|
|
type LoginRedirect = 'current' | EnumRoutePath;
|
2021-09-16 13:58:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 路由跳转
|
|
|
|
* @param inSetup - 是否在vue页面/组件的setup里面调用
|
|
|
|
*/
|
|
|
|
export default function useRouterChange(inSetup: boolean = true) {
|
|
|
|
const router = inSetup ? useRouter() : globalRouter;
|
2021-09-16 20:11:45 +08:00
|
|
|
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 - 展示的登录模块
|
2021-10-18 18:01:14 +08:00
|
|
|
* @param redirectUrl - 重定向地址
|
2021-09-11 02:34:36 +08:00
|
|
|
*/
|
2021-10-18 18:01:14 +08:00
|
|
|
function toLogin(module: LoginModuleType = 'pwd-login', redirectUrl?: LoginRedirect) {
|
2021-09-16 18:43:52 +08:00
|
|
|
const routeLocation: RouteLocationRaw = {
|
2021-10-20 16:38:43 +08:00
|
|
|
path: EnumRoutePath.login,
|
|
|
|
query: { module }
|
2021-09-16 18:43:52 +08:00
|
|
|
};
|
2021-10-18 18:01:14 +08:00
|
|
|
if (redirectUrl) {
|
|
|
|
let url = redirectUrl;
|
|
|
|
if (redirectUrl === 'current') {
|
|
|
|
url = router.currentRoute.value.fullPath as EnumRoutePath;
|
|
|
|
}
|
2021-10-20 16:38:43 +08:00
|
|
|
routeLocation.query!.redirectUrl = url;
|
2021-09-16 18:43:52 +08:00
|
|
|
}
|
|
|
|
router.push(routeLocation);
|
2021-09-11 02:34:36 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 20:11:45 +08:00
|
|
|
/**
|
|
|
|
* 登陆页跳转登陆页
|
|
|
|
* @param module - 展示的登录模块
|
|
|
|
* @param query - 查询参数
|
|
|
|
*/
|
|
|
|
function toCurrentLogin(module: LoginModuleType) {
|
|
|
|
if (route) {
|
|
|
|
const { query } = route;
|
2021-10-20 16:38:43 +08:00
|
|
|
router.push({ path: EnumRoutePath.login, query: { ...query, module } });
|
2021-09-16 20:11:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 18:01:14 +08:00
|
|
|
/** 登录后跳转重定向的地址 */
|
|
|
|
function toLoginRedirectUrl(path: EnumRoutePath) {
|
|
|
|
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,
|
2021-09-16 20:11:45 +08:00
|
|
|
toLogin,
|
2021-09-17 19:50:24 +08:00
|
|
|
toCurrentLogin,
|
2021-10-18 18:01:14 +08:00
|
|
|
toLoginRedirectUrl
|
2021-09-11 02:34:36 +08:00
|
|
|
};
|
|
|
|
}
|