user.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. import { getCurrentUser, setCurrentUser } from '@/utils/token-util';
  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. p.meta = {
  31. secretLevel: p.secretLevel,
  32. source: p.source,
  33. path: p.path
  34. };
  35. arr.push(p);
  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. state.authorities = authorities.map((item) => item.permissionCode);
  71. },
  72. // 设置登录用户的权限路由
  73. setAuthoritiesRouter(state, authoritiesRouter) {
  74. state.authoritiesRouter = authoritiesRouter;
  75. },
  76. // 设置登录用户的角色
  77. setRoles(state, roles) {
  78. state.roles = roles;
  79. },
  80. // 当前登录用户的数据权限部门数据
  81. setAuthorityDept(state, info) {
  82. state.authorityDept = info;
  83. }
  84. },
  85. actions: {
  86. async getCurrentUserAuthorityDept({ commit }) {
  87. return [];
  88. const info = await getCurrentUserAuthorityDeptAPI();
  89. commit('setAuthorityDept', info);
  90. },
  91. /**
  92. * 请求用户信息、权限、角色、菜单
  93. */
  94. // async fetchUserInfo({ commit }) {
  95. // const result = await getResourcesTree().catch(() => {});
  96. // if (!result) {
  97. // return {};
  98. // }
  99. // // 用户信息
  100. // commit('setUserInfo', result);
  101. // // 用户权限
  102. // const authorities =
  103. // result.authorities
  104. // ?.filter((d) => !!d.authority)
  105. // ?.map((d) => d.authority) ?? [];
  106. // commit('setAuthorities', authorities);
  107. // // 用户角色
  108. // const roles = result.roles?.map((d) => d.roleCode) ?? [];
  109. // commit('setRoles', roles);
  110. // // 用户菜单, 过滤掉按钮类型并转为 children 形式
  111. // const { menus, homePath } = formatMenus(
  112. // USER_MENUS ??
  113. // toTreeData({
  114. // data: result.authorities?.filter((d) => d.menuType !== 1),
  115. // idField: 'menuId',
  116. // parentIdField: 'parentId'
  117. // })
  118. // );
  119. // commit('setMenus', menus);
  120. // console.log('menus',menus, 'homePath',homePath)
  121. // return { menus, homePath };
  122. // },
  123. //动态路由
  124. async fetchUserInfo({ commit, state }) {
  125. let currentUser = getCurrentUser();
  126. const result = await getResourcesTree({
  127. groupId: currentUser.currentGroupId,
  128. roleId: currentUser.currentRoleId
  129. }).catch(() => {});
  130. const list = result?.filter((i) => i.path === '/page-main-data');
  131. if (!list?.length) {
  132. return {};
  133. }
  134. const { menuList, authorities } = formatRouter(list[0].children);
  135. // 用户权限
  136. // const authorities =
  137. // result.authorities
  138. // ?.filter((d) => !!d.authority)
  139. // ?.map((d) => d.authority) ?? [];
  140. commit('setAuthorities', authorities);
  141. // 用户角色
  142. // const roles = result.roles?.map((d) => d.roleCode) ?? [];
  143. // commit('setRoles', roles);
  144. // 用户菜单, 过滤掉按钮类型并转为 children 形式
  145. const { menus, homePath } = formatMenus(
  146. USER_MENUS ??
  147. toTreeData({
  148. data: menuList,
  149. idField: 'id',
  150. parentIdField: 'parentId'
  151. })
  152. );
  153. // 用户路由按钮
  154. const { menus: authoritiesRouter } = formatMenus(
  155. USER_MENUS ??
  156. toTreeData({
  157. data: authorities.filter((i) => i.path),
  158. idField: 'id',
  159. parentIdField: 'parentId'
  160. })
  161. );
  162. console.log('menus--', homePath, menus);
  163. commit('setMenus', menus);
  164. commit('setAuthoritiesRouter', authoritiesRouter);
  165. // const menus = result;
  166. // const homePath = '/dashboard/workplace';
  167. return {
  168. menus,
  169. homePath,
  170. authoritiesRouter
  171. };
  172. },
  173. /**
  174. * 更新用户信息
  175. */
  176. setInfo({ commit }, value) {
  177. commit('setUserInfo', value);
  178. },
  179. /**
  180. * 更新菜单数据
  181. */
  182. setMenus({ commit }, value) {
  183. commit('setMenus', value);
  184. },
  185. /**
  186. * 更新菜单的badge
  187. */
  188. setMenuBadge({ commit, state }, { path, value, color }) {
  189. const menus = formatTreeData(state.menus, (m) => {
  190. if (path === m.path) {
  191. return {
  192. ...m,
  193. meta: {
  194. ...m.meta,
  195. badge: value,
  196. badgeColor: color
  197. }
  198. };
  199. }
  200. return m;
  201. });
  202. commit('setMenus', menus);
  203. }
  204. }
  205. };