workbench.vue 13 KB

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