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