account.api.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { type AccountFormState, type PeopleModel, transformAccount } from '@/model';
  2. import type { UserModel } from '@/model/system.model';
  3. import request from '@/request/alova';
  4. import router from '@/router';
  5. import { roleMethod } from './system.api';
  6. export interface Menu {
  7. label: string;
  8. title: string;
  9. key: string;
  10. children?: Menu[];
  11. }
  12. export interface AccountModel {
  13. token: string;
  14. local: PeopleModel;
  15. menus?: Menu[];
  16. permission: string[];
  17. user?: UserModel;
  18. }
  19. export function loginMethod(data: AccountFormState) {
  20. return request.Post<string, { access_token: string }>(`/auth/token`, data, {
  21. transform(data, headers) {
  22. return `Bearer ${data.access_token}`;
  23. },
  24. });
  25. }
  26. export function accountMethod(token: string) {
  27. return request.Get<AccountModel>(`/system/user/getInfo`, {
  28. headers: { Authorization: token },
  29. transform(data: any, headers) {
  30. return {
  31. token,
  32. local: transformAccount(data.user),
  33. permission: Array.isArray(data.permissions) ? data.permissions : [],
  34. user: data.user,
  35. };
  36. },
  37. meta: { unconvert: true },
  38. });
  39. }
  40. export function getMenusMethod(account: AccountModel) {
  41. const routes = new Set(router.getRoutes().map((route) => route.path));
  42. const transformMenus = (data: any[], prefix?: string) => {
  43. const menus: Menu[] = [];
  44. if (!Array.isArray(data)) return menus;
  45. for (const menu of data) {
  46. const path = [prefix, menu?.path].filter((v) => v != null).join('/');
  47. const title = menu?.meta?.title ?? path;
  48. const children = transformMenus(menu.children, path === '/' ? '' : path);
  49. if (children.length > 1) {
  50. menus.push({ key: path, title, label: title, children });
  51. } else if (children.length === 1) {
  52. if (path === '/') {
  53. menus.push(children[0]);
  54. } else {
  55. menus.push({ key: path, title, label: title, children });
  56. }
  57. } else if (routes.has(path)) {
  58. menus.push({ key: path, title, label: title });
  59. }
  60. }
  61. return menus;
  62. };
  63. return request.Get<AccountModel, any[]>(`/system/menu/getRouters`, {
  64. headers: { Authorization: account.token },
  65. transform(data) {
  66. // data.push({
  67. // path: '/equipment',
  68. // meta: { title: '设备管理' },
  69. // children: [
  70. // {
  71. // path: 'registe',
  72. // meta: { title: '设备登记' },
  73. // },
  74. // {
  75. // path: 'configured',
  76. // meta: { title: '调理方案配置' },
  77. // },
  78. // ],
  79. // });
  80. // console.log(data, 'push之后的data', transformMenus(data));
  81. return { ...account, menus: transformMenus(data) };
  82. },
  83. });
  84. }