userInfo.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // 全局共享数据示例
  2. import { useRequest } from '@umijs/max';
  3. import { queryCurrentV2, queryUserRole } from '@/services/user';
  4. const useUser = () => {
  5. const { data: user, loading } = useRequest(queryCurrentV2, {
  6. cacheKey: 'queryCurrent',
  7. });
  8. // const { data: roleData, loading } = useRequest(() => queryUserRole(user.ID), {
  9. // cacheKey: 'queryUserRole',
  10. // ready: !!user?.ID,
  11. // });
  12. // if (!roleData) return { user: null, loading: loading };
  13. if (!user) return { user: null, loading: loading };
  14. let permission = {};
  15. user.Permissions?.forEach((item: any) => {
  16. permission = {
  17. ...permission,
  18. ...item.Menus,
  19. };
  20. });
  21. // let roleList = getRoleList(roleData.Dep);
  22. return {
  23. user: {
  24. ...user,
  25. Permission: permission,
  26. // roleList: roleList,
  27. },
  28. loading,
  29. };
  30. };
  31. const getRoleList = (data: any) => {
  32. let roleList: any = [];
  33. (data || []).forEach((dep: any) => {
  34. (dep.Role || []).forEach((role: any) => {
  35. roleList.push(role);
  36. });
  37. if (dep.children) {
  38. let res = getRoleList(dep.children);
  39. roleList = res.concat(res);
  40. }
  41. });
  42. return roleList;
  43. };
  44. export default useUser;