user.js 5.9 KB

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