| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /**
- * 登录用户 / 菜单 / 权限(对齐 EOM)
- */
- import { formatMenus, toTreeData, formatTreeData } from 'ele-admin';
- import { USER_MENUS, SYSTEM_NAME, HOME_PATH } from '@/config/setting';
- import { DEFAULT_MENUS } from '@/config/menus';
- import { getResourcesTree } from '@/api/layout';
- import { getCurrentUser } from '@/utils/token-util';
- import { rewriteLegacyHrMenus } from '@/utils/menu-rewrite';
- import Vue from 'vue';
- const formatRouter = (list = []) => {
- const authorities = [];
- const walk = (nodes) => {
- const arr = [];
- for (const item of nodes || []) {
- if (item.menuType === 2) {
- authorities.push(item);
- item.hide = true;
- }
- if (item.source === 2 && item.extend) {
- item.path = `${item.path}/${item.extend}`;
- }
- item.children = item.children?.length ? walk(item.children) : [];
- arr.push(item);
- }
- return arr;
- };
- return { menuList: walk(list), authorities };
- };
- function buildLocalMenus() {
- return formatMenus(JSON.parse(JSON.stringify(DEFAULT_MENUS)));
- }
- export default {
- namespaced: true,
- state: {
- info: null,
- menus: null,
- authorities: [],
- authoritiesRouter: [],
- roles: [],
- },
- mutations: {
- setUserInfo(state, info) {
- state.info = info;
- },
- setMenus(state, menus) {
- state.menus = menus == null ? menus : rewriteLegacyHrMenus(menus);
- },
- setAuthorities(state, authorities) {
- state.authorities = (authorities || []).map((item) =>
- typeof item === 'string' ? item : item.permissionCode,
- );
- },
- setAuthoritiesRouter(state, authoritiesRouter) {
- state.authoritiesRouter =
- authoritiesRouter == null
- ? authoritiesRouter
- : rewriteLegacyHrMenus(authoritiesRouter);
- },
- setRoles(state, roles) {
- state.roles = roles;
- },
- },
- actions: {
- /**
- * 与 EOM 一致:按 `/page-hr` 过滤资源树,只写入本子应用 store
- */
- async fetchUserInfo({ commit }) {
- const currentUser = getCurrentUser() || {};
- const result = await getResourcesTree({
- groupId: currentUser.currentGroupId,
- roleId: currentUser.currentRoleId,
- }).catch(() => {});
- const list = (Array.isArray(result) ? result : []).filter(
- (item) => item.path === `/page-${SYSTEM_NAME}`,
- );
- if (!list?.length) {
- if (window.__POWERED_BY_QIANKUN__) {
- return {};
- }
- const local = buildLocalMenus();
- const nextMenus = rewriteLegacyHrMenus(local.menus || []);
- commit('setMenus', nextMenus);
- commit('setAuthorities', []);
- commit('setAuthoritiesRouter', []);
- return {
- menus: nextMenus,
- homePath: HOME_PATH || local.homePath,
- authoritiesRouter: [],
- };
- }
- const { menuList, authorities } = formatRouter(list[0].children || []);
- commit('setAuthorities', authorities);
- const { menus, homePath } = formatMenus(
- USER_MENUS ??
- toTreeData({
- data: menuList,
- idField: 'id',
- parentIdField: 'parentId',
- }),
- );
- const { menus: authoritiesRouter } = formatMenus(
- USER_MENUS ??
- toTreeData({
- data: authorities.filter((item) => item.path),
- idField: 'id',
- parentIdField: 'parentId',
- }),
- );
- const nextMenus = rewriteLegacyHrMenus(menus || []);
- const nextAuth = rewriteLegacyHrMenus(authoritiesRouter || []);
- commit('setMenus', nextMenus);
- commit('setAuthoritiesRouter', nextAuth);
- return {
- menus: nextMenus,
- homePath: HOME_PATH || homePath,
- authoritiesRouter: nextAuth,
- };
- },
- setInfo({ commit }, value) {
- commit('setUserInfo', value);
- },
- setMenus({ commit }, value) {
- commit('setMenus', value);
- },
- setMenuBadge({ commit, state }, { path, value, color }) {
- if (window.__POWERED_BY_QIANKUN__) {
- Vue.prototype.$portalStore?.dispatch('user/setMenuBadge', {
- path: `/page-${SYSTEM_NAME}${path}`,
- value,
- color,
- });
- return;
- }
- const menus = formatTreeData(state.menus, (menu) => {
- if (path === menu.path) {
- return {
- ...menu,
- meta: {
- ...menu.meta,
- badge: value,
- badgeColor: color,
- },
- };
- }
- return menu;
- });
- commit('setMenus', menus);
- },
- },
- };
|