user.js 5.4 KB

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