| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- /**
- * 登录状态管理
- */
- import { formatMenus, toTreeData, formatTreeData } from 'ele-admin';
- import { USER_MENUS } from '@/config/setting';
- import { getResourcesTree } from '@/api/layout';
- const formatRouter = (list) => {
- let menuList = []; // menuType
- let authorities = [];
- const fn = (list, pPath = '') => {
- let arr = [];
- for (const p of list) {
- let curPath = pPath;
- p.path = curPath ? `${curPath}${p.path}` : p.path;
- if (p.parentId === '0') {
- curPath = p.path;
- }
- p.component = '';
- if (p.menuType === 2) {
- p.children = [];
- authorities.push(p);
- } else {
- if (p.children?.length) {
- p.children = fn(p.children, curPath);
- } 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: []
- },
- 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;
- }
- },
- actions: {
- /**
- * 请求用户信息、权限、角色、菜单
- */
- // 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 }) {
- const result = await getResourcesTree().catch(() => {});
- if (!result) {
- return {};
- }
- const { menuList, authorities } = formatRouter(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: 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 }) {
- const menus = formatTreeData(state.menus, (m) => {
- if (path === m.path) {
- return {
- ...m,
- meta: {
- ...m.meta,
- badge: value,
- badgeColor: color
- }
- };
- }
- return m;
- });
- commit('setMenus', menus);
- }
- }
- };
|