account.api.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. interface Menu {
  6. label: string;
  7. title: string;
  8. key: string;
  9. children?: Menu[];
  10. }
  11. export interface AccountModel {
  12. token: string;
  13. local: PeopleModel;
  14. menus?: Menu[];
  15. user?: UserModel;
  16. }
  17. export function loginMethod(data: AccountFormState) {
  18. return request.Post<string, { access_token: string }>(`/auth/token`, data, {
  19. transform(data, headers) {
  20. return `Bearer ${data.access_token}`;
  21. },
  22. });
  23. }
  24. export function accountMethod(token: string) {
  25. return request.Get<AccountModel>(`/system/user/getInfo`, {
  26. headers: { Authorization: token },
  27. transform(data: any, headers) {
  28. return {
  29. token,
  30. local: transformAccount(data.user),
  31. user: data.user,
  32. };
  33. },
  34. meta: { unconvert: true },
  35. });
  36. }
  37. export function getMenusMethod(account: AccountModel) {
  38. const routes = new Set(router.getRoutes().map((route) => route.path));
  39. const transformMenus = (data: any[], prefix?: string) => {
  40. const menus: Menu[] = [];
  41. if (!Array.isArray(data)) return menus;
  42. for (const menu of data) {
  43. const path = [prefix, menu?.path].filter((v) => v != null).join('/');
  44. const title = menu?.meta?.title ?? path;
  45. const children = transformMenus(menu.children, path === '/' ? '' : path);
  46. if (children.length > 1) {
  47. menus.push({ key: path, title, label: title, children });
  48. } else if (children.length === 1) {
  49. menus.push(children[0]);
  50. } else if (routes.has(path)) {
  51. menus.push({ key: path, title, label: title });
  52. }
  53. }
  54. return menus;
  55. };
  56. return request.Get<AccountModel, any[]>(`/system/menu/getRouters`, {
  57. headers: { Authorization: account.token },
  58. transform(data) {
  59. // data.push(
  60. // {
  61. // path: '/follow',
  62. // meta: {
  63. // title: '随访管理',
  64. // },
  65. // children: [
  66. // {
  67. // path: 'plan',
  68. // meta: {
  69. // title: '随访计划',
  70. // },
  71. // },
  72. // {
  73. // path: 'task',
  74. // meta: {
  75. // title: '随访任务',
  76. // },
  77. // },
  78. // {
  79. // path: 'assessment',
  80. // meta: {
  81. // title: '随访评估',
  82. // },
  83. // },
  84. // ],
  85. // },
  86. // {
  87. // path: '/tcmRecuperation',
  88. // meta: {
  89. // title: '中医调养',
  90. // },
  91. // children: [
  92. // {
  93. // path: 'preserve',
  94. // meta: {
  95. // title: '服务项目维护',
  96. // },
  97. // },
  98. // {
  99. // path: 'system',
  100. // meta: {
  101. // title: '系统服务包',
  102. // },
  103. // },
  104. // {
  105. // path: 'institution',
  106. // meta: {
  107. // title: '机构服务包',
  108. // },
  109. // },
  110. // ],
  111. // }
  112. // );
  113. // console.log(data);
  114. return { ...account, menus: transformMenus(data) };
  115. },
  116. });
  117. }