auth.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { defineStore } from "pinia";
  2. import { AuthState } from "@/stores/interface";
  3. import { getAuthButtonListApi, getAuthMenuListApi } from "@/api/modules/login";
  4. import { getFlatMenuList, getShowMenuList, getAllBreadcrumbList, localGet } from "@/utils";
  5. import { usePermission } from "@/utils/permission";
  6. export const useAuthStore = defineStore({
  7. id: "geeker-auth",
  8. state: (): AuthState => ({
  9. // 按钮权限列表
  10. authButtonList: {},
  11. // 菜单权限列表
  12. authMenuList: [],
  13. // 当前页面的 router name,用来做按钮权限筛选
  14. routeName: ""
  15. }),
  16. getters: {
  17. // 按钮权限列表
  18. authButtonListGet: state => state.authButtonList,
  19. // 菜单权限列表 ==> 这里的菜单没有经过任何处理
  20. authMenuListGet: state => state.authMenuList,
  21. // 菜单权限列表 ==> 左侧菜单栏渲染,需要剔除 isHide == true
  22. showMenuListGet: state => getShowMenuList(state.authMenuList),
  23. // 菜单权限列表 ==> 扁平化之后的一维数组菜单,主要用来添加动态路由
  24. flatMenuListGet: state => getFlatMenuList(state.authMenuList),
  25. // 递归处理后的所有面包屑导航列表
  26. breadcrumbListGet: state => getAllBreadcrumbList(state.authMenuList)
  27. },
  28. actions: {
  29. // Get AuthButtonList
  30. async getAuthButtonList() {
  31. const { data } = await getAuthButtonListApi();
  32. this.authButtonList = data;
  33. },
  34. // Get AuthMenuList
  35. async getAuthMenuList() {
  36. const { data } = (await getAuthMenuListApi()) as any;
  37. const hasPermission = await usePermission();
  38. const hideMenuNames = ["storeDecoration", "financialManagement", "licenseManagement", "dynamicManagement"];
  39. // 获取用户信息和 businessSection(经营板块)、mealsFlag(是否提供餐食)、storeId、storeTickets
  40. const userInfo = localGet("geeker-user")?.userInfo || {};
  41. const businessSection = Number(userInfo.businessSection || localGet("businessSection") || 0);
  42. const mealsFlag = Number(userInfo.mealsFlag || localGet("mealsFlag") || 0);
  43. const storeId = userInfo.storeId ?? localGet("createdId");
  44. const storeTickets = Number(userInfo.storeTickets ?? localGet("storeTickets") ?? 0);
  45. // 递归处理菜单的显示/隐藏状态
  46. const processMenus = (menuList: any[]) => {
  47. menuList.forEach(menu => {
  48. // 根据菜单名称判断是否需要处理
  49. if (menu.name && hideMenuNames.includes(menu.name)) {
  50. if (!hasPermission) {
  51. // 如果没有权限,隐藏菜单
  52. menu.meta.isHide = true;
  53. } else {
  54. // 如果有权限,确保菜单显示(移除 isHide 或设置为 false)
  55. if (menu.meta) {
  56. menu.meta.isHide = false;
  57. } else {
  58. menu.meta = { isHide: false };
  59. }
  60. }
  61. }
  62. // 根据 businessSection 判断菜单显示
  63. if (menu.meta && menu.meta.title) {
  64. switch (menu.meta.title) {
  65. case "菜单管理":
  66. // 只在特色美食(1) 时显示菜单管理
  67. menu.meta.isHide = businessSection !== 1;
  68. break;
  69. case "酒单管理":
  70. // 只在酒吧(2) 时显示酒单管理
  71. menu.meta.isHide = businessSection !== 2;
  72. break;
  73. case "设施与服务":
  74. // 只在洗浴汗蒸(4) 和 运动健身(7) 时显示设施与服务
  75. menu.meta.isHide = ![4, 7].includes(businessSection);
  76. break;
  77. case "门店基础信息":
  78. case "门店头图":
  79. case "官方相册":
  80. // 所有业务类型都显示
  81. menu.meta.isHide = false;
  82. break;
  83. case "人员配置":
  84. // 美食(1)和KTV(3)不显示人员配置,其他业务类型都显示
  85. menu.meta.isHide = [1, 3].includes(businessSection) || !storeId;
  86. break;
  87. case "食品经营许可证":
  88. // 特色美食(1) 和 酒吧(2):正常显示
  89. // KTV(3)、洗浴汗蒸(4)、按摩足疗(5)、丽人美发(6)、运动健身(7):仅提供餐食时显示
  90. if ([1, 2].includes(businessSection)) {
  91. // 酒吧和特色美食无条件显示
  92. menu.meta.isHide = false;
  93. } else if ([3, 4, 5, 6, 7].includes(businessSection)) {
  94. // KTV等业态需要提供餐食才显示
  95. menu.meta.isHide = mealsFlag !== 1;
  96. }
  97. break;
  98. case "其他资质证明":
  99. // 只有酒吧(businessSection=2) 和 KTV(businessSection=3) 显示其他资质证明
  100. menu.meta.isHide = ![2, 3].includes(businessSection);
  101. break;
  102. case "子账号与角色管理":
  103. // 有 storeId 时显示,无 storeId 时不显示
  104. menu.meta.isHide = !storeId;
  105. break;
  106. case "价目表":
  107. // 有店铺的商家才显示价目表
  108. menu.meta.isHide = !storeId;
  109. break;
  110. case "演出":
  111. // 有店铺且为休闲娱乐(businessSection=2) 时显示演出
  112. menu.meta.isHide = !storeId || businessSection !== 2;
  113. break;
  114. case "门店装修":
  115. menu.meta.isHide = !storeId;
  116. break;
  117. }
  118. }
  119. // 门店装修:根据 storeTickets 只显示其一,0=只显示「门店装修」,1=只显示「装修公司」
  120. if (menu.name === "storeDecorationManagement" && menu.children?.length) {
  121. menu.children.forEach((child: any) => {
  122. if (child.meta?.title === "门店装修") {
  123. child.meta.isHide = storeTickets !== 0;
  124. } else if (child.meta?.title === "装修公司") {
  125. child.meta.isHide = storeTickets !== 1;
  126. }
  127. });
  128. }
  129. // 处理子菜单
  130. // if (menu.children && menu.children.length > 0) {
  131. // // 检查子菜单中是否有需要显示的菜单项
  132. // let hasVisibleChild = false;
  133. // menu.children.forEach((child: any) => {
  134. // if (!child.meta?.isHide) {
  135. // hasVisibleChild = true;
  136. // }
  137. // });
  138. // // 如果父菜单基础信息、头图、相册本身被隐藏,但有可见子菜单,则显示父菜单
  139. // if (menu.meta?.isHide && hasVisibleChild) {
  140. // menu.meta.isHide = false;
  141. // }
  142. // // 递归处理子菜单
  143. // processMenus(menu.children);
  144. // }
  145. });
  146. };
  147. processMenus(data);
  148. this.authMenuList = data;
  149. },
  150. // Set RouteName
  151. async setRouteName(name: string) {
  152. this.routeName = name;
  153. }
  154. }
  155. });