优化backend前端布局
This commit is contained in:
1 parent
68617335f0
commit
e34543465a
11 files changed
+1899
-903
No files matched your search
+50
-28
@@ -58,27 +58,23 @@ import { ref as vueRef } from 'vue';
|
||||
* }
|
||||
*/
|
||||
export const useTabsStore = defineTabsStore('tabs', () => {
|
||||
// 固定首页tab
|
||||
const defaultDashboardPath = '/home';
|
||||
// 默认标签页:不再固定「首页」,而是由 Main.vue 按当前模块设置成仪表盘
|
||||
const defaultDashboardPath = vueRef('');
|
||||
|
||||
// 从 localStorage 恢复 tabs 状态
|
||||
// 从 localStorage 恢复 tabs 状态(兼容旧版本里遗留的 /home 首页标签)
|
||||
function loadTabsFromStorage() {
|
||||
try {
|
||||
const savedTabs = localStorage.getItem('tabs_list');
|
||||
const savedActiveTab = localStorage.getItem('active_tab');
|
||||
if (savedTabs) {
|
||||
const tabs = JSON.parse(savedTabs);
|
||||
// 确保至少包含首页
|
||||
const hasHome = tabs.some(t => t.fullPath === defaultDashboardPath);
|
||||
if (!hasHome) {
|
||||
tabs.unshift({ title: '首页', fullPath: defaultDashboardPath, name: 'Home' });
|
||||
if (Array.isArray(tabs)) {
|
||||
return tabs.filter(t => t && t.fullPath && t.fullPath !== '/home');
|
||||
}
|
||||
return tabs;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('恢复 tabs 失败:', e);
|
||||
}
|
||||
return [{ title: '首页', fullPath: defaultDashboardPath, name: 'Home' }];
|
||||
return [];
|
||||
}
|
||||
|
||||
// 保存 tabs 到 localStorage
|
||||
@@ -95,7 +91,29 @@ export const useTabsStore = defineTabsStore('tabs', () => {
|
||||
|
||||
const tabList = vueRef(loadTabsFromStorage());
|
||||
const savedActiveTab = localStorage.getItem('active_tab');
|
||||
const activeTab = vueRef(savedActiveTab || defaultDashboardPath);
|
||||
const activeTab = vueRef(
|
||||
savedActiveTab && savedActiveTab !== '/home' ? savedActiveTab : ''
|
||||
);
|
||||
|
||||
// 设置当前模块的默认仪表盘标签:始终排在第一位且不可关闭
|
||||
function setDefaultTab(tab) {
|
||||
const path = tab?.fullPath || '';
|
||||
if (!path) return;
|
||||
defaultDashboardPath.value = path;
|
||||
|
||||
const idx = tabList.value.findIndex(t => t.fullPath === path);
|
||||
if (idx === -1) {
|
||||
tabList.value.unshift(tab);
|
||||
} else if (idx > 0) {
|
||||
const [exist] = tabList.value.splice(idx, 1);
|
||||
tabList.value.unshift(exist);
|
||||
}
|
||||
|
||||
if (!activeTab.value || !tabList.value.some(t => t.fullPath === activeTab.value)) {
|
||||
activeTab.value = path;
|
||||
}
|
||||
saveTabsToStorage(tabList.value, activeTab.value);
|
||||
}
|
||||
|
||||
// 添加tab,若已存在则激活
|
||||
function addTab(tab) {
|
||||
@@ -107,8 +125,9 @@ export const useTabsStore = defineTabsStore('tabs', () => {
|
||||
saveTabsToStorage(tabList.value, activeTab.value);
|
||||
}
|
||||
|
||||
// 删除指定tab并切换激活tab
|
||||
// 删除指定tab并切换激活tab(默认仪表盘标签不可关闭)
|
||||
function removeTab(fullPath) {
|
||||
if (fullPath === defaultDashboardPath.value) return;
|
||||
const idx = tabList.value.findIndex((t) => t.fullPath === fullPath);
|
||||
if (idx > -1) {
|
||||
tabList.value.splice(idx, 1);
|
||||
@@ -119,33 +138,31 @@ export const useTabsStore = defineTabsStore('tabs', () => {
|
||||
const newIdx = idx >= tabList.value.length ? tabList.value.length - 1 : idx;
|
||||
activeTab.value = tabList.value[newIdx].fullPath;
|
||||
} else {
|
||||
// 全部关闭,兜底首页
|
||||
activeTab.value = defaultDashboardPath;
|
||||
activeTab.value = defaultDashboardPath.value;
|
||||
}
|
||||
}
|
||||
saveTabsToStorage(tabList.value, activeTab.value);
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭其他,只留首页和当前激活tab
|
||||
// 关闭其他,只留默认仪表盘和当前激活tab
|
||||
function closeOthers() {
|
||||
tabList.value = tabList.value.filter(
|
||||
(t) => t.fullPath === defaultDashboardPath || t.fullPath === activeTab.value
|
||||
(t) => t.fullPath === defaultDashboardPath.value || t.fullPath === activeTab.value
|
||||
);
|
||||
saveTabsToStorage(tabList.value, activeTab.value);
|
||||
}
|
||||
|
||||
// 关闭左侧(关闭指定tab左侧的所有tab,保留首页和目标tab)
|
||||
// 关闭左侧(关闭指定tab左侧的所有tab,保留默认仪表盘和目标tab)
|
||||
function closeLeft(targetFullPath) {
|
||||
const targetIndex = tabList.value.findIndex((t) => t.fullPath === targetFullPath);
|
||||
if (targetIndex > -1) {
|
||||
// 保留首页和目标tab及其右侧的所有tab
|
||||
const beforeIndex = tabList.value.slice(0, targetIndex);
|
||||
const hasCloseableLeft = beforeIndex.some(t => t.fullPath !== defaultDashboardPath);
|
||||
const hasCloseableLeft = beforeIndex.some(t => t.fullPath !== defaultDashboardPath.value);
|
||||
|
||||
if (hasCloseableLeft) {
|
||||
tabList.value = tabList.value.filter((t, index) =>
|
||||
t.fullPath === defaultDashboardPath || index >= targetIndex
|
||||
t.fullPath === defaultDashboardPath.value || index >= targetIndex
|
||||
);
|
||||
// 如果关闭的tab中包含了当前激活的tab,则激活目标tab
|
||||
if (!tabList.value.find(t => t.fullPath === activeTab.value)) {
|
||||
@@ -156,17 +173,16 @@ export const useTabsStore = defineTabsStore('tabs', () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭右侧(关闭指定tab右侧的所有tab,保留首页和目标tab)
|
||||
// 关闭右侧(关闭指定tab右侧的所有tab,保留默认仪表盘和目标tab)
|
||||
function closeRight(targetFullPath) {
|
||||
const targetIndex = tabList.value.findIndex((t) => t.fullPath === targetFullPath);
|
||||
if (targetIndex > -1) {
|
||||
// 保留首页和目标tab及其左侧的所有tab
|
||||
const afterIndex = tabList.value.slice(targetIndex + 1);
|
||||
const hasCloseableRight = afterIndex.length > 0;
|
||||
|
||||
if (hasCloseableRight) {
|
||||
tabList.value = tabList.value.filter((t, index) =>
|
||||
t.fullPath === defaultDashboardPath || index <= targetIndex
|
||||
t.fullPath === defaultDashboardPath.value || index <= targetIndex
|
||||
);
|
||||
// 如果关闭的tab中包含了当前激活的tab,则激活目标tab
|
||||
if (!tabList.value.find(t => t.fullPath === activeTab.value)) {
|
||||
@@ -177,10 +193,13 @@ export const useTabsStore = defineTabsStore('tabs', () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭全部,只留首页
|
||||
// 关闭全部,只留默认仪表盘
|
||||
function closeAll() {
|
||||
tabList.value = tabList.value.filter((t) => t.fullPath === defaultDashboardPath);
|
||||
activeTab.value = defaultDashboardPath;
|
||||
const target = defaultDashboardPath.value || activeTab.value;
|
||||
tabList.value = target
|
||||
? tabList.value.filter((t) => t.fullPath === target)
|
||||
: [];
|
||||
activeTab.value = tabList.value.length ? target : '';
|
||||
saveTabsToStorage(tabList.value, activeTab.value);
|
||||
}
|
||||
|
||||
@@ -192,8 +211,9 @@ export const useTabsStore = defineTabsStore('tabs', () => {
|
||||
|
||||
// 重置 tabs store 到初始状态(登出时使用)
|
||||
function resetTabs() {
|
||||
tabList.value = [{ title: '首页', fullPath: defaultDashboardPath, name: 'Dashboard' }];
|
||||
activeTab.value = defaultDashboardPath;
|
||||
tabList.value = [];
|
||||
activeTab.value = '';
|
||||
defaultDashboardPath.value = '';
|
||||
// 清除 localStorage 中的 tabs 数据
|
||||
localStorage.removeItem('tabs_list');
|
||||
localStorage.removeItem('active_tab');
|
||||
@@ -202,7 +222,9 @@ export const useTabsStore = defineTabsStore('tabs', () => {
|
||||
return {
|
||||
tabList,
|
||||
activeTab,
|
||||
defaultDashboardPath,
|
||||
addTab,
|
||||
setDefaultTab,
|
||||
removeTab,
|
||||
closeOthers,
|
||||
closeLeft,
|
||||
|
||||
Reference in new issue
Block a user