user.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * 登录状态管理
  3. */
  4. import { formatMenus, toTreeData, formatTreeData } from 'ele-admin';
  5. import { USER_MENUS } from '@/config/setting';
  6. import { getResourcesTree } from '@/api/layout';
  7. const formatRouter = (list) => {
  8. let menuList = []; // menuType
  9. let authorities = [];
  10. const fn = (list, pPath = '') => {
  11. let arr = [];
  12. for (const p of list) {
  13. let curPath = pPath;
  14. p.path = curPath ? `${curPath}${p.path}` : p.path;
  15. if (p.parentId === '0') {
  16. curPath = p.path;
  17. }
  18. p.component = '';
  19. if (p.menuType === 2) {
  20. p.children = [];
  21. authorities.push(p);
  22. } else {
  23. if (p.children?.length) {
  24. p.children = fn(p.children, curPath);
  25. } else {
  26. p.children = [];
  27. }
  28. arr.push(p);
  29. }
  30. }
  31. return arr;
  32. };
  33. menuList = fn(list);
  34. return { menuList, authorities };
  35. };
  36. export default {
  37. namespaced: true,
  38. state: {
  39. // 当前登录用户信息
  40. info: null,
  41. // 当前登录用户的菜单
  42. menus: null,
  43. // 当前登录用户的权限
  44. authorities: [],
  45. // 当前登录用户的权限路由
  46. authoritiesRouter: [],
  47. // 当前登录用户的角色
  48. roles: []
  49. },
  50. mutations: {
  51. // 设置登录用户的信息
  52. setUserInfo (state, info) {
  53. state.info = info;
  54. },
  55. // 设置登录用户的菜单
  56. setMenus (state, menus) {
  57. state.menus = menus;
  58. },
  59. // 设置登录用户的权限
  60. setAuthorities (state, authorities) {
  61. state.authorities = authorities;
  62. },
  63. // 设置登录用户的权限路由
  64. setAuthoritiesRouter (state, authoritiesRouter) {
  65. state.authoritiesRouter = authoritiesRouter;
  66. },
  67. // 设置登录用户的角色
  68. setRoles (state, roles) {
  69. state.roles = roles;
  70. }
  71. },
  72. actions: {
  73. /**
  74. * 请求用户信息、权限、角色、菜单
  75. */
  76. // async fetchUserInfo({ commit }) {
  77. // const result = await getResourcesTree().catch(() => {});
  78. // if (!result) {
  79. // return {};
  80. // }
  81. // // 用户信息
  82. // commit('setUserInfo', result);
  83. // // 用户权限
  84. // const authorities =
  85. // result.authorities
  86. // ?.filter((d) => !!d.authority)
  87. // ?.map((d) => d.authority) ?? [];
  88. // commit('setAuthorities', authorities);
  89. // // 用户角色
  90. // const roles = result.roles?.map((d) => d.roleCode) ?? [];
  91. // commit('setRoles', roles);
  92. // // 用户菜单, 过滤掉按钮类型并转为 children 形式
  93. // const { menus, homePath } = formatMenus(
  94. // USER_MENUS ??
  95. // toTreeData({
  96. // data: result.authorities?.filter((d) => d.menuType !== 1),
  97. // idField: 'menuId',
  98. // parentIdField: 'parentId'
  99. // })
  100. // );
  101. // commit('setMenus', menus);
  102. // console.log('menus',menus, 'homePath',homePath)
  103. // return { menus, homePath };
  104. // },
  105. //动态路由
  106. async fetchUserInfo ({ commit }) {
  107. const result = await getResourcesTree().catch(() => {});
  108. if (!result) {
  109. return {};
  110. }
  111. const { menuList, authorities } = formatRouter(result);
  112. // 用户权限
  113. // const authorities =
  114. // result.authorities
  115. // ?.filter((d) => !!d.authority)
  116. // ?.map((d) => d.authority) ?? [];
  117. commit('setAuthorities', authorities);
  118. // 用户角色
  119. // const roles = result.roles?.map((d) => d.roleCode) ?? [];
  120. // commit('setRoles', roles);
  121. // 用户菜单, 过滤掉按钮类型并转为 children 形式
  122. const { menus, homePath } = formatMenus(
  123. USER_MENUS ??
  124. toTreeData({
  125. data: menuList,
  126. idField: 'id',
  127. parentIdField: 'parentId'
  128. })
  129. );
  130. // 用户路由按钮
  131. const { menus: authoritiesRouter } = formatMenus(
  132. USER_MENUS ??
  133. toTreeData({
  134. data: authorities.filter((i) => i.path),
  135. idField: 'id',
  136. parentIdField: 'parentId'
  137. })
  138. );
  139. console.log('menus--', menus);
  140. commit('setMenus', menus);
  141. commit('setAuthoritiesRouter', authoritiesRouter);
  142. // const menus = result;
  143. // const homePath = '/dashboard/workplace';
  144. return {
  145. menus,
  146. homePath,
  147. authoritiesRouter
  148. };
  149. },
  150. /**
  151. * 更新用户信息
  152. */
  153. setInfo ({ commit }, value) {
  154. commit('setUserInfo', value);
  155. },
  156. /**
  157. * 更新菜单数据
  158. */
  159. setMenus ({ commit }, value) {
  160. commit('setMenus', value);
  161. },
  162. /**
  163. * 更新菜单的badge
  164. */
  165. setMenuBadge ({ commit, state }, { path, value, color }) {
  166. const menus = formatTreeData(state.menus, (m) => {
  167. if (path === m.path) {
  168. return {
  169. ...m,
  170. meta: {
  171. ...m.meta,
  172. badge: value,
  173. badgeColor: color
  174. }
  175. };
  176. }
  177. return m;
  178. });
  179. commit('setMenus', menus);
  180. }
  181. }
  182. };