refactor(projects): 登录重定向地址相关重构

This commit is contained in:
Soybean 2021-10-18 18:01:14 +08:00
parent 336c7766f9
commit 04008b63ee
11 changed files with 52 additions and 52 deletions

View File

@ -1,5 +1,6 @@
import { computed } from 'vue';
import { useRoute } from 'vue-router';
import { EnumRoutePath } from '@/enum';
import { RouteNameMap } from '@/router';
export default function useRouteQuery() {
@ -7,9 +8,9 @@ export default function useRouteQuery() {
/** 登录跳转链接 */
const loginRedirectUrl = computed(() => {
let url = '';
let url: EnumRoutePath | undefined;
if (route.name === RouteNameMap.get('login')) {
url = (route.query?.redirectUrl as string) ?? '';
url = (route.query?.redirectUrl as EnumRoutePath) || '';
}
return url;
});

View File

@ -4,16 +4,11 @@ import { EnumRoutePath } from '@/enum';
import { router as globalRouter, RouteNameMap } from '@/router';
import type { LoginModuleType } from '@/interface';
interface LoginRedirect {
/**
*
* - current: 取当前的地址作为重定向地址
* - custom: 自定义地址作为重定向地址
*/
type: 'current' | 'custom' | 'no';
/** 自定义地址 */
url: string;
}
/**
*
* - current: 取当前的path作为重定向地址
*/
type LoginRedirect = 'current' | EnumRoutePath;
/**
*
@ -31,21 +26,19 @@ export default function useRouterChange(inSetup: boolean = true) {
/**
* (vue路由)
* @param module -
* @param addRedirect -
* @param redirect -
* @param redirectUrl -
*/
function toLogin(
module: LoginModuleType = 'pwd-login',
addRedirect: boolean = false,
redirect: LoginRedirect = { type: 'current', url: '' }
) {
const redirectUrl = redirect.type === 'current' ? window.location.href : redirect.url;
function toLogin(module: LoginModuleType = 'pwd-login', redirectUrl?: LoginRedirect) {
const routeLocation: RouteLocationRaw = {
name: RouteNameMap.get('login'),
params: { module }
};
if (addRedirect) {
routeLocation.query = { redirectUrl };
if (redirectUrl) {
let url = redirectUrl;
if (redirectUrl === 'current') {
url = router.currentRoute.value.fullPath as EnumRoutePath;
}
routeLocation.query = { redirectUrl: url };
}
router.push(routeLocation);
}
@ -62,14 +55,15 @@ export default function useRouterChange(inSetup: boolean = true) {
}
}
function toReload(redirectUrl: string) {
router.push({ path: EnumRoutePath.reload, query: { redirectUrl } });
/** 登录后跳转重定向的地址 */
function toLoginRedirectUrl(path: EnumRoutePath) {
router.push(path);
}
return {
toHome,
toLogin,
toCurrentLogin,
toReload
toLoginRedirectUrl
};
}

View File

@ -13,9 +13,11 @@ import { UserAvatar, Logout } from '@vicons/carbon';
import { dynamicIconRender, resetAuthStorage } from '@/utils';
import { HoverContainer } from '@/components';
import avatar from '@/assets/svg/avatar/avatar01.svg';
import { useRouterChange } from '@/hooks';
type DropdownKey = 'user-center' | 'logout';
const { toLogin } = useRouterChange();
const dialog = useDialog();
const options = [
@ -45,7 +47,7 @@ function handleDropdown(optionKey: string) {
negativeText: '取消',
onPositiveClick: () => {
resetAuthStorage();
window.location.reload();
toLogin('pwd-login', 'current');
}
});
}

View File

@ -6,6 +6,7 @@
:collapsed="app.menu.collapsed"
:collapsed-width="theme.menuStyle.collapsedWidth"
:width="menuWidth"
show-trigger="bar"
@collapse="handleMenuCollapse(true)"
@expand="handleMenuCollapse(false)"
>

View File

@ -1,6 +1,6 @@
import type { Router, RouteLocationNormalized, NavigationGuardNext } from 'vue-router';
import { useTitle } from '@vueuse/core';
import { getToken } from '@/utils';
import { getToken, getLoginRedirectUrl } from '@/utils';
import { RouteNameMap } from '../helpers';
/**
@ -46,7 +46,7 @@ function handleRouterAction(to: RouteLocationNormalized, from: RouteLocationNorm
[
!isLogin && needLogin,
() => {
const redirectUrl = window.location.href;
const redirectUrl = getLoginRedirectUrl();
next({ name: RouteNameMap.get('login'), query: { redirectUrl } });
}
],

View File

@ -1,2 +1 @@
export { getToken, setToken, removeToken, getUserInfo, resetAuthStorage, getLoginModuleRegExp } from './user';
export { getLoginRedirectUrl, toLoginRedirectUrl } from './location';

View File

@ -1,9 +0,0 @@
/** 获取登录重定向的地址 */
export function getLoginRedirectUrl() {
return window.location.href;
}
/** 登录后跳转重定向的地址 */
export function toLoginRedirectUrl(redirectUrl: string) {
window.location.href = redirectUrl;
}

View File

@ -1,13 +1,4 @@
export {
setToken,
getToken,
removeToken,
getUserInfo,
resetAuthStorage,
getLoginModuleRegExp,
getLoginRedirectUrl,
toLoginRedirectUrl
} from './auth';
export { setToken, getToken, removeToken, getUserInfo, resetAuthStorage, getLoginModuleRegExp } from './auth';
export {
isNumber,
@ -39,4 +30,4 @@ export {
clearSession
} from './storage';
export { getRouteNameMap, setRouterCacheName } from './router';
export { getRouteNameMap, setRouterCacheName, getLoginRedirectUrl } from './router';

View File

@ -1,6 +1,7 @@
import type { Component } from 'vue';
import { EnumRoutePath } from '@/enum';
import type { RoutePathKey } from '@/interface';
import { router } from '@/router';
/** 获取路由name map */
export function getRouteNameMap() {
@ -13,3 +14,9 @@ export function setRouterCacheName(component: Component, name?: string) {
Object.assign(component, { name });
}
}
export function getLoginRedirectUrl() {
const path = router.currentRoute.value.fullPath as EnumRoutePath;
const redirectUrl = path === EnumRoutePath.root ? undefined : path;
return redirectUrl;
}

View File

@ -41,6 +41,9 @@
<n-radio :default-checked="true" />
</n-space>
</div>
<n-space>
<n-button type="primary" @click="handleDialog">Dialog</n-button>
</n-space>
</n-space>
</n-spin>
</div>
@ -48,12 +51,23 @@
<script lang="ts" setup>
import { ref } from 'vue';
import { NGradientText, NSpace, NButton, NSpin, NTag, NSwitch, NCheckbox, NRadio } from 'naive-ui';
import { NGradientText, NSpace, NButton, NSpin, NTag, NSwitch, NCheckbox, NRadio, useDialog } from 'naive-ui';
const dialog = useDialog();
const loading = ref(true);
setTimeout(() => {
loading.value = false;
}, 1500);
function handleDialog() {
dialog.info({
title: '提示',
content: '您确定要退出登录吗?',
positiveText: '确定',
negativeText: '取消'
});
}
</script>
<style scoped></style>

View File

@ -40,12 +40,12 @@ import type { FormInst, FormRules } from 'naive-ui';
import { EnumLoginModule } from '@/enum';
import { useThemeStore } from '@/store';
import { useRouterChange, useRouteQuery } from '@/hooks';
import { setToken, toLoginRedirectUrl } from '@/utils';
import { setToken } from '@/utils';
import { OtherLogin } from './components';
import logo from '@/assets/img/common/logo.png';
const theme = useThemeStore();
const { toHome, toCurrentLogin } = useRouterChange();
const { toHome, toCurrentLogin, toLoginRedirectUrl } = useRouterChange();
const { loginRedirectUrl } = useRouteQuery();
const notification = useNotification();