user.js 5.7 KB

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