feat(projects): 添加路由跳转浏览器新标签

This commit is contained in:
Soybean 2021-11-30 22:00:59 +08:00
parent 2ad1ad32b8
commit 987cef3363
2 changed files with 33 additions and 19 deletions

View File

@ -1,7 +1,7 @@
import { unref } from 'vue'; import { unref } from 'vue';
import { useRouter, useRoute } from 'vue-router'; import { useRouter, useRoute } from 'vue-router';
import type { RouteLocationRaw } from 'vue-router'; import type { RouteLocationRaw } from 'vue-router';
import { router as globalRouter, routeName } from '@/router'; import { router as globalRouter, routeName, ROUTE_HOME } from '@/router';
import type { LoginModuleType } from '@/interface'; import type { LoginModuleType } from '@/interface';
/** /**
@ -12,9 +12,26 @@ export function useRouterPush(inSetup: boolean = true) {
const router = inSetup ? useRouter() : globalRouter; const router = inSetup ? useRouter() : globalRouter;
const route = inSetup ? useRoute() : unref(globalRouter.currentRoute); const route = inSetup ? useRoute() : unref(globalRouter.currentRoute);
/** 跳转首页 */ /**
function toHome() { *
router.push('/'); * @param to -
* @param newTab -
*/
function routerPush(to: RouteLocationRaw, newTab = false) {
if (newTab) {
const routerData = router.resolve(to);
window.open(routerData.href, '_blank');
return;
}
router.push(to);
}
/**
*
* @param newTab -
*/
function toHome(newTab = false) {
routerPush(ROUTE_HOME.path, newTab);
} }
/** /**
@ -22,12 +39,14 @@ export function useRouterPush(inSetup: boolean = true) {
* - current: 取当前的path作为重定向地址 * - current: 取当前的path作为重定向地址
*/ */
type LoginRedirect = 'current' | string; type LoginRedirect = 'current' | string;
/** /**
* (vue路由) * (vue路由)
* @param module - * @param module -
* @param redirect - () * @param redirect - ()
* @param newTab -
*/ */
function toLogin(module: LoginModuleType = 'pwd-login', redirect: LoginRedirect = 'current') { function toLogin(module: LoginModuleType = 'code-login', redirect: LoginRedirect = 'current', newTab = false) {
const routeLocation: RouteLocationRaw = { const routeLocation: RouteLocationRaw = {
name: routeName('login'), name: routeName('login'),
params: { module } params: { module }
@ -35,32 +54,27 @@ export function useRouterPush(inSetup: boolean = true) {
if (redirect) { if (redirect) {
let url = redirect; let url = redirect;
if (redirect === 'current') { if (redirect === 'current') {
url = router.currentRoute.value.fullPath; url = route.fullPath;
} }
Object.assign(routeLocation, { query: { redirect: url } }); Object.assign(routeLocation, { query: { redirect: url } });
} }
router.push(routeLocation); routerPush(routeLocation, newTab);
} }
/** /**
* () * ()
* @param module - * @param module -
* @param query - * @param newTab -
*/ */
function toCurrentLogin(module: LoginModuleType) { function toCurrentLogin(module: LoginModuleType, newTab = false) {
const { query } = route; const { query } = route;
router.push({ name: routeName('login'), params: { module }, query: { ...query } }); routerPush({ name: routeName('login'), params: { module }, query: { ...query } }, newTab);
}
/** 登录后跳转重定向的地址 */
function toLoginRedirectUrl(path: string) {
router.push(path);
} }
return { return {
routerPush,
toHome, toHome,
toLogin, toLogin,
toCurrentLogin, toCurrentLogin
toLoginRedirectUrl
}; };
} }

View File

@ -43,7 +43,7 @@ import { OtherLogin } from './components';
const notification = useNotification(); const notification = useNotification();
const auth = useAuthStore(); const auth = useAuthStore();
const { toHome, toCurrentLogin, toLoginRedirectUrl } = useRouterPush(); const { routerPush, toHome, toCurrentLogin } = useRouterPush();
const { loginRedirectUrl } = useRouteQuery(); const { loginRedirectUrl } = useRouteQuery();
const { loading, startLoading, endLoading } = useLoading(); const { loading, startLoading, endLoading } = useLoading();
@ -77,7 +77,7 @@ function handleSubmit(e: MouseEvent) {
endLoading(); endLoading();
setToken('temp-token'); setToken('temp-token');
if (loginRedirectUrl.value) { if (loginRedirectUrl.value) {
toLoginRedirectUrl(loginRedirectUrl.value); routerPush(loginRedirectUrl.value);
} else { } else {
toHome(); toHome();
} }