From fe5522dc2d72c9799ae3eaad7e7e53a96ecf0d69 Mon Sep 17 00:00:00 2001 From: Jason <5340635+wen-jason@user.noreply.gitee.com> Date: Tue, 20 Sep 2022 10:58:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E6=A0=87=E7=AD=BEtabs=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/package.json | 1 + admin/src/hooks/useMultipleTabs.ts | 47 +++++++++++++ .../components/header/multiple-tabs.vue | 35 ++++------ admin/src/layout/default/components/main.vue | 9 ++- .../default/components/setting/drawer.vue | 1 - admin/src/stores/modules/multipleTabs.ts | 68 ++++++++++++++----- admin/src/stores/modules/user.ts | 11 +-- admin/src/utils/auth.ts | 4 +- admin/src/views/article/column/index.vue | 2 +- admin/src/views/article/lists/edit.vue | 9 ++- admin/src/views/article/lists/index.vue | 6 +- admin/src/views/channel/h5.vue | 2 +- admin/src/views/channel/weapp.vue | 2 +- admin/src/views/channel/wx_dev.vue | 2 +- admin/src/views/channel/wx_oa/config.vue | 2 +- admin/src/views/channel/wx_oa/menu.vue | 2 +- admin/src/views/consumer/lists/detail.vue | 2 +- admin/src/views/consumer/lists/index.vue | 5 +- admin/src/views/decoration/pages/index.vue | 2 +- admin/src/views/decoration/tabbar.vue | 2 +- admin/src/views/dev_tools/code/edit.vue | 6 +- admin/src/views/dev_tools/code/index.vue | 6 +- admin/src/views/material/index.vue | 2 +- admin/src/views/message/notice/edit.vue | 6 +- admin/src/views/message/notice/index.vue | 6 +- .../src/views/message/short_letter/index.vue | 2 +- .../views/organization/department/index.vue | 2 +- admin/src/views/organization/post/index.vue | 2 +- admin/src/views/permission/admin/index.vue | 2 +- admin/src/views/permission/menu/edit.vue | 30 ++++---- admin/src/views/permission/menu/index.vue | 2 +- admin/src/views/permission/role/index.vue | 2 +- admin/src/views/setting/dict/data/index.vue | 2 +- admin/src/views/setting/dict/type/index.vue | 2 +- admin/src/views/setting/search/index.vue | 2 +- admin/src/views/setting/storage/index.vue | 2 +- admin/src/views/setting/system/cache.vue | 2 +- .../src/views/setting/system/environment.vue | 2 +- admin/src/views/setting/system/journal.vue | 2 +- .../src/views/setting/user/login_register.vue | 2 +- admin/src/views/setting/user/setup.vue | 2 +- admin/src/views/setting/website/filing.vue | 2 +- .../src/views/setting/website/information.vue | 2 +- admin/src/views/setting/website/protocol.vue | 2 +- admin/src/views/user/setting.vue | 2 +- admin/src/views/workbench/index.vue | 2 +- admin/typings/router.d.ts | 1 + admin/vite.config.ts | 5 +- admin/yarn.lock | 66 ++++++++++++++++++ 49 files changed, 273 insertions(+), 109 deletions(-) create mode 100644 admin/src/hooks/useMultipleTabs.ts diff --git a/admin/package.json b/admin/package.json index a6681cf9..25c05ad8 100644 --- a/admin/package.json +++ b/admin/package.json @@ -55,6 +55,7 @@ "vite": "^3.0.0", "vite-plugin-style-import": "^2.0.0", "vite-plugin-svg-icons": "^2.0.1", + "vite-plugin-vue-setup-extend": "^0.4.0", "vue-tsc": "^0.38.1" } } diff --git a/admin/src/hooks/useMultipleTabs.ts b/admin/src/hooks/useMultipleTabs.ts new file mode 100644 index 00000000..02e1e435 --- /dev/null +++ b/admin/src/hooks/useMultipleTabs.ts @@ -0,0 +1,47 @@ +import useTabsStore from '@/stores/modules/multipleTabs' +import useSettingStore from '@/stores/modules/setting' + +export default function useMultipleTabs() { + const router = useRouter() + const route = useRoute() + const tabsStore = useTabsStore() + const settingStore = useSettingStore() + + const tabsLists = computed(() => { + return tabsStore.getTabList + }) + + const currentTab = computed(() => { + return route.fullPath + }) + + const addTab = () => { + if (!settingStore.openMultipleTabs) return + tabsStore.addTab(router) + } + + const removeTab = (fullPath?: any) => { + if (!settingStore.openMultipleTabs) return + fullPath = fullPath ?? route.fullPath + tabsStore.removeTab(fullPath, router) + } + + const removeOtherTab = () => { + if (!settingStore.openMultipleTabs) return + tabsStore.removeOtherTab(route) + } + + const removeAllTab = () => { + if (!settingStore.openMultipleTabs) return + tabsStore.removeAllTab(router) + } + + return { + tabsLists, + currentTab, + addTab, + removeTab, + removeOtherTab, + removeAllTab + } +} diff --git a/admin/src/layout/default/components/header/multiple-tabs.vue b/admin/src/layout/default/components/header/multiple-tabs.vue index 2b6fb41b..16a5d48f 100644 --- a/admin/src/layout/default/components/header/multiple-tabs.vue +++ b/admin/src/layout/default/components/header/multiple-tabs.vue @@ -3,12 +3,12 @@
- diff --git a/admin/src/layout/default/components/setting/drawer.vue b/admin/src/layout/default/components/setting/drawer.vue index 19bcc60c..020f6159 100644 --- a/admin/src/layout/default/components/setting/drawer.vue +++ b/admin/src/layout/default/components/setting/drawer.vue @@ -65,7 +65,6 @@ import theme_light from '@/assets/images/theme_white.png' import theme_dark from '@/assets/images/theme_black.png' const settingStore = useSettingStore() - const predefineColors = ref(['#409EFF', '#28C76F', '#EA5455', '#FF9F43', '#01CFE8', '#4A5DFF']) const sideThemeList = [ { diff --git a/admin/src/stores/modules/multipleTabs.ts b/admin/src/stores/modules/multipleTabs.ts index d063546a..b14e3664 100644 --- a/admin/src/stores/modules/multipleTabs.ts +++ b/admin/src/stores/modules/multipleTabs.ts @@ -11,6 +11,7 @@ import { PageEnum } from '@/enums/pageEnum' interface TabItem { name: RouteRecordName + fullPath: string path: string title?: string query?: LocationQuery @@ -24,8 +25,8 @@ interface TabsSate { indexRouteName: RouteRecordName } -const getHasTabIndex = (path: string, tabList: TabItem[]) => { - return tabList.findIndex((item) => item.path == path) +const getHasTabIndex = (fullPath: string, tabList: TabItem[]) => { + return tabList.findIndex((item) => item.fullPath == fullPath) } const isCannotAddRoute = (route: RouteLocationNormalized, router: Router) => { @@ -39,8 +40,12 @@ const isCannotAddRoute = (route: RouteLocationNormalized, router: Router) => { return false } -const findTabsIndex = (path: string, tabList: TabItem[]) => { - return tabList.findIndex((item) => item.path === path) +const findTabsIndex = (fullPath: string, tabList: TabItem[]) => { + return tabList.findIndex((item) => item.fullPath === fullPath) +} + +const getComponentName = (route: RouteLocationNormalized) => { + return route.matched.at(-1)?.components?.default?.name } export const getRouteParams = (tabItem: TabItem) => { @@ -63,45 +68,67 @@ const useTabsStore = defineStore({ getters: { getTabList(): TabItem[] { return this.tabList + }, + getCacheTabList(): string[] { + return Array.from(this.cacheTabList) } }, actions: { setRouteName(name: RouteRecordName) { this.indexRouteName = name }, + addCache(componentName?: string) { + if (componentName) this.cacheTabList.add(componentName) + }, + removeCache(componentName?: string) { + if (componentName && this.cacheTabList.has(componentName)) { + this.cacheTabList.delete(componentName) + } + console.log(this.cacheTabList) + }, + clearCache() { + this.cacheTabList.clear() + }, resetState() { this.cacheTabList = new Set() this.tabList = [] this.tasMap = {} this.indexRouteName = '' }, - addTab(route: RouteLocationNormalized, router: Router) { - const { name, path, query, meta, params } = route + addTab(router: Router) { + const route = unref(router.currentRoute) + const { name, query, meta, params, fullPath, path } = route if (isCannotAddRoute(route, router)) return - const hasTabIndex = getHasTabIndex(path!, this.tabList) - + const hasTabIndex = getHasTabIndex(fullPath!, this.tabList) + const componentName = getComponentName(route) const tabItem = { name: name!, path, + fullPath, title: meta?.title, query, params } - this.tasMap[path] = tabItem + this.tasMap[fullPath] = tabItem + if (meta?.keepAlive) { + this.addCache(componentName) + } if (hasTabIndex != -1) { - this.tabList.splice(hasTabIndex, 1, tabItem) return } + this.tabList.push(tabItem) }, - removeTab(path: string, router: Router) { + removeTab(fullPath: string, router: Router) { const { currentRoute, push } = router - const index = findTabsIndex(path, this.tabList) + const index = findTabsIndex(fullPath, this.tabList) // 移除tab if (this.tabList.length > 1) { index !== -1 && this.tabList.splice(index, 1) } - if (path !== currentRoute.value.path) { + const componentName = getComponentName(currentRoute.value) + this.removeCache(componentName) + if (fullPath !== currentRoute.value.fullPath) { return } // 删除选中的tab @@ -116,17 +143,24 @@ const useTabsStore = defineStore({ const toRoute = getRouteParams(toTab) push(toRoute) }, - removeOtherTab(path: string) { - this.tabList = this.tabList.filter((item) => item.path == path) + removeOtherTab(route: RouteLocationNormalized) { + this.tabList = this.tabList.filter((item) => item.fullPath == route.fullPath) + const componentName = getComponentName(route) + this.cacheTabList.forEach((name) => { + if (componentName !== name) { + this.removeCache(name) + } + }) }, removeAllTab(router: Router) { const { push, currentRoute } = router - const { path, name } = unref(currentRoute) + const { name } = unref(currentRoute) if (name == this.indexRouteName) { - this.removeOtherTab(path) + this.removeOtherTab(currentRoute.value) return } this.tabList = [] + this.clearCache() push(PageEnum.INDEX) } } diff --git a/admin/src/stores/modules/user.ts b/admin/src/stores/modules/user.ts index f84da3f6..8ac6a42a 100644 --- a/admin/src/stores/modules/user.ts +++ b/admin/src/stores/modules/user.ts @@ -28,12 +28,6 @@ const useUserStore = defineStore({ }), getters: {}, actions: { - resetState() { - this.token = '' - this.userInfo = {} - this.perms = [] - this.menu = [] - }, login(playload: any) { const { account, password } = playload return new Promise((resolve, reject) => { @@ -54,8 +48,9 @@ const useUserStore = defineStore({ logout() { return new Promise((resolve, reject) => { logout() - .then((data) => { - router.push(PageEnum.LOGIN) + .then(async (data) => { + this.token = '' + await router.push(PageEnum.LOGIN) clearAuthInfo() resolve(data) }) diff --git a/admin/src/utils/auth.ts b/admin/src/utils/auth.ts index abc6f922..cbcc5194 100644 --- a/admin/src/utils/auth.ts +++ b/admin/src/utils/auth.ts @@ -11,8 +11,8 @@ export function getToken() { export function clearAuthInfo() { const userStore = useUserStore() const tabsStore = useTabsStore() - userStore.resetState() - tabsStore.resetState() + userStore.$reset() + tabsStore.$reset() cache.remove(TOKEN_KEY) resetRouter() } diff --git a/admin/src/views/article/column/index.vue b/admin/src/views/article/column/index.vue index e7d5cf0b..4346878e 100644 --- a/admin/src/views/article/column/index.vue +++ b/admin/src/views/article/column/index.vue @@ -65,7 +65,7 @@
- diff --git a/admin/src/views/channel/h5.vue b/admin/src/views/channel/h5.vue index 5c75fc3d..c884fea2 100644 --- a/admin/src/views/channel/h5.vue +++ b/admin/src/views/channel/h5.vue @@ -32,7 +32,7 @@ - diff --git a/admin/src/views/decoration/pages/index.vue b/admin/src/views/decoration/pages/index.vue index 20057e44..4de9db57 100644 --- a/admin/src/views/decoration/pages/index.vue +++ b/admin/src/views/decoration/pages/index.vue @@ -12,7 +12,7 @@ - diff --git a/admin/src/views/material/index.vue b/admin/src/views/material/index.vue index 975a338b..3d7f8d11 100644 --- a/admin/src/views/material/index.vue +++ b/admin/src/views/material/index.vue @@ -23,7 +23,7 @@ - diff --git a/admin/src/views/message/short_letter/index.vue b/admin/src/views/message/short_letter/index.vue index 8d890365..02726701 100644 --- a/admin/src/views/message/short_letter/index.vue +++ b/admin/src/views/message/short_letter/index.vue @@ -26,7 +26,7 @@ -