2021-11-29 20:34:56 +08:00
|
|
|
import { unref } from 'vue';
|
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-11-29 20:34:56 +08:00
|
|
|
import { router as globalRouter, routeName } from '@/router';
|
2021-09-11 02:34:36 +08:00
|
|
|
import type { LoginModuleType } from '@/interface';
|
|
|
|
|
2021-09-16 13:58:11 +08:00
|
|
|
/**
|
|
|
|
* 路由跳转
|
|
|
|
* @param inSetup - 是否在vue页面/组件的setup里面调用
|
|
|
|
*/
|
2021-11-19 01:33:36 +08:00
|
|
|
export function useRouterPush(inSetup: boolean = true) {
|
2021-09-16 13:58:11 +08:00
|
|
|
const router = inSetup ? useRouter() : globalRouter;
|
2021-11-29 20:34:56 +08:00
|
|
|
const route = inSetup ? useRoute() : unref(globalRouter.currentRoute);
|
2021-09-11 02:34:36 +08:00
|
|
|
|
2021-09-14 01:31:29 +08:00
|
|
|
/** 跳转首页 */
|
|
|
|
function toHome() {
|
|
|
|
router.push('/');
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:33:36 +08:00
|
|
|
/**
|
|
|
|
* 重定向地址
|
|
|
|
* - current: 取当前的path作为重定向地址
|
|
|
|
*/
|
|
|
|
type LoginRedirect = 'current' | string;
|
2021-09-11 02:34:36 +08:00
|
|
|
/**
|
|
|
|
* 跳转登录页面(通过vue路由)
|
|
|
|
* @param module - 展示的登录模块
|
2021-11-29 20:34:56 +08:00
|
|
|
* @param redirect - 重定向地址(登录成功后跳转的地址)
|
2021-09-11 02:34:36 +08:00
|
|
|
*/
|
2021-11-29 20:34:56 +08:00
|
|
|
function toLogin(module: LoginModuleType = 'pwd-login', redirect: LoginRedirect = 'current') {
|
2021-09-16 18:43:52 +08:00
|
|
|
const routeLocation: RouteLocationRaw = {
|
2021-11-29 20:34:56 +08:00
|
|
|
name: routeName('login'),
|
|
|
|
params: { module }
|
2021-09-16 18:43:52 +08:00
|
|
|
};
|
2021-11-29 20:34:56 +08:00
|
|
|
if (redirect) {
|
|
|
|
let url = redirect;
|
|
|
|
if (redirect === 'current') {
|
2021-11-19 01:33:36 +08:00
|
|
|
url = router.currentRoute.value.fullPath;
|
2021-10-18 18:01:14 +08:00
|
|
|
}
|
2021-11-29 20:34:56 +08:00
|
|
|
Object.assign(routeLocation, { query: { redirect: 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
|
|
|
/**
|
2021-11-29 20:34:56 +08:00
|
|
|
* 登陆页跳转登陆页(登录模块切换)
|
2021-09-16 20:11:45 +08:00
|
|
|
* @param module - 展示的登录模块
|
|
|
|
* @param query - 查询参数
|
|
|
|
*/
|
|
|
|
function toCurrentLogin(module: LoginModuleType) {
|
2021-11-29 20:34:56 +08:00
|
|
|
const { query } = route;
|
|
|
|
router.push({ name: routeName('login'), params: { module }, query: { ...query } });
|
2021-09-16 20:11:45 +08:00
|
|
|
}
|
|
|
|
|
2021-10-18 18:01:14 +08:00
|
|
|
/** 登录后跳转重定向的地址 */
|
2021-11-19 01:33:36 +08:00
|
|
|
function toLoginRedirectUrl(path: string) {
|
2021-10-18 18:01:14 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
}
|