feat: 重构sidebar菜单块逻辑

This commit is contained in:
rd
2025-07-07 18:17:31 +08:00
parent 0fe45bb2b3
commit bd4c338f35
11 changed files with 163 additions and 122 deletions

View File

@ -7,8 +7,12 @@
<script setup lang="ts"> <script setup lang="ts">
import { useUserStore } from '@/stores'; import { useUserStore } from '@/stores';
import { useEnterpriseStore } from '@/stores/modules/enterprise';
import zhCN from '@arco-design/web-vue/es/locale/lang/zh-cn'; import zhCN from '@arco-design/web-vue/es/locale/lang/zh-cn';
const userStore = useUserStore(); const userStore = useUserStore();
const enterpriseStore = useEnterpriseStore();
const redTheme = { const redTheme = {
token: { token: {
@ -17,10 +21,13 @@ const redTheme = {
}, },
}; };
const init = () => { const init = async () => {
const { isLogin, fetchUserInfo } = userStore; const { isLogin, fetchUserInfo } = userStore;
const { updateEnterpriseInfo } = enterpriseStore;
if (isLogin) { if (isLogin) {
fetchUserInfo(); await fetchUserInfo();
await updateEnterpriseInfo();
} }
}; };

View File

@ -12,7 +12,7 @@ export default function useMenuTree() {
// const appStore = useAppStore(); // const appStore = useAppStore();
const sidebarStore = useSidebarStore(); const sidebarStore = useSidebarStore();
const appRoute = computed(() => { const appRoute = computed(() => {
const _filterRoutes = appRoutes.filter((v) => v.meta.id === sidebarStore.activeMenuId); const _filterRoutes = appRoutes.filter((v) => v.meta?.id === sidebarStore.activeMenuId);
return _filterRoutes; return _filterRoutes;
}); });
const menuTree = computed(() => { const menuTree = computed(() => {

View File

@ -1,88 +1,43 @@
<script lang="ts" setup> <script setup>
import { useAppStore } from '@/stores'; // import { useAppStore } from '@/stores';
import { IconExport, IconFile, IconCaretDown } from '@arco-design/web-vue/es/icon'; // import { useEnterpriseStore } from '@/stores/modules/enterprise';
import { fetchMenusTree } from '@/api/all'; // import { IconExport, IconFile, IconCaretDown } from '@arco-design/web-vue/es/icon';
import { handleUserLogout } from '@/utils/user'; // import { handleUserLogout } from '@/utils/user';
import { fetchLogOut } from '@/api/all/login'; // import { fetchLogOut } from '@/api/all/login';
import { useSidebarStore } from '@/stores/modules/side-bar'; import { useSidebarStore } from '@/stores/modules/side-bar';
import { MENU_GROUP_IDS } from '@/router/constants'; // import { MENU_GROUP_IDS } from '@/router/constants';
import router from '@/router'; import router from '@/router';
import { useRoute } from 'vue-router'; // import { useRoute } from 'vue-router';
import ExitAccountModal from '@/components/_base/exit-account-modal/index.vue'; import ExitAccountModal from '@/components/_base/exit-account-modal/index.vue';
import { appRoutes } from '@/router/routes'; // import { appRoutes } from '@/router/routes';
// import { MENU_LIST } from './constants';
interface MenuItem {
name: string;
id: number;
children: Array<{
name: string;
}>;
}
const lists = ref<MenuItem[]>([]);
const sidebarStore = useSidebarStore(); const sidebarStore = useSidebarStore();
const route = useRoute(); // const enterpriseStore = useEnterpriseStore();
// const route = useRoute();
const exitAccountModalRef = ref(null); const exitAccountModalRef = ref(null);
// const selectedKey = ref([]);
// const enterpriseInfo = enterpriseStore.getEnterpriseInfo();
const selectedKey = computed(() => { const selectedKey = computed(() => {
// 判断是否为工作台页面(假设路由名为 'Home' 或 path 为 '/'
if (route.name === 'Home' || route.path === '/') {
return [`${MENU_GROUP_IDS.WORK_BENCH_ID}`];
}
// 其他页面activeMenuId 作为 key
return [String(sidebarStore.activeMenuId)]; return [String(sidebarStore.activeMenuId)];
}); });
const menuList = computed(() => {
return sidebarStore.menuList;
});
const clickExit = async () => { const clickExit = async () => {
exitAccountModalRef.value?.open(); exitAccountModalRef.value?.open();
}; };
const getMenus = async () => {
const res = await fetchMenusTree(); // const appStore = useAppStore();
if (res.code === 200) {
lists.value = res.data;
}
};
onMounted(() => {
getMenus();
});
const appStore = useAppStore();
const setServerMenu = () => { const setServerMenu = () => {
router.push('/management/person'); router.push('/management/person');
}; };
const handleSelect = (index: any) => {
if (index === 0) {
router.push('/');
} else {
router.push('/dataEngine/hotTranslation');
}
};
const flattenRoutes = (routes: any, parentPath = ''): any[] => { const handleDopdownClick = (item) => {
let result: any[] = []; router.push({ name: item.pathName });
for (const route of routes) {
const fullPath = route.path.startsWith('/') ? route.path : parentPath.replace(/\/$/, '') + '/' + route.path;
if (route.children && route.children.length) {
result = result.concat(flattenRoutes(route.children, fullPath));
} else {
result.push({ ...route, fullPath });
}
}
return result;
};
const handleDopdownClick = (index: any, ind: any) => {
const { children } = lists.value[index];
const indPath = children[ind];
const allChildren = flattenRoutes(appRoutes);
const target = allChildren.find((item) => item.meta && item.meta.menuId === indPath.id);
if (target) {
router.push(target.fullPath);
} else {
console.warn('未找到对应的菜单路由', indPath.id);
}
}; };
</script> </script>
@ -95,26 +50,26 @@ const handleDopdownClick = (index: any, ind: any) => {
</div> </div>
<div class="center-side"> <div class="center-side">
<div class="menu-demo h-100%"> <div class="menu-demo h-100%">
<a-menu <a-menu mode="horizontal" :selected-keys="selectedKey">
mode="horizontal" <a-menu-item v-for="item in menuList" :key="String(item.id)">
:selected-keys="selectedKey" <template v-if="item.children">
:default-selected-keys="[`${MENU_GROUP_IDS.WORK_BENCH_ID}`]" <a-dropdown :popup-max-height="false" class="layout-menu-item-dropdown">
> <a-button>
<a-menu-item :key="`${MENU_GROUP_IDS.WORK_BENCH_ID}`" @click="handleSelect(0)"> <span class="menu-item-text mr-2px"> {{ item.name }}</span>
<span class="menu-item-text">工作台</span> <icon-caret-down size="16" class="arco-icon-down !mr-0" />
</a-menu-item> </a-button>
<a-menu-item v-for="(item, index) in lists" :key="String(item.id)"> <template #content>
<a-dropdown :popup-max-height="false" class="layout-menu-item-dropdown"> <a-doption v-for="(child, ind) in item.children" :key="ind" @click="handleDopdownClick(child)">
<a-button> <span class="menu-item-text"> {{ child.name }}</span>
<span class="menu-item-text mr-2px"> {{ item.name }}</span> </a-doption>
<icon-caret-down size="16" class="arco-icon-down !mr-0" /> </template>
</a-button> </a-dropdown>
<template #content> </template>
<a-doption v-for="(child, ind) in item.children" :key="ind" @click="handleDopdownClick(index, ind)"> <template v-else>
<span class="menu-item-text"> {{ child.name }}</span> <a-menu-item :key="String(item.id)" @click="handleDopdownClick(item)">
</a-doption> <span class="menu-item-text"> {{ item.name }}</span>
</template> </a-menu-item>
</a-dropdown> </template>
</a-menu-item> </a-menu-item>
</a-menu> </a-menu>
</div> </div>

View File

@ -7,12 +7,12 @@ import { appRoutes } from './routes';
import { REDIRECT_MAIN, NOT_FOUND_ROUTE } from './routes/base'; import { REDIRECT_MAIN, NOT_FOUND_ROUTE } from './routes/base';
import NProgress from 'nprogress'; import NProgress from 'nprogress';
import 'nprogress/nprogress.css'; import 'nprogress/nprogress.css';
import { MENU_GROUP_IDS } from './constants';
import createRouteGuard from './guard'; import createRouteGuard from './guard';
NProgress.configure({ showSpinner: false }); // NProgress Configuration NProgress.configure({ showSpinner: false }); // NProgress Configuration
// console.log({ appRoutes });
const router = createRouter({ export const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL), history: createWebHistory(import.meta.env.BASE_URL),
routes: [ routes: [
{ {
@ -30,6 +30,7 @@ const router = createRouter({
meta: { meta: {
hideSidebar: true, hideSidebar: true,
requiresAuth: true, requiresAuth: true,
id: MENU_GROUP_IDS.WORK_BENCH_ID,
}, },
}, },
{ {

View File

@ -1,4 +1,6 @@
import type { RouteRecordNormalized } from 'vue-router'; import type { RouteRecordNormalized } from 'vue-router';
import { REDIRECT_MAIN, NOT_FOUND_ROUTE } from './base';
import { MENU_GROUP_IDS } from '@/router/constants';
const modules = import.meta.glob('./modules/*.ts', { eager: true }); const modules = import.meta.glob('./modules/*.ts', { eager: true });
// const externalModules = import.meta.glob('./externalModules/*.ts', { // const externalModules = import.meta.glob('./externalModules/*.ts', {
@ -15,6 +17,6 @@ function formatModules(_modules: any, result: RouteRecordNormalized[]) {
return result; return result;
} }
export const appRoutes: RouteRecordNormalized[] = formatModules(modules, []); export const appRoutes: any[] = formatModules(modules, []);
// export const appExternalRoutes: RouteRecordNormalized[] = formatModules(externalModules, []); // export const appExternalRoutes: RouteRecordNormalized[] = formatModules(externalModules, []);

View File

@ -27,7 +27,6 @@ const COMPONENTS: AppRouteRecordRaw[] = [
locale: '行业热门话题洞察', locale: '行业热门话题洞察',
requiresAuth: true, requiresAuth: true,
roles: ['*'], roles: ['*'],
menuId: 2,
}, },
component: () => import('@/views/components/dataEngine/hotTranslation.vue'), component: () => import('@/views/components/dataEngine/hotTranslation.vue'),
}, },
@ -38,7 +37,6 @@ const COMPONENTS: AppRouteRecordRaw[] = [
locale: '行业词云', locale: '行业词云',
requiresAuth: true, requiresAuth: true,
roles: ['*'], roles: ['*'],
menuId: 3,
}, },
component: () => import('@/views/components/dataEngine/hotCloud.vue'), component: () => import('@/views/components/dataEngine/hotCloud.vue'),
}, },
@ -49,7 +47,6 @@ const COMPONENTS: AppRouteRecordRaw[] = [
locale: '行业关键词动向', locale: '行业关键词动向',
requiresAuth: true, requiresAuth: true,
roles: ['*'], roles: ['*'],
menuId: 4,
}, },
component: () => import('@/views/components/dataEngine/keyWord.vue'), component: () => import('@/views/components/dataEngine/keyWord.vue'),
}, },
@ -60,7 +57,6 @@ const COMPONENTS: AppRouteRecordRaw[] = [
locale: '用户痛点观察', locale: '用户痛点观察',
requiresAuth: true, requiresAuth: true,
roles: ['*'], roles: ['*'],
menuId: 5,
}, },
component: () => import('@/views/components/dataEngine/userPainPoints.vue'), component: () => import('@/views/components/dataEngine/userPainPoints.vue'),
}, },
@ -71,7 +67,6 @@ const COMPONENTS: AppRouteRecordRaw[] = [
locale: '重点品牌动向', locale: '重点品牌动向',
requiresAuth: true, requiresAuth: true,
roles: ['*'], roles: ['*'],
menuId: 6,
}, },
component: () => import('@/views/components/dataEngine/keyBrandMovement.vue'), component: () => import('@/views/components/dataEngine/keyBrandMovement.vue'),
}, },
@ -82,7 +77,6 @@ const COMPONENTS: AppRouteRecordRaw[] = [
locale: '用户画像', locale: '用户画像',
requiresAuth: true, requiresAuth: true,
roles: ['*'], roles: ['*'],
menuId: 7,
}, },
component: () => import('@/views/components/dataEngine/userPersona.vue'), component: () => import('@/views/components/dataEngine/userPersona.vue'),
}, },

View File

@ -31,7 +31,6 @@ const COMPONENTS: AppRouteRecordRaw[] = [
locale: '品牌信息', locale: '品牌信息',
requiresAuth: true, requiresAuth: true,
roles: ['*'], roles: ['*'],
menuId: 11,
}, },
component: () => import('@/views/property-marketing/brands/brand-materials/index.vue'), component: () => import('@/views/property-marketing/brands/brand-materials/index.vue'),
}, },
@ -57,7 +56,6 @@ const COMPONENTS: AppRouteRecordRaw[] = [
locale: '账号管理', locale: '账号管理',
requiresAuth: true, requiresAuth: true,
roles: ['*'], roles: ['*'],
menuId: 12,
}, },
component: () => import('@/views/property-marketing/media-account/account-manage'), component: () => import('@/views/property-marketing/media-account/account-manage'),
}, },
@ -105,7 +103,6 @@ const COMPONENTS: AppRouteRecordRaw[] = [
locale: '账户管理', locale: '账户管理',
requiresAuth: true, requiresAuth: true,
roles: ['*'], roles: ['*'],
menuId: 13,
}, },
component: () => import('@/views/property-marketing/put-account/account-manage'), component: () => import('@/views/property-marketing/put-account/account-manage'),
}, },
@ -161,7 +158,6 @@ const COMPONENTS: AppRouteRecordRaw[] = [
locale: '业务洞察报告', locale: '业务洞察报告',
requiresAuth: true, requiresAuth: true,
roles: ['*'], roles: ['*'],
menuId: 14,
}, },
component: () => import('@/views/property-marketing/intelligent-solution/businessAnalysisReport'), component: () => import('@/views/property-marketing/intelligent-solution/businessAnalysisReport'),
}, },

View File

@ -1,4 +1,5 @@
import { fetchEnterpriseInfo } from '@/api/all/login'; import { fetchEnterpriseInfo } from '@/api/all/login';
import { useSidebarStore } from '@/stores/modules/side-bar';
interface EnterpriseInfo { interface EnterpriseInfo {
id: number; id: number;
@ -7,6 +8,7 @@ interface EnterpriseInfo {
used_update_name_count: number; used_update_name_count: number;
sub_account_quota: number; sub_account_quota: number;
used_sub_account_count: number; used_sub_account_count: number;
permissions: string[];
} }
interface EnterpriseState { interface EnterpriseState {
@ -55,10 +57,13 @@ export const useEnterpriseStore = defineStore('enterprise', {
return this.enterpriseInfo; return this.enterpriseInfo;
}, },
async updateEnterpriseInfo() { async updateEnterpriseInfo() {
const sidebarStore = useSidebarStore();
const res = await fetchEnterpriseInfo(this.enterpriseInfo!.id); const res = await fetchEnterpriseInfo(this.enterpriseInfo!.id);
const { code, data } = res; const { code, data } = res;
if (code === 200) { if (code === 200) {
this.setEnterpriseInfo(data); this.setEnterpriseInfo(data);
sidebarStore.getNavbarMenuList();
} }
}, },
}, },

View File

@ -0,0 +1,63 @@
import { MENU_GROUP_IDS } from '@/router/constants';
export const MENU_LIST = [
{
id: MENU_GROUP_IDS.WORK_BENCH_ID,
name: '工作台',
pathName: 'Home',
permissionKey: '', // 权限key如果为空则表示该菜单不需要权限与后端约定
},
{
id: MENU_GROUP_IDS.DATA_ENGINE_ID,
name: '全域数据分析',
permissionKey: 'data_analysis',
children: [
{
name: '行业热门话题洞察',
pathName: 'DataEngineHotTranslation',
},
{
name: '行业词云',
pathName: 'DataEngineHotCloud',
},
{
name: '行业关键词动向',
pathName: 'DataEngineKeyWord',
},
{
name: '用户痛点观察',
pathName: 'DataEngineUserPainPoints',
},
{
name: '重点品牌动向',
pathName: 'DataEngineKeyBrandMovement',
},
{
name: '用户画像',
pathName: 'DataEngineUserPersona',
},
],
},
{
id: MENU_GROUP_IDS.PROPERTY_ID,
name: '营销资产中台',
permissionKey: 'marketing_asset',
children: [
{
name: '品牌资产管理',
pathName: 'RepositoryBrandMaterials',
},
{
name: '账号资源中心',
pathName: 'MediaAccountAccountManagement',
},
{
name: '投放资源中心',
pathName: 'PutAccountAccountManagement',
},
{
name: '智能方案管理',
pathName: 'IntelligentSolutionBusinessAnalysisReport',
},
],
},
];

View File

@ -3,19 +3,20 @@
* @Date: 2025-06-23 22:13:30 * @Date: 2025-06-23 22:13:30
*/ */
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { MENU_GROUP_IDS } from '@/router/constants'; import router from '@/router';
import { appRoutes } from '@/router/routes';
import type { RouteLocationNormalized } from 'vue-router'; import type { RouteLocationNormalized } from 'vue-router';
import { MENU_LIST } from './constants';
const { DATA_ENGINE_ID, MANAGEMENT_ID } = MENU_GROUP_IDS; import { useEnterpriseStore } from '@/stores/modules/enterprise';
interface sidebarState { interface sidebarState {
activeMenuId: number | null; activeMenuId: number | null;
menuList: any[];
} }
export const useSidebarStore = defineStore('sidebar', { export const useSidebarStore = defineStore('sidebar', {
state: (): sidebarState => ({ state: (): sidebarState => ({
activeMenuId: null, activeMenuId: null,
menuList: [],
}), }),
actions: { actions: {
clearActiveMenuId() { clearActiveMenuId() {
@ -24,14 +25,23 @@ export const useSidebarStore = defineStore('sidebar', {
setActiveMenuId(id: number) { setActiveMenuId(id: number) {
this.activeMenuId = id; this.activeMenuId = id;
}, },
// navbar菜单列表由企业对应权限决定
getNavbarMenuList() {
const enterpriseStore = useEnterpriseStore();
const enterpriseInfo = enterpriseStore.getEnterpriseInfo();
this.menuList = MENU_LIST.filter(
(item) => !item.permissionKey || enterpriseInfo?.permissions?.includes(item.permissionKey),
);
},
// 根据当前路由自动设置 activeMenuId // 根据当前路由自动设置 activeMenuId
setActiveMenuIdByRoute(route: RouteLocationNormalized) { setActiveMenuIdByRoute(route: RouteLocationNormalized) {
// console.log('setActiveMenuIdByRoute '); const appRoutes = router.options?.routes ?? [];
// 查找当前路由所属的菜单组 // 查找当前路由所属的菜单组
const findMenuGroup = (routes: any[]): number | null => { const findMenuGroup = (routes: any[]): number | null => {
for (const routeItem of routes) { for (const routeItem of routes) {
// 检查子路由 // 检查子路由
if (routeItem.children && routeItem.children.length > 0) { if (routeItem.children?.length > 0) {
// 检查当前路由是否是这个父路由的子路由 // 检查当前路由是否是这个父路由的子路由
const isChildRoute = routeItem.children.some((child: any) => child.name === route.name); const isChildRoute = routeItem.children.some((child: any) => child.name === route.name);
if (isChildRoute) { if (isChildRoute) {
@ -42,6 +52,10 @@ export const useSidebarStore = defineStore('sidebar', {
if (childResult !== null) { if (childResult !== null) {
return routeItem.meta?.id || childResult; return routeItem.meta?.id || childResult;
} }
} else {
if (routeItem.name === route.name) {
return routeItem.meta?.id || null;
}
} }
} }
return null; return null;

View File

@ -26,7 +26,7 @@
v-if="props.product.status === Status.Enable || props.product.status === Status.ON_TRIAL" v-if="props.product.status === Status.Enable || props.product.status === Status.ON_TRIAL"
class="primary-button" class="primary-button"
type="primary" type="primary"
@click="gotoModule(props.product.menu_id)" @click="gotoModule(props.product.id)"
> >
进入模块 进入模块
</a-button> </a-button>
@ -71,12 +71,12 @@ import { useRouter } from 'vue-router';
import CustomerServiceModal from '@/components/customer-service-modal.vue'; import CustomerServiceModal from '@/components/customer-service-modal.vue';
import { appRoutes } from '@/router/routes'; import { appRoutes } from '@/router/routes';
import { useSidebarStore } from '@/stores/modules/side-bar'; import { useSidebarStore } from '@/stores/modules/side-bar';
import { useEnterpriseStore } from '@/stores/modules/enterprise';
const props = defineProps<{ const props = defineProps<{
product: Product; product: Product;
}>(); }>();
const emit = defineEmits(['refresh']); const emit = defineEmits(['refresh']);
const sidebarStore = useSidebarStore();
enum Status { enum Status {
Disable = 0, // 禁用 Disable = 0, // 禁用
@ -87,30 +87,34 @@ enum Status {
} }
interface Product { interface Product {
id: number;
status: Status; status: Status;
name: string; name: string;
image: string; image: string;
desc: string; desc: string;
menu_id: number; id: number;
expired_at?: number; expired_at?: number;
} }
const visible = ref(false); const visible = ref(false);
const router = useRouter(); const router = useRouter();
const enterpriseStore = useEnterpriseStore();
const handleTrial = async (id: any) => { const handleTrial = async (id: any) => {
await trialProduct(id); const { code } = await trialProduct(id);
AMessage.success('试用成功!'); if (code === 200) {
emit('refresh'); enterpriseStore.updateEnterpriseInfo();
AMessage.success('试用成功!');
emit('refresh');
}
}; };
const gotoModule = (menuId: number) => { const gotoModule = (menuId: number) => {
const _target = appRoutes.find((v) => v.meta.id === menuId); const routeMap: Record<number, string> = {
if (_target) { '1': 'DataEngineHotTranslation',
console.log({ _target }); '2': 'RepositoryBrandMaterials',
router.push({ name: _target.name }); };
} console.log(routeMap[menuId]);
router.push({ name: routeMap[menuId] });
}; };
</script> </script>
<style scoped lang="less"> <style scoped lang="less">