feat: 优化登录跳转支持回跳带query参数的页面 (#374)

This commit is contained in:
PDieE 2022-12-09 09:51:08 +08:00 committed by GitHub
parent 6ccc3279f9
commit 6cd9fe9134
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 7 deletions

View File

@ -143,7 +143,10 @@ const handleNav = (url) => {
};
const handleLogout = () => {
router.push(`/login?redirect=${router.currentRoute.value.fullPath}`);
router.push({
path: '/login',
query: { redirect: encodeURIComponent(router.currentRoute.value.fullPath) },
});
};
const navToGitHub = () => {

View File

@ -80,7 +80,7 @@
<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useRoute, useRouter } from 'vue-router';
import QrcodeVue from 'qrcode.vue';
import { FormInstanceFunctions, MessagePlugin } from 'tdesign-vue-next';
import { useCounter } from '@/hooks';
@ -116,6 +116,7 @@ const switchType = (val: string) => {
};
const router = useRouter();
const route = useRoute();
/**
* 发送验证码
@ -134,9 +135,9 @@ const onSubmit = async ({ validateResult }) => {
await userStore.login(formData.value);
MessagePlugin.success('登陆成功');
router.push({
path: '/dashboard/base',
});
const redirect = route.query.redirect as string;
const redirectUrl = redirect ? decodeURIComponent(redirect) : '/dashboard';
router.push(redirectUrl);
} catch (e) {
console.log(e);
MessagePlugin.error(e.message);

View File

@ -40,7 +40,10 @@ router.beforeEach(async (to, from, next) => {
}
} catch (error) {
MessagePlugin.error(error);
next(`/login?redirect=${to.path}`);
next({
path: '/login',
query: { redirect: encodeURIComponent(to.fullPath) },
});
NProgress.done();
}
}
@ -49,7 +52,10 @@ router.beforeEach(async (to, from, next) => {
if (whiteListRouters.indexOf(to.path) !== -1) {
next();
} else {
next(`/login?redirect=${to.path}`);
next({
path: '/login',
query: { redirect: encodeURIComponent(to.fullPath) },
});
}
NProgress.done();
}