workbench.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <template>
  2. <div class="workbench-bos" :class="isOpen ? 'workbench-bos1' : 'workbench-bos2'">
  3. <!-- 展开状态 -->
  4. <div class="workbench-box1" v-if="isOpen">
  5. <div class="workbench-expand1 flex-row-start" @click="onOpen">
  6. <img src="@/assets/images/layout/workbench.png" alt="" />
  7. <div>收起菜单</div>
  8. </div>
  9. <!-- 修改点1: 增加 v-if="item1.show" 过滤无权限菜单 -->
  10. <template v-for="(item1, index1) in menuList" :key="index1">
  11. <div class="menu-list1" v-if="item1.show" @click="toggleMenu(item1.path)">
  12. <div class="menu-head1 flex-row-between">
  13. <div class="menu-title1 flex-row-start">
  14. <img :src="item1.icon" alt="" />
  15. <div>{{ item1.title }}</div>
  16. </div>
  17. <el-icon v-if="openedMenus.includes(item1.path)" size="14">
  18. <ArrowDown />
  19. </el-icon>
  20. <el-icon v-else size="14">
  21. <ArrowRight />
  22. </el-icon>
  23. </div>
  24. <div class="menu-item" v-show="openedMenus.includes(item1.path)">
  25. <template v-for="(item2, index2) in item1.children" :key="index2">
  26. <div @click.stop="onPath(item2.path)" class="menu-box" :class="{ 'menu-hig': activeMenu == item2.path }">
  27. {{ item2.title }}
  28. </div>
  29. </template>
  30. </div>
  31. </div>
  32. </template>
  33. </div>
  34. <!-- 收起状态 -->
  35. <div v-else class="workbench-box2">
  36. <div class="workbench-expand2 flex-row-center" @click="onOpen">
  37. <div class="menu-icon flex-row-center">
  38. <img src="@/assets/images/layout/workbench.png" alt="" />
  39. </div>
  40. </div>
  41. <!-- 修改点2: 增加 v-if="item1.show" 过滤无权限菜单 -->
  42. <template v-for="(item1, index1) in menuList" :key="index1">
  43. <div class="workbench-expand2 flex-row-center" v-if="item1.show">
  44. <el-popover placement="right">
  45. <template #reference>
  46. <div class="menu-icon flex-row-center" :class="{ 'hig': openedMenus.includes(item1.path) }">
  47. <img :src="item1.icon" alt="" />
  48. </div>
  49. </template>
  50. <div class="popover-bos">
  51. <div class="popover-title">{{ item1.title }}</div>
  52. <template v-for="(item2, index2) in item1.children" :key="index2">
  53. <div class="popover-list" :class="{ 'popover-hig': activeMenu == item2.path }"
  54. @click="onPath(item2.path)">
  55. {{ item2.title }}
  56. </div>
  57. </template>
  58. </div>
  59. </el-popover>
  60. </div>
  61. </template>
  62. </div>
  63. </div>
  64. </template>
  65. <script setup lang="ts">
  66. import workbench1 from '@/assets/images/layout/workbench1.png';
  67. import workbench2 from '@/assets/images/layout/workbench2.png';
  68. import workbench3 from '@/assets/images/layout/workbench3.png';
  69. import workbench4 from '@/assets/images/layout/workbench4.png';
  70. import workbench5 from '@/assets/images/layout/workbench5.png';
  71. import workbench6 from '@/assets/images/layout/workbench6.png';
  72. import workbench7 from '@/assets/images/layout/workbench7.png';
  73. import { getWorkbenchMenuList } from '@/api/pc/system/index';
  74. import { onPath } from '@/utils/siteConfig';
  75. import { useRouter } from 'vue-router';
  76. import { getInfo } from '@/api/login';
  77. import { el } from 'element-plus/es/locale/index.mjs';
  78. const router = useRouter();
  79. const isOpen = ref(false);
  80. const openedMenus = ref<string[]>([]);
  81. const route = useRoute();
  82. const menuIndex = ref<any>(0);
  83. interface MenuItemChild {
  84. path: string;
  85. title: string;
  86. }
  87. interface MenuItem {
  88. path: string;
  89. title: string;
  90. icon: string;
  91. children?: MenuItemChild[];
  92. show?: boolean;
  93. }
  94. // 注意:这里定义的是完整的全量菜单结构,后续会根据权限动态修改 children 和 show
  95. const menuLists = ref<any>([]);
  96. const menuList = ref<MenuItem[]>([
  97. {
  98. path: '/enterprise',
  99. title: '企业账户',
  100. icon: workbench1,
  101. show: false,
  102. children: [
  103. { path: '/enterprise/companyInfo', title: '企业信息' },
  104. { path: '/enterprise/messageNotice', title: '消息通知' }, // 接口没返这个,应该被过滤
  105. { path: '/easybuv', title: '地址管理' },
  106. { path: '/enterprise/invoiceManage', title: '发票抬头管理' },
  107. { path: '/enterprise/purchasePlan', title: '专属采购方案' },
  108. { path: '/enterprise/agreementSupply', title: '协议供货' },
  109. { path: '/enterprise/myCollection', title: '我的收藏' },
  110. { path: '/enterprise/purchaseHistory', title: '历史购买' },
  111. { path: '/enterprise/myFootprint', title: '我的足迹' }
  112. ]
  113. },
  114. {
  115. path: '/order',
  116. title: '交易管理',
  117. icon: workbench2,
  118. show: false,
  119. children: [
  120. { path: '/order/orderManage', title: '订单管理' },
  121. { path: '/order/orderAudit', title: '审核订单' },
  122. { path: '/order/afterSale', title: '售后服务' },
  123. { path: '/order/batchOrder', title: '批量下单' },
  124. { path: '/order/orderEvaluation', title: '订单评价' }
  125. ]
  126. },
  127. {
  128. path: '/organization',
  129. title: '组织管理',
  130. icon: workbench3,
  131. show: false,
  132. children: [
  133. { path: '/i', title: '个人信息' },
  134. { path: '/organization/deptManage', title: '部门管理' },
  135. { path: '/organization/staffManage', title: '人员管理' },
  136. { path: '/organization/roleManage', title: '角色管理' },
  137. { path: '/organization/approvalFlow', title: '审批流程' },
  138. { path: '/organization/groupEnterprise', title: '集团关联企业' }
  139. ]
  140. },
  141. {
  142. path: '/cost',
  143. title: '成本管理',
  144. icon: workbench4,
  145. show: false,
  146. children: [
  147. { path: '/cost/itemExpense', title: '分项费用' },
  148. { path: '/cost/quotaControl', title: '额度控制' }
  149. ]
  150. },
  151. {
  152. path: '/reconciliation',
  153. title: '对账管理',
  154. icon: workbench5,
  155. show: false,
  156. children: [
  157. { path: '/reconciliation/billManage', title: '对账单管理' },
  158. { path: '/reconciliation/invoiceManage', title: '开票管理' }
  159. ]
  160. },
  161. {
  162. path: '/valueAdded',
  163. title: '增值服务',
  164. icon: workbench6,
  165. show: false,
  166. children: [
  167. { path: '/valueAdded/maintenance', title: '维保服务' },
  168. { path: '/valueAdded/complaint', title: '投诉与建议' }
  169. ]
  170. },
  171. {
  172. path: '/analysis',
  173. title: '采购分析',
  174. icon: workbench7,
  175. show: false,
  176. children: [
  177. { path: '/analysis/orderAnalysis', title: '订单交易分析' },
  178. { path: '/analysis/purchaseDetail', title: '商品采购明细' },
  179. { path: '/analysis/orderStatus', title: '订单执行状态' },
  180. { path: '/analysis/settlementStatus', title: '对账结算状况' },
  181. { path: '/analysis/deptPurchase', title: '部门采购金额' }
  182. ]
  183. }
  184. ]);
  185. const activeMenu = computed(() => route.path);
  186. const userInfo = ref<any>({});
  187. const onOpen = () => {
  188. isOpen.value = !isOpen.value;
  189. };
  190. const allowedPaths = ref<Set<string>>(new Set());
  191. const processMenuPermissions = (apiMenuList: any[]) => {
  192. // 1. 收集接口返回的所有合法 path
  193. const paths = new Set<string>();
  194. const traverse = (items: any[]) => {
  195. items.forEach((item) => {
  196. // 统一格式:确保以 / 开头
  197. const p = item.path.startsWith('/') ? item.path : `/${item.path}`;
  198. paths.add(p);
  199. if (item.children && item.children.length > 0) {
  200. traverse(item.children);
  201. }
  202. });
  203. };
  204. traverse(apiMenuList);
  205. allowedPaths.value = paths;
  206. // 2. 更新本地 menuList:过滤子菜单并设置父级显示状态
  207. menuList.value.forEach((menu) => {
  208. if (!menu.children) {
  209. menu.show = false;
  210. return;
  211. }
  212. // 【核心修改】过滤出有权限的子菜单
  213. const permittedChildren = menu.children.filter((child) => {
  214. const childPath = child.path.startsWith('/') ? child.path : `/${child.path}`;
  215. return paths.has(childPath);
  216. });
  217. // 将过滤后的子菜单重新赋值给 menu.children
  218. // 这样模板里 v-for 循环时就只有有权限的子项了
  219. menu.children = permittedChildren;
  220. // 如果过滤后还有子菜单,则显示父级;否则隐藏父级
  221. menu.show = permittedChildren.length > 0;
  222. });
  223. checkCurrentRoutePermission();
  224. // 权限处理完后,重新计算展开状态
  225. initOpenedMenus();
  226. };
  227. const checkCurrentRoutePermission = () => {
  228. const currentPath = route.path;
  229. const publicPaths = ['/', '/login', '/404'];
  230. if (!publicPaths.includes(currentPath) && !allowedPaths.value.has(currentPath) && menuLists.value.includes(currentPath)) {
  231. onPath('/');
  232. }
  233. };
  234. onMounted(() => {
  235. if (window.innerWidth > 1420) {
  236. isOpen.value = true;
  237. } else {
  238. isOpen.value = false;
  239. }
  240. window.addEventListener('resize', handleResize);
  241. menuList.value.forEach((item1: any) => {
  242. item1.children.forEach((item2: any) => {
  243. menuLists.value.push(item2.path);
  244. });
  245. });
  246. getInfo().then((res1) => {
  247. if (res1.code == 200) {
  248. userInfo.value = res1.data;
  249. if (res1.data && res1.data.user?.userSonType == 4) {
  250. menuList.value.forEach((item1: any) => {
  251. if (item1.path == '/enterprise') {
  252. item1.show = false;
  253. } else {
  254. item1.show = true;
  255. }
  256. item1.children.forEach((item2: any) => {
  257. item2.show = true;
  258. });
  259. });
  260. initOpenedMenus();
  261. } else {
  262. getWorkbenchMenuList().then((res: any) => {
  263. const apiData = res.data || res;
  264. if (Array.isArray(apiData)) {
  265. processMenuPermissions(apiData);
  266. }
  267. });
  268. }
  269. }
  270. });
  271. });
  272. const handleResize = () => {
  273. if (window.innerWidth > 1420) {
  274. isOpen.value = true;
  275. } else {
  276. isOpen.value = false;
  277. }
  278. };
  279. const initOpenedMenus = () => {
  280. const currentPath = route.path;
  281. openedMenus.value = [];
  282. for (const menu of menuList.value) {
  283. if (!menu.show) continue;
  284. // 此时 menu.children 已经是过滤后的干净数据
  285. const hasActiveChild = menu.children?.some((child) => {
  286. const childPath = child.path.startsWith('/') ? child.path : `/${child.path}`;
  287. return currentPath === childPath || currentPath.startsWith(childPath + '/');
  288. });
  289. if (hasActiveChild) {
  290. openedMenus.value = [menu.path];
  291. return;
  292. }
  293. }
  294. };
  295. const toggleMenu = (path: string) => {
  296. const index = openedMenus.value.indexOf(path);
  297. if (index > -1) {
  298. openedMenus.value.splice(index, 1);
  299. } else {
  300. openedMenus.value = [path];
  301. }
  302. };
  303. watch(
  304. () => route.path,
  305. () => {
  306. initOpenedMenus();
  307. if (userInfo.value && userInfo.value.user.userSonType != 4) {
  308. checkCurrentRoutePermission();
  309. }
  310. }
  311. );
  312. </script>
  313. <style lang="scss" scoped>
  314. /* 样式保持不变 */
  315. .workbench-bos {
  316. // position: absolute;
  317. // top: 0;
  318. //展开的
  319. .workbench-box1 {
  320. background: #ffffff;
  321. border-radius: 10px;
  322. padding: 10px 8px;
  323. width: 180px;
  324. .workbench-expand1 {
  325. height: 42px;
  326. width: 100%;
  327. font-size: 14px;
  328. color: #1d2129;
  329. padding-left: 12px;
  330. cursor: pointer;
  331. &:hover {
  332. color: #e60012;
  333. }
  334. img {
  335. height: 16px;
  336. width: 16px;
  337. margin-right: 8px;
  338. }
  339. }
  340. .menu-list1 {
  341. width: calc(100% - 24px);
  342. border-bottom: 1px #e5e6eb solid;
  343. margin: 0 12px;
  344. &:nth-last-child(1) {
  345. border-bottom: none;
  346. }
  347. .menu-head1 {
  348. height: 54px;
  349. width: 100%;
  350. color: #1d2129;
  351. cursor: pointer;
  352. &:hover {
  353. color: #e60012;
  354. }
  355. .menu-title1 {
  356. font-size: 14px;
  357. img {
  358. height: 16px;
  359. width: 16px;
  360. margin-right: 8px;
  361. }
  362. }
  363. }
  364. .menu-item {
  365. padding-bottom: 5px;
  366. .menu-box {
  367. width: 100%;
  368. height: 36px;
  369. border-radius: 3px;
  370. font-size: 13px;
  371. color: #4e5969;
  372. padding-left: 12px;
  373. line-height: 36px;
  374. cursor: pointer;
  375. &.menu-hig {
  376. background: #f7f8fa;
  377. color: #e60012;
  378. }
  379. &:hover {
  380. color: #e60012;
  381. }
  382. }
  383. }
  384. }
  385. }
  386. //收起的
  387. .workbench-box2 {
  388. background: #ffffff;
  389. border-radius: 10px;
  390. padding: 10px 8px;
  391. width: 50px;
  392. .workbench-expand2 {
  393. width: 34px;
  394. height: 36px;
  395. cursor: pointer;
  396. position: relative;
  397. .menu-icon {
  398. width: 24px;
  399. height: 24px;
  400. &.hig {
  401. background-color: #ffe8e8;
  402. }
  403. img {
  404. height: 16px;
  405. width: 16px;
  406. }
  407. }
  408. .menu-open {
  409. position: absolute;
  410. right: -168px;
  411. top: 0;
  412. width: 148px;
  413. background-color: #ffffff;
  414. z-index: 99;
  415. padding: 10px 18px;
  416. border-radius: 10px;
  417. }
  418. }
  419. }
  420. &.workbench-bos1 {
  421. left: -200px;
  422. }
  423. &.workbench-bos2 {
  424. left: -70px;
  425. }
  426. }
  427. .popover-bos {
  428. .popover-title {
  429. font-size: 12px;
  430. color: #9095a0;
  431. margin-bottom: 7px;
  432. padding-left: 4px;
  433. }
  434. .popover-list {
  435. width: 120px;
  436. height: 32px;
  437. font-size: 14px;
  438. color: #1c2c49;
  439. line-height: 32px;
  440. padding-left: 4px;
  441. cursor: pointer;
  442. &:hover {
  443. color: var(--el-color-primary);
  444. }
  445. &.popover-hig {
  446. background: #ffe8e8;
  447. }
  448. }
  449. }
  450. </style>