| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /**
- * 登录状态管理
- */
- import { formatMenus, toTreeData, formatTreeData } from 'ele-admin';
- import { USER_MENUS } from '@/config/setting';
- import { getResourcesTree } from '@/api/layout';
- import { SYSTEM_NAME } from '@/config/setting';
- import Vue from 'vue';
- import {getCurrentUserAuthorityDeptAPI} from "@/api/login";
- const formatRouter = (list) => {
- let menuList = []; // menuType
- let authorities = [];
- const fn = (list) => {
- let arr = [];
- for (const p of list) {
- if (p.menuType === 2) {
- // p.children = [];
- // authorities.push(p);
- p.hide = true;
- }
- //报表菜单 source = 2 为报表时 extend 为报表id
- if (p.source === 2 && p.extend) {
- p.path = p.path + '/' + p.extend;
- }
- // else {
- if (p.children?.length) {
- p.children = fn(p.children);
- } else {
- p.children = [];
- }
- arr.push(p);
- // }
- }
- return arr;
- };
- menuList = fn(list);
- return { menuList, authorities };
- };
- export default {
- namespaced: true,
- state: {
- // 当前登录用户信息
- info: null,
- // 当前登录用户的菜单
- menus: null,
- // 当前登录用户的权限
- authorities: [],
- // 当前登录用户的权限路由
- authoritiesRouter: [],
- // 当前登录用户的角色
- roles: [],
- // 当前登录用户的数据权限部门数据
- authorityDept: {}
- },
- mutations: {
- // 设置登录用户的信息
- setUserInfo (state, info) {
- state.info = info;
- },
- // 设置登录用户的菜单
- setMenus (state, menus) {
- state.menus = menus;
- },
- // 设置登录用户的权限
- setAuthorities (state, authorities) {
- state.authorities = authorities;
- },
- // 设置登录用户的权限路由
- setAuthoritiesRouter (state, authoritiesRouter) {
- state.authoritiesRouter = authoritiesRouter;
- },
- // 设置登录用户的角色
- setRoles (state, roles) {
- state.roles = roles;
- },
- // 当前登录用户的数据权限部门数据
- setAuthorityDept(state, info) {
- state.authorityDept = info;
- }
- },
- actions: {
- async getCurrentUserAuthorityDept({ commit }) {
- const info = await getCurrentUserAuthorityDeptAPI();
- commit('setAuthorityDept', info);
- },
- /**
- * 请求用户信息、权限、角色、菜单
- */
- // async fetchUserInfo({ commit }) {
- // const result = await getResourcesTree().catch(() => {});
- // if (!result) {
- // return {};
- // }
- // // 用户信息
- // commit('setUserInfo', result);
- // // 用户权限
- // const authorities =
- // result.authorities
- // ?.filter((d) => !!d.authority)
- // ?.map((d) => d.authority) ?? [];
- // commit('setAuthorities', authorities);
- // // 用户角色
- // const roles = result.roles?.map((d) => d.roleCode) ?? [];
- // commit('setRoles', roles);
- // // 用户菜单, 过滤掉按钮类型并转为 children 形式
- // const { menus, homePath } = formatMenus(
- // USER_MENUS ??
- // toTreeData({
- // data: result.authorities?.filter((d) => d.menuType !== 1),
- // idField: 'menuId',
- // parentIdField: 'parentId'
- // })
- // );
- // commit('setMenus', menus);
- // console.log('menus',menus, 'homePath',homePath)
- // return { menus, homePath };
- // },
- //动态路由
- async fetchUserInfo ({ commit }) {
- let currentUser=JSON.parse(sessionStorage['currentUser'])
- const result = await getResourcesTree({groupId:currentUser.currentGroupId,roleId:currentUser.currentRoleId}).catch(() => {});
- const list = result.filter((i) => i.path === `/page-${SYSTEM_NAME}`);
- if (!list.length) {
- return {};
- }
- const { menuList, authorities } = formatRouter(list[0].children);
- // 用户权限
- // const authorities =
- // result.authorities
- // ?.filter((d) => !!d.authority)
- // ?.map((d) => d.authority) ?? [];
- commit('setAuthorities', authorities);
- // 用户角色
- // const roles = result.roles?.map((d) => d.roleCode) ?? [];
- // commit('setRoles', roles);
- // 用户菜单, 过滤掉按钮类型并转为 children 形式
- const { menus, homePath } = formatMenus(
- USER_MENUS ??
- toTreeData({
- data: menuList,
- idField: 'id',
- parentIdField: 'parentId'
- })
- );
- // 用户路由按钮
- const { menus: authoritiesRouter } = formatMenus(
- USER_MENUS ??
- toTreeData({
- data: authorities.filter((i) => i.path),
- idField: 'id',
- parentIdField: 'parentId'
- })
- );
- console.log('menus--', menus);
- commit('setMenus', menus);
- commit('setAuthoritiesRouter', authoritiesRouter);
- // const menus = result;
- // const homePath = '/dashboard/workplace';
- return {
- menus,
- homePath,
- authoritiesRouter
- };
- },
- /**
- * 更新用户信息
- */
- setInfo ({ commit }, value) {
- commit('setUserInfo', value);
- },
- /**
- * 更新菜单数据
- */
- setMenus ({ commit }, value) {
- commit('setMenus', value);
- },
- /**
- * 更新菜单的badge
- */
- setMenuBadge ({ commit, state }, { path, value, color }) {
- if (window.__POWERED_BY_QIANKUN__) {
- Vue.prototype.$portalStore?.dispatch('user/setMenuBadge', {
- path: `/page-${SYSTEM_NAME}${path}`,
- value,
- color
- });
- } else {
- const menus = formatTreeData(state.menus, (m) => {
- if (path === m.path) {
- return {
- ...m,
- meta: {
- ...m.meta,
- badge: value,
- badgeColor: color
- }
- };
- }
- return m;
- });
- commit('setMenus', menus);
- }
- }
- }
- };
|