menu.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import type { Recordable } from '@vben/types';
  2. import { requestClient } from '#/api/request';
  3. export namespace SystemMenuApi {
  4. /** 徽标颜色集合 */
  5. export const BadgeVariants = [
  6. 'default',
  7. 'destructive',
  8. 'primary',
  9. 'success',
  10. 'warning',
  11. ] as const;
  12. /** 徽标类型集合 */
  13. export const BadgeTypes = ['dot', 'normal'] as const;
  14. /** 菜单类型集合 */
  15. export const MenuTypes = [
  16. 'catalog',
  17. 'menu',
  18. 'embedded',
  19. 'link',
  20. 'button',
  21. ] as const;
  22. /** 系统菜单 */
  23. export interface SystemMenu {
  24. [key: string]: any;
  25. /** 后端权限标识 */
  26. authCode: string;
  27. /** 子级 */
  28. children?: SystemMenu[];
  29. /** 组件 */
  30. component?: string;
  31. /** 菜单ID */
  32. id: string;
  33. /** 菜单元数据 */
  34. meta?: {
  35. /** 激活时显示的图标 */
  36. activeIcon?: string;
  37. /** 作为路由时,需要激活的菜单的Path */
  38. activePath?: string;
  39. /** 固定在标签栏 */
  40. affixTab?: boolean;
  41. /** 在标签栏固定的顺序 */
  42. affixTabOrder?: number;
  43. /** 徽标内容(当徽标类型为normal时有效) */
  44. badge?: string;
  45. /** 徽标类型 */
  46. badgeType?: (typeof BadgeTypes)[number];
  47. /** 徽标颜色 */
  48. badgeVariants?: (typeof BadgeVariants)[number];
  49. /** 在菜单中隐藏下级 */
  50. hideChildrenInMenu?: boolean;
  51. /** 在面包屑中隐藏 */
  52. hideInBreadcrumb?: boolean;
  53. /** 在菜单中隐藏 */
  54. hideInMenu?: boolean;
  55. /** 在标签栏中隐藏 */
  56. hideInTab?: boolean;
  57. /** 菜单图标 */
  58. icon?: string;
  59. /** 内嵌Iframe的URL */
  60. iframeSrc?: string;
  61. /** 是否缓存页面 */
  62. keepAlive?: boolean;
  63. /** 外链页面的URL */
  64. link?: string;
  65. /** 同一个路由最大打开的标签数 */
  66. maxNumOfOpenTab?: number;
  67. /** 无需基础布局 */
  68. noBasicLayout?: boolean;
  69. /** 是否在新窗口打开 */
  70. openInNewWindow?: boolean;
  71. /** 菜单排序 */
  72. order?: number;
  73. /** 额外的路由参数 */
  74. query?: Recordable<any>;
  75. /** 菜单标题 */
  76. title?: string;
  77. };
  78. /** 菜单名称 */
  79. name: string;
  80. /** 路由路径 */
  81. path: string;
  82. /** 父级ID */
  83. pid: string;
  84. /** 重定向 */
  85. redirect?: string;
  86. /** 菜单类型 */
  87. type: (typeof MenuTypes)[number];
  88. }
  89. }
  90. /**
  91. * 获取菜单数据列表
  92. */
  93. async function getMenuList() {
  94. return requestClient.get<Array<SystemMenuApi.SystemMenu>>(
  95. '/system/menu/list',
  96. );
  97. }
  98. async function isMenuNameExists(
  99. name: string,
  100. id?: SystemMenuApi.SystemMenu['id'],
  101. ) {
  102. return requestClient.get<boolean>('/system/menu/name-exists', {
  103. params: { id, name },
  104. });
  105. }
  106. async function isMenuPathExists(
  107. path: string,
  108. id?: SystemMenuApi.SystemMenu['id'],
  109. ) {
  110. return requestClient.get<boolean>('/system/menu/path-exists', {
  111. params: { id, path },
  112. });
  113. }
  114. /**
  115. * 创建菜单
  116. * @param data 菜单数据
  117. */
  118. async function createMenu(
  119. data: Omit<SystemMenuApi.SystemMenu, 'children' | 'id'>,
  120. ) {
  121. return requestClient.post('/system/menu', data);
  122. }
  123. /**
  124. * 更新菜单
  125. *
  126. * @param id 菜单 ID
  127. * @param data 菜单数据
  128. */
  129. async function updateMenu(
  130. id: string,
  131. data: Omit<SystemMenuApi.SystemMenu, 'children' | 'id'>,
  132. ) {
  133. return requestClient.put(`/system/menu/${id}`, data);
  134. }
  135. /**
  136. * 删除菜单
  137. * @param id 菜单 ID
  138. */
  139. async function deleteMenu(id: string) {
  140. return requestClient.delete(`/system/menu/${id}`);
  141. }
  142. export {
  143. createMenu,
  144. deleteMenu,
  145. getMenuList,
  146. isMenuNameExists,
  147. isMenuPathExists,
  148. updateMenu,
  149. };