refactor(projects): 登录重定向地址相关重构
This commit is contained in:
parent
336c7766f9
commit
04008b63ee
@ -1,5 +1,6 @@
|
|||||||
import { computed } from 'vue';
|
import { computed } from 'vue';
|
||||||
import { useRoute } from 'vue-router';
|
import { useRoute } from 'vue-router';
|
||||||
|
import { EnumRoutePath } from '@/enum';
|
||||||
import { RouteNameMap } from '@/router';
|
import { RouteNameMap } from '@/router';
|
||||||
|
|
||||||
export default function useRouteQuery() {
|
export default function useRouteQuery() {
|
||||||
@ -7,9 +8,9 @@ export default function useRouteQuery() {
|
|||||||
|
|
||||||
/** 登录跳转链接 */
|
/** 登录跳转链接 */
|
||||||
const loginRedirectUrl = computed(() => {
|
const loginRedirectUrl = computed(() => {
|
||||||
let url = '';
|
let url: EnumRoutePath | undefined;
|
||||||
if (route.name === RouteNameMap.get('login')) {
|
if (route.name === RouteNameMap.get('login')) {
|
||||||
url = (route.query?.redirectUrl as string) ?? '';
|
url = (route.query?.redirectUrl as EnumRoutePath) || '';
|
||||||
}
|
}
|
||||||
return url;
|
return url;
|
||||||
});
|
});
|
||||||
|
@ -4,16 +4,11 @@ import { EnumRoutePath } from '@/enum';
|
|||||||
import { router as globalRouter, RouteNameMap } from '@/router';
|
import { router as globalRouter, RouteNameMap } from '@/router';
|
||||||
import type { LoginModuleType } from '@/interface';
|
import type { LoginModuleType } from '@/interface';
|
||||||
|
|
||||||
interface LoginRedirect {
|
/**
|
||||||
/**
|
* 重定向地址
|
||||||
* 重定向类型
|
* - current: 取当前的path作为重定向地址
|
||||||
* - current: 取当前的地址作为重定向地址
|
*/
|
||||||
* - custom: 自定义地址作为重定向地址
|
type LoginRedirect = 'current' | EnumRoutePath;
|
||||||
*/
|
|
||||||
type: 'current' | 'custom' | 'no';
|
|
||||||
/** 自定义地址 */
|
|
||||||
url: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 路由跳转
|
* 路由跳转
|
||||||
@ -31,21 +26,19 @@ export default function useRouterChange(inSetup: boolean = true) {
|
|||||||
/**
|
/**
|
||||||
* 跳转登录页面(通过vue路由)
|
* 跳转登录页面(通过vue路由)
|
||||||
* @param module - 展示的登录模块
|
* @param module - 展示的登录模块
|
||||||
* @param addRedirect - 是否添加重定向地址
|
* @param redirectUrl - 重定向地址
|
||||||
* @param redirect - 登录后重定向相关配置
|
|
||||||
*/
|
*/
|
||||||
function toLogin(
|
function toLogin(module: LoginModuleType = 'pwd-login', redirectUrl?: LoginRedirect) {
|
||||||
module: LoginModuleType = 'pwd-login',
|
|
||||||
addRedirect: boolean = false,
|
|
||||||
redirect: LoginRedirect = { type: 'current', url: '' }
|
|
||||||
) {
|
|
||||||
const redirectUrl = redirect.type === 'current' ? window.location.href : redirect.url;
|
|
||||||
const routeLocation: RouteLocationRaw = {
|
const routeLocation: RouteLocationRaw = {
|
||||||
name: RouteNameMap.get('login'),
|
name: RouteNameMap.get('login'),
|
||||||
params: { module }
|
params: { module }
|
||||||
};
|
};
|
||||||
if (addRedirect) {
|
if (redirectUrl) {
|
||||||
routeLocation.query = { redirectUrl };
|
let url = redirectUrl;
|
||||||
|
if (redirectUrl === 'current') {
|
||||||
|
url = router.currentRoute.value.fullPath as EnumRoutePath;
|
||||||
|
}
|
||||||
|
routeLocation.query = { redirectUrl: url };
|
||||||
}
|
}
|
||||||
router.push(routeLocation);
|
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 {
|
return {
|
||||||
toHome,
|
toHome,
|
||||||
toLogin,
|
toLogin,
|
||||||
toCurrentLogin,
|
toCurrentLogin,
|
||||||
toReload
|
toLoginRedirectUrl
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,11 @@ import { UserAvatar, Logout } from '@vicons/carbon';
|
|||||||
import { dynamicIconRender, resetAuthStorage } from '@/utils';
|
import { dynamicIconRender, resetAuthStorage } from '@/utils';
|
||||||
import { HoverContainer } from '@/components';
|
import { HoverContainer } from '@/components';
|
||||||
import avatar from '@/assets/svg/avatar/avatar01.svg';
|
import avatar from '@/assets/svg/avatar/avatar01.svg';
|
||||||
|
import { useRouterChange } from '@/hooks';
|
||||||
|
|
||||||
type DropdownKey = 'user-center' | 'logout';
|
type DropdownKey = 'user-center' | 'logout';
|
||||||
|
|
||||||
|
const { toLogin } = useRouterChange();
|
||||||
const dialog = useDialog();
|
const dialog = useDialog();
|
||||||
|
|
||||||
const options = [
|
const options = [
|
||||||
@ -45,7 +47,7 @@ function handleDropdown(optionKey: string) {
|
|||||||
negativeText: '取消',
|
negativeText: '取消',
|
||||||
onPositiveClick: () => {
|
onPositiveClick: () => {
|
||||||
resetAuthStorage();
|
resetAuthStorage();
|
||||||
window.location.reload();
|
toLogin('pwd-login', 'current');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
:collapsed="app.menu.collapsed"
|
:collapsed="app.menu.collapsed"
|
||||||
:collapsed-width="theme.menuStyle.collapsedWidth"
|
:collapsed-width="theme.menuStyle.collapsedWidth"
|
||||||
:width="menuWidth"
|
:width="menuWidth"
|
||||||
|
show-trigger="bar"
|
||||||
@collapse="handleMenuCollapse(true)"
|
@collapse="handleMenuCollapse(true)"
|
||||||
@expand="handleMenuCollapse(false)"
|
@expand="handleMenuCollapse(false)"
|
||||||
>
|
>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import type { Router, RouteLocationNormalized, NavigationGuardNext } from 'vue-router';
|
import type { Router, RouteLocationNormalized, NavigationGuardNext } from 'vue-router';
|
||||||
import { useTitle } from '@vueuse/core';
|
import { useTitle } from '@vueuse/core';
|
||||||
import { getToken } from '@/utils';
|
import { getToken, getLoginRedirectUrl } from '@/utils';
|
||||||
import { RouteNameMap } from '../helpers';
|
import { RouteNameMap } from '../helpers';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,7 +46,7 @@ function handleRouterAction(to: RouteLocationNormalized, from: RouteLocationNorm
|
|||||||
[
|
[
|
||||||
!isLogin && needLogin,
|
!isLogin && needLogin,
|
||||||
() => {
|
() => {
|
||||||
const redirectUrl = window.location.href;
|
const redirectUrl = getLoginRedirectUrl();
|
||||||
next({ name: RouteNameMap.get('login'), query: { redirectUrl } });
|
next({ name: RouteNameMap.get('login'), query: { redirectUrl } });
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -1,2 +1 @@
|
|||||||
export { getToken, setToken, removeToken, getUserInfo, resetAuthStorage, getLoginModuleRegExp } from './user';
|
export { getToken, setToken, removeToken, getUserInfo, resetAuthStorage, getLoginModuleRegExp } from './user';
|
||||||
export { getLoginRedirectUrl, toLoginRedirectUrl } from './location';
|
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
/** 获取登录重定向的地址 */
|
|
||||||
export function getLoginRedirectUrl() {
|
|
||||||
return window.location.href;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 登录后跳转重定向的地址 */
|
|
||||||
export function toLoginRedirectUrl(redirectUrl: string) {
|
|
||||||
window.location.href = redirectUrl;
|
|
||||||
}
|
|
@ -1,13 +1,4 @@
|
|||||||
export {
|
export { setToken, getToken, removeToken, getUserInfo, resetAuthStorage, getLoginModuleRegExp } from './auth';
|
||||||
setToken,
|
|
||||||
getToken,
|
|
||||||
removeToken,
|
|
||||||
getUserInfo,
|
|
||||||
resetAuthStorage,
|
|
||||||
getLoginModuleRegExp,
|
|
||||||
getLoginRedirectUrl,
|
|
||||||
toLoginRedirectUrl
|
|
||||||
} from './auth';
|
|
||||||
|
|
||||||
export {
|
export {
|
||||||
isNumber,
|
isNumber,
|
||||||
@ -39,4 +30,4 @@ export {
|
|||||||
clearSession
|
clearSession
|
||||||
} from './storage';
|
} from './storage';
|
||||||
|
|
||||||
export { getRouteNameMap, setRouterCacheName } from './router';
|
export { getRouteNameMap, setRouterCacheName, getLoginRedirectUrl } from './router';
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import type { Component } from 'vue';
|
import type { Component } from 'vue';
|
||||||
import { EnumRoutePath } from '@/enum';
|
import { EnumRoutePath } from '@/enum';
|
||||||
import type { RoutePathKey } from '@/interface';
|
import type { RoutePathKey } from '@/interface';
|
||||||
|
import { router } from '@/router';
|
||||||
|
|
||||||
/** 获取路由name map */
|
/** 获取路由name map */
|
||||||
export function getRouteNameMap() {
|
export function getRouteNameMap() {
|
||||||
@ -13,3 +14,9 @@ export function setRouterCacheName(component: Component, name?: string) {
|
|||||||
Object.assign(component, { name });
|
Object.assign(component, { name });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getLoginRedirectUrl() {
|
||||||
|
const path = router.currentRoute.value.fullPath as EnumRoutePath;
|
||||||
|
const redirectUrl = path === EnumRoutePath.root ? undefined : path;
|
||||||
|
return redirectUrl;
|
||||||
|
}
|
||||||
|
@ -41,6 +41,9 @@
|
|||||||
<n-radio :default-checked="true" />
|
<n-radio :default-checked="true" />
|
||||||
</n-space>
|
</n-space>
|
||||||
</div>
|
</div>
|
||||||
|
<n-space>
|
||||||
|
<n-button type="primary" @click="handleDialog">Dialog</n-button>
|
||||||
|
</n-space>
|
||||||
</n-space>
|
</n-space>
|
||||||
</n-spin>
|
</n-spin>
|
||||||
</div>
|
</div>
|
||||||
@ -48,12 +51,23 @@
|
|||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ref } from 'vue';
|
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);
|
const loading = ref(true);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
}, 1500);
|
}, 1500);
|
||||||
|
|
||||||
|
function handleDialog() {
|
||||||
|
dialog.info({
|
||||||
|
title: '提示',
|
||||||
|
content: '您确定要退出登录吗?',
|
||||||
|
positiveText: '确定',
|
||||||
|
negativeText: '取消'
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
<style scoped></style>
|
<style scoped></style>
|
||||||
|
@ -40,12 +40,12 @@ import type { FormInst, FormRules } from 'naive-ui';
|
|||||||
import { EnumLoginModule } from '@/enum';
|
import { EnumLoginModule } from '@/enum';
|
||||||
import { useThemeStore } from '@/store';
|
import { useThemeStore } from '@/store';
|
||||||
import { useRouterChange, useRouteQuery } from '@/hooks';
|
import { useRouterChange, useRouteQuery } from '@/hooks';
|
||||||
import { setToken, toLoginRedirectUrl } from '@/utils';
|
import { setToken } from '@/utils';
|
||||||
import { OtherLogin } from './components';
|
import { OtherLogin } from './components';
|
||||||
import logo from '@/assets/img/common/logo.png';
|
import logo from '@/assets/img/common/logo.png';
|
||||||
|
|
||||||
const theme = useThemeStore();
|
const theme = useThemeStore();
|
||||||
const { toHome, toCurrentLogin } = useRouterChange();
|
const { toHome, toCurrentLogin, toLoginRedirectUrl } = useRouterChange();
|
||||||
const { loginRedirectUrl } = useRouteQuery();
|
const { loginRedirectUrl } = useRouteQuery();
|
||||||
const notification = useNotification();
|
const notification = useNotification();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user