user.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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;
  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 }) {
  120. let currentUser=JSON.parse(sessionStorage['currentUser'])
  121. const result = await getResourcesTree({groupId:currentUser.currentGroupId,roleId:currentUser.currentRoleId}).catch(() => {});
  122. const list = result.filter((i) => i.path === `/page-${SYSTEM_NAME}`);
  123. if (!list.length) {
  124. return {};
  125. }
  126. const { menuList, authorities } = formatRouter(list[0].children);
  127. // 用户权限
  128. // const authorities =
  129. // result.authorities
  130. // ?.filter((d) => !!d.authority)
  131. // ?.map((d) => d.authority) ?? [];
  132. commit('setAuthorities', authorities);
  133. // 用户角色
  134. // const roles = result.roles?.map((d) => d.roleCode) ?? [];
  135. // commit('setRoles', roles);
  136. // 用户菜单, 过滤掉按钮类型并转为 children 形式
  137. const { menus, homePath } = formatMenus(
  138. USER_MENUS ??
  139. toTreeData({
  140. data: menuList,
  141. idField: 'id',
  142. parentIdField: 'parentId'
  143. })
  144. );
  145. // 用户路由按钮
  146. const { menus: authoritiesRouter } = formatMenus(
  147. USER_MENUS ??
  148. toTreeData({
  149. data: authorities.filter((i) => i.path),
  150. idField: 'id',
  151. parentIdField: 'parentId'
  152. })
  153. );
  154. console.log('menus--', menus);
  155. commit('setMenus', menus);
  156. commit('setAuthoritiesRouter', authoritiesRouter);
  157. // const menus = result;
  158. // const homePath = '/dashboard/workplace';
  159. return {
  160. menus,
  161. homePath,
  162. authoritiesRouter
  163. };
  164. },
  165. /**
  166. * 更新用户信息
  167. */
  168. setInfo ({ commit }, value) {
  169. commit('setUserInfo', value);
  170. },
  171. /**
  172. * 更新菜单数据
  173. */
  174. setMenus ({ commit }, value) {
  175. commit('setMenus', value);
  176. },
  177. /**
  178. * 更新菜单的badge
  179. */
  180. setMenuBadge ({ commit, state }, { path, value, color }) {
  181. if (window.__POWERED_BY_QIANKUN__) {
  182. Vue.prototype.$portalStore?.dispatch('user/setMenuBadge', {
  183. path: `/page-${SYSTEM_NAME}${path}`,
  184. value,
  185. color
  186. });
  187. } else {
  188. const menus = formatTreeData(state.menus, (m) => {
  189. if (path === m.path) {
  190. return {
  191. ...m,
  192. meta: {
  193. ...m.meta,
  194. badge: value,
  195. badgeColor: color
  196. }
  197. };
  198. }
  199. return m;
  200. });
  201. commit('setMenus', menus);
  202. }
  203. }
  204. }
  205. };