perf:优化未登陆时的跳转路由体验

This commit is contained in:
Xwen
2023-12-16 22:46:10 +08:00
parent 6fd7baa10f
commit 0d5e993042
3 changed files with 16 additions and 9 deletions

View File

@@ -80,8 +80,6 @@ export default function useAuths(opt) {
Session.clear();
Local.clear();
removeToken();
window.location.reload();
Session.set("vuex", null);
};
// 用户名密码登录
@@ -116,10 +114,16 @@ export default function useAuths(opt) {
// 存储用户信息
await getUserInfo(); // 用户信息
// 登录成功后 路由跳转
router.replace({
path: option.loginReUrl ? option.loginReUrl : option.homeUrl,
query: option.otherQuery,
});
// 如果有记录当前跳转页面
const currentPath = Session.get("currentPath");
if (currentPath) {
router.push(currentPath);
} else {
router.replace({
path: option.loginReUrl ? option.loginReUrl : option.homeUrl,
query: option.otherQuery,
});
}
} catch (error) {
removeToken();
return false;

View File

@@ -69,14 +69,16 @@
<script setup>
import AvatarInfo from "@/components/AvatarInfo.vue";
import { ref } from "vue";
import { useRouter } from "vue-router";
import { useRoute, useRouter } from "vue-router";
import useUserStore from "@/stores/user.js";
import useConfigStore from "@/stores/config";
import useAuths from "@/hooks/useAuths";
import { Session } from "@/utils/storage";
const { getToken } = useAuths();
const configStore = useConfigStore();
const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
const activeIndex = ref("1");
const searchText = ref("");
@@ -106,6 +108,7 @@ const enterProfile = () => {
router.push("/profile");
};
const toLogin = () => {
Session.set("currentPath", route.path);
router.push("/login");
};
const search = () => {

View File

@@ -3,7 +3,7 @@ import { getUserDetailInfo } from "@/apis/auth";
import useAuths from "@/hooks/useAuths";
import { defineStore } from "pinia";
const { getToken, setToken, removeToken } = useAuths();
const { getToken, setToken, clearStorage } = useAuths();
const useUserStore = defineStore("user", {
state: () => ({
@@ -81,7 +81,7 @@ const useUserStore = defineStore("user", {
this.token = "";
this.roles = [];
this.permissions = [];
removeToken();
clearStorage();
resolve();
})
.catch((error) => {