Browse Source

fix(@six/smart-pharmacy) - 683: 智慧药事系统第一版禁用状态里外对不上 fix(@six/smart-pharmacy) - 685: 智慧药事系统第一版切换账号登录后,展示了没有权限的菜单

cmj 1 month ago
parent
commit
19ae387957

+ 4 - 1
apps/smart-pharmacy/src/api/method/system.ts

@@ -271,6 +271,7 @@ export function getRoleMethod(id: string) {
   return http.get<SystemModel.Role, TransformData>(
     `/manager/system/role/${id}`,
     {
+      cacheFor: 0,
       transform(data) {
         return fromRole(data);
       },
@@ -470,7 +471,9 @@ export function listDecoctionCentersAllMethod() {
       params: { enterpriseType: 1 },
       cacheFor: 60_000,
       transform(data) {
-        return normalizeEnterpriseList(data).map((item) => fromEnterprise(item));
+        return normalizeEnterpriseList(data).map((item) =>
+          fromEnterprise(item),
+        );
       },
     },
   );

+ 1 - 1
apps/smart-pharmacy/src/preferences.ts

@@ -14,7 +14,7 @@ export const overridesPreferences = defineOverridesPreferences({
     authPageLayout: 'panel-center',
     layout: 'sidebar-nav',
     enablePreferences: false,
-    defaultHomePath: '/prescription/management',
+    defaultHomePath: '',
   },
   logo: {
     source: import.meta.env.VITE_APP_LOGO || '',

+ 36 - 10
apps/smart-pharmacy/src/router/guard.ts

@@ -16,6 +16,31 @@ import { useAuthStore } from '#/store';
 
 import { generateAccess } from './access';
 
+type AccessibleMenu = {
+  children?: AccessibleMenu[];
+  path?: string;
+};
+
+/** 取权限菜单中第一个可访问的叶子页面路径 */
+function getFirstAccessiblePath(menus: AccessibleMenu[]): string | undefined {
+  for (const menu of menus) {
+    if (menu.children?.length) {
+      const childPath = getFirstAccessiblePath(menu.children);
+      if (childPath) return childPath;
+    } else if (menu.path) {
+      return menu.path;
+    }
+  }
+}
+
+function isHomeEntryPath(path: string) {
+  return (
+    path === DEFAULT_PATH ||
+    (!!preferences.app.defaultHomePath &&
+      path === preferences.app.defaultHomePath)
+  );
+}
+
 /**
  * 通用守卫配置
  * @param router
@@ -62,7 +87,8 @@ function setupAccessGuard(router: Router) {
         return decodeURIComponent(
           (to.query?.redirect as string) ||
             userStore.userInfo?.homePath ||
-            preferences.app.defaultHomePath,
+            preferences.app.defaultHomePath ||
+            DEFAULT_PATH,
         );
       }
       return true;
@@ -82,7 +108,7 @@ function setupAccessGuard(router: Router) {
           path: LOGIN_PATH,
           // 如不需要,直接删除 query
           query:
-            to.fullPath === preferences.app.defaultHomePath
+            isHomeEntryPath(to.fullPath)
               ? {}
               : { redirect: encodeURIComponent(to.fullPath) },
           // 携带当前跳转的页面,登录后重新跳转该页面
@@ -122,8 +148,7 @@ function setupAccessGuard(router: Router) {
       // 则会在菜单中显示,但是访问会被重定向到403
       routes: accessRoutes,
     });
-    const path =
-      to.path === DEFAULT_PATH ? preferences.app.defaultHomePath : to.path;
+    const firstAccessiblePath = getFirstAccessiblePath(accessibleMenus);
     const redirect =
       decodeURIComponent(<string>from.query.redirect) === DEFAULT_PATH
         ? void 0
@@ -131,21 +156,22 @@ function setupAccessGuard(router: Router) {
 
     if (!userInfo.homePath) {
       userStore.updateHomePath(
-        preferences.app.defaultHomePath || accessibleMenus[0]?.path,
+        firstAccessiblePath || preferences.app.defaultHomePath,
       );
     }
 
+    const resolvedHomePath =
+      userStore.userInfo?.homePath ||
+      firstAccessiblePath ||
+      preferences.app.defaultHomePath;
+
     // 保存菜单信息和路由信息
     accessStore.setAccessMenus(accessibleMenus);
     accessStore.setAccessRoutes(accessibleRoutes);
     accessStore.setIsAccessChecked(true);
     setLastCheckedUserId(userInfo.userId);
     const redirectPath = (redirect ??
-      (path === preferences.app.defaultHomePath
-        ? userInfo.homePath ||
-          preferences.app.defaultHomePath ||
-          accessibleMenus[0]?.path
-        : to.fullPath)) as string;
+      (isHomeEntryPath(to.path) ? resolvedHomePath : to.fullPath)) as string;
 
     return {
       ...router.resolve(decodeURIComponent(redirectPath)),

+ 1 - 4
apps/smart-pharmacy/src/store/auth.ts

@@ -3,7 +3,6 @@ import { useRouter } from 'vue-router';
 
 import { DEFAULT_PATH, LOGIN_PATH } from '@vben/constants';
 import { $t } from '@vben/locales';
-import { preferences } from '@vben/preferences';
 import { resetAllStores, useAccessStore, useUserStore } from '@vben/stores';
 
 import { useRequest } from '@six/request';
@@ -32,9 +31,7 @@ export const useAuthStore = defineStore('auth', () => {
     resetAccessState(accessStore);
     const userInfo = await userinfo.send(data.accessToken);
 
-    await router.push(
-      userInfo.homePath || preferences.app.defaultHomePath || DEFAULT_PATH,
-    );
+    await router.push(userInfo.homePath || DEFAULT_PATH);
     if (userInfo.realName) {
       notification.success({
         description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.realName}`,