user.js 5.8 KB

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