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 = () => { const handleLogout = () => {
router.push(`/login?redirect=${router.currentRoute.value.fullPath}`); router.push({
path: '/login',
query: { redirect: encodeURIComponent(router.currentRoute.value.fullPath) },
});
}; };
const navToGitHub = () => { const navToGitHub = () => {

View File

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

View File

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