user.js 6.2 KB

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