workbench.vue 13 KB

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