user.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * 登录用户 / 菜单 / 权限(对齐 EOM)
  3. */
  4. import { formatMenus, toTreeData, formatTreeData } from 'ele-admin';
  5. import { USER_MENUS, SYSTEM_NAME, HOME_PATH } from '@/config/setting';
  6. import { DEFAULT_MENUS } from '@/config/menus';
  7. import { getResourcesTree } from '@/api/layout';
  8. import { getCurrentUser } from '@/utils/token-util';
  9. import { rewriteLegacyHrMenus } from '@/utils/menu-rewrite';
  10. import Vue from 'vue';
  11. const formatRouter = (list = []) => {
  12. const authorities = [];
  13. const walk = (nodes) => {
  14. const arr = [];
  15. for (const item of nodes || []) {
  16. if (item.menuType === 2) {
  17. authorities.push(item);
  18. item.hide = true;
  19. }
  20. if (item.source === 2 && item.extend) {
  21. item.path = `${item.path}/${item.extend}`;
  22. }
  23. item.children = item.children?.length ? walk(item.children) : [];
  24. arr.push(item);
  25. }
  26. return arr;
  27. };
  28. return { menuList: walk(list), authorities };
  29. };
  30. function buildLocalMenus() {
  31. return formatMenus(JSON.parse(JSON.stringify(DEFAULT_MENUS)));
  32. }
  33. export default {
  34. namespaced: true,
  35. state: {
  36. info: null,
  37. menus: null,
  38. authorities: [],
  39. authoritiesRouter: [],
  40. roles: [],
  41. },
  42. mutations: {
  43. setUserInfo(state, info) {
  44. state.info = info;
  45. },
  46. setMenus(state, menus) {
  47. state.menus = menus == null ? menus : rewriteLegacyHrMenus(menus);
  48. },
  49. setAuthorities(state, authorities) {
  50. state.authorities = (authorities || []).map((item) =>
  51. typeof item === 'string' ? item : item.permissionCode,
  52. );
  53. },
  54. setAuthoritiesRouter(state, authoritiesRouter) {
  55. state.authoritiesRouter =
  56. authoritiesRouter == null
  57. ? authoritiesRouter
  58. : rewriteLegacyHrMenus(authoritiesRouter);
  59. },
  60. setRoles(state, roles) {
  61. state.roles = roles;
  62. },
  63. },
  64. actions: {
  65. /**
  66. * 与 EOM 一致:按 `/page-hr` 过滤资源树,只写入本子应用 store
  67. */
  68. async fetchUserInfo({ commit }) {
  69. const currentUser = getCurrentUser() || {};
  70. const result = await getResourcesTree({
  71. groupId: currentUser.currentGroupId,
  72. roleId: currentUser.currentRoleId,
  73. }).catch(() => {});
  74. const list = (Array.isArray(result) ? result : []).filter(
  75. (item) => item.path === `/page-${SYSTEM_NAME}`,
  76. );
  77. if (!list?.length) {
  78. if (window.__POWERED_BY_QIANKUN__) {
  79. return {};
  80. }
  81. const local = buildLocalMenus();
  82. const nextMenus = rewriteLegacyHrMenus(local.menus || []);
  83. commit('setMenus', nextMenus);
  84. commit('setAuthorities', []);
  85. commit('setAuthoritiesRouter', []);
  86. return {
  87. menus: nextMenus,
  88. homePath: HOME_PATH || local.homePath,
  89. authoritiesRouter: [],
  90. };
  91. }
  92. const { menuList, authorities } = formatRouter(list[0].children || []);
  93. commit('setAuthorities', authorities);
  94. const { menus, homePath } = formatMenus(
  95. USER_MENUS ??
  96. toTreeData({
  97. data: menuList,
  98. idField: 'id',
  99. parentIdField: 'parentId',
  100. }),
  101. );
  102. const { menus: authoritiesRouter } = formatMenus(
  103. USER_MENUS ??
  104. toTreeData({
  105. data: authorities.filter((item) => item.path),
  106. idField: 'id',
  107. parentIdField: 'parentId',
  108. }),
  109. );
  110. const nextMenus = rewriteLegacyHrMenus(menus || []);
  111. const nextAuth = rewriteLegacyHrMenus(authoritiesRouter || []);
  112. commit('setMenus', nextMenus);
  113. commit('setAuthoritiesRouter', nextAuth);
  114. return {
  115. menus: nextMenus,
  116. homePath: HOME_PATH || homePath,
  117. authoritiesRouter: nextAuth,
  118. };
  119. },
  120. setInfo({ commit }, value) {
  121. commit('setUserInfo', value);
  122. },
  123. setMenus({ commit }, value) {
  124. commit('setMenus', value);
  125. },
  126. setMenuBadge({ commit, state }, { path, value, color }) {
  127. if (window.__POWERED_BY_QIANKUN__) {
  128. Vue.prototype.$portalStore?.dispatch('user/setMenuBadge', {
  129. path: `/page-${SYSTEM_NAME}${path}`,
  130. value,
  131. color,
  132. });
  133. return;
  134. }
  135. const menus = formatTreeData(state.menus, (menu) => {
  136. if (path === menu.path) {
  137. return {
  138. ...menu,
  139. meta: {
  140. ...menu.meta,
  141. badge: value,
  142. badgeColor: color,
  143. },
  144. };
  145. }
  146. return menu;
  147. });
  148. commit('setMenus', menus);
  149. },
  150. },
  151. };