refactor: 项目重构

This commit is contained in:
郝先瑞
2024-02-07 21:33:51 +08:00
parent cf8a76c203
commit 56f5ac3802
44 changed files with 1005 additions and 1257 deletions

View File

@@ -1,5 +1,3 @@
import { defineStore } from "pinia";
import { useStorage } from "@vueuse/core";
import defaultSettings from "@/settings";
// 导入 Element Plus 中英文语言包
@@ -19,7 +17,7 @@ export const useAppStore = defineStore("app", () => {
opened: sidebarStatus.value !== "closed",
withoutAnimation: false,
});
const activeTopMenu = useStorage("activeTop", "");
const activeTopMenuPath = useStorage("activeTopMenuPath", "");
/**
* 根据语言标识读取对应的语言包
*/
@@ -72,8 +70,8 @@ export const useAppStore = defineStore("app", () => {
/**
* 混合模式顶部切换
*/
function changeTopActive(val: string) {
activeTopMenu.value = val;
function activeTopMenu(val: string) {
activeTopMenuPath.value = val;
}
return {
device,
@@ -88,6 +86,6 @@ export const useAppStore = defineStore("app", () => {
toggleSidebar,
closeSideBar,
openSideBar,
changeTopActive,
activeTopMenuPath,
};
});

View File

@@ -100,17 +100,24 @@ export const usePermissionStore = defineStore("permission", () => {
}
/**
* 混合模式左侧菜单
* 获取与激活的顶部菜单项相关的混合模式左侧菜单集合
*/
const mixLeftMenu = ref<RouteRecordRaw[]>([]);
function getMixLeftMenu(activeTop: string) {
routes.value.forEach((item) => {
if (item.path === activeTop) {
mixLeftMenu.value = item.children || [];
}
});
const mixLeftMenus = ref<RouteRecordRaw[]>([]);
function setMixLeftMenus(activeTopMenu: string) {
const matchedItem = routes.value.find(
(item) => item.path === activeTopMenu
);
if (matchedItem && matchedItem.children) {
mixLeftMenus.value = matchedItem.children;
}
}
return { routes, setRoutes, generateRoutes, getMixLeftMenu, mixLeftMenu };
return {
routes,
setRoutes,
generateRoutes,
mixLeftMenus,
setMixLeftMenus,
};
});
// 非setup

View File

@@ -1,5 +1,3 @@
import { defineStore } from "pinia";
export const useTagsViewStore = defineStore("tagsView", () => {
const visitedViews = ref<TagView[]>([]);
const cachedViews = ref<string[]>([]);
@@ -9,7 +7,7 @@ export const useTagsViewStore = defineStore("tagsView", () => {
*/
function addVisitedView(view: TagView) {
// 如果已经存在于已访问的视图列表中,则不再添加
if (visitedViews.value.some((v) => v.fullPath === view.fullPath)) {
if (visitedViews.value.some((v) => v.path === view.path)) {
return;
}
// 如果视图是固定的affix则在已访问的视图列表的开头添加

View File

@@ -1,5 +1,3 @@
import { defineStore } from "pinia";
import { loginApi, logoutApi } from "@/api/auth";
import { getUserInfoApi } from "@/api/user";
import { resetRouter } from "@/router";
@@ -8,16 +6,12 @@ import { store } from "@/store";
import { LoginData } from "@/api/auth/types";
import { UserInfo } from "@/api/user/types";
import { useStorage } from "@vueuse/core";
export const useUserStore = defineStore("user", () => {
const user: UserInfo = {
roles: [],
perms: [],
};
const token = useStorage("accessToken", "");
/**
* 登录
*
@@ -29,7 +23,7 @@ export const useUserStore = defineStore("user", () => {
loginApi(loginData)
.then((response) => {
const { tokenType, accessToken } = response.data;
token.value = tokenType + " " + accessToken; // Bearer eyJhbGciOiJIUzI1NiJ9.xxx.xxx
localStorage.setItem("token", tokenType + " " + accessToken); // Bearer eyJhbGciOiJIUzI1NiJ9.xxx.xxx
resolve();
})
.catch((error) => {
@@ -65,7 +59,7 @@ export const useUserStore = defineStore("user", () => {
return new Promise<void>((resolve, reject) => {
logoutApi()
.then(() => {
token.value = "";
localStorage.setItem("token", "");
location.reload(); // 清空路由
resolve();
})
@@ -77,15 +71,15 @@ export const useUserStore = defineStore("user", () => {
// remove token
function resetToken() {
console.log("resetToken");
return new Promise<void>((resolve) => {
token.value = "";
localStorage.setItem("token", "");
resetRouter();
resolve();
});
}
return {
token,
user,
login,
getUserInfo,