2 Commity 1ac3738f11 ... 41b04145dc

Autor SHA1 Wiadomość Data
  1811872455@163.com 41b04145dc Merge remote-tracking branch 'origin/master' 1 tydzień temu
  1811872455@163.com 5cc3e054b4 菜单 1 tydzień temu

+ 99 - 19
java/storlead-system/storlead-system-api/src/main/java/com/storlead/system/controller/PermissionResApiController.java

@@ -1,14 +1,18 @@
 package com.storlead.system.controller;
 
 import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.storlead.framework.common.result.Result;
 import com.storlead.framework.common.util.PingyinUtil;
 import com.storlead.framework.util.LoginUserUtil;
 import com.storlead.system.pojo.entity.MenuEntity;
+import com.storlead.system.pojo.entity.SysAppEntity;
+import com.storlead.system.pojo.vo.AppRouterVO;
 import com.storlead.system.pojo.vo.MetaVo;
 import com.storlead.system.pojo.vo.RouterVo;
 import com.storlead.system.pojo.vo.UserMenuVO;
 import com.storlead.system.service.IMenuService;
+import com.storlead.system.service.SysAppService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.log4j.Log4j2;
@@ -21,6 +25,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @program: sp-sales-platform
@@ -37,6 +42,10 @@ public class PermissionResApiController {
 
     @Resource
     private IMenuService menuService;
+
+    @Resource
+    private SysAppService sysAppService;
+
     private static final String PARENT_VIEW = "ParentView";
     private static final String LAYOUT = "Layout";
 
@@ -47,25 +56,14 @@ public class PermissionResApiController {
             Map<String,Object> map = new HashMap();
             Long currentUserId = LoginUserUtil.getCurrentUserId();
             List<MenuEntity> userMenuList = menuService.getUserMenuList(currentUserId,Arrays.asList(0),Arrays.asList(1,2));
-            List<UserMenuVO> menuls = new ArrayList();
-
-            userMenuList.forEach(menu -> {
-                UserMenuVO menuVo = new UserMenuVO();
-                BeanUtils.copyProperties(menu,menuVo);
-                menuls.add(menuVo);
-            });
-            if (!CollectionUtils.isEmpty(menuls)) {
-                menuls.forEach(e -> {
-                    if (StrUtil.isNotBlank(e.getMenuName())) {
-                        String pyIcon = PingyinUtil.getLowFullSpell(e.getMenuName());
-                        e.setIcon(pyIcon);
-                    }
-                });
-            }
-            List<UserMenuVO>  menuRes = createTreeMenus(Long.valueOf(0),menuls);
-            List<RouterVo> treeRouter = new ArrayList<>();
-            menuToRouter(menuRes, null, treeRouter);
-            map.put("routerVo",treeRouter);
+            List<UserMenuVO> menuls = toUserMenuVoList(userMenuList);
+            List<AppRouterVO> appRouterList = buildAppRouterList(menuls);
+            List<RouterVo> treeRouter = appRouterList.stream()
+                    .filter(app -> !CollectionUtils.isEmpty(app.getChildren()))
+                    .flatMap(app -> app.getChildren().stream())
+                    .collect(Collectors.toList());
+            map.put("appRouterVo", appRouterList);
+//            map.put("routerVo", treeRouter);
             return Result.result(map);
         }catch (Exception e) {
             e.printStackTrace();
@@ -106,6 +104,88 @@ public class PermissionResApiController {
             return Result.error("获取菜单信息错误:"+e.getMessage());
         }
     }
+    /**
+     * 按应用分组构建菜单路由:应用 -> 菜单树。
+     */
+    private List<AppRouterVO> buildAppRouterList(List<UserMenuVO> menuls) {
+        if (CollectionUtils.isEmpty(menuls)) {
+            return Collections.emptyList();
+        }
+        Map<Long, List<UserMenuVO>> menusByApp = menuls.stream()
+                .collect(Collectors.groupingBy(menu -> menu.getAppId() == null ? 0L : menu.getAppId()));
+
+        Set<Long> appIds = menusByApp.keySet();
+        List<SysAppEntity> apps = sysAppService.list(new LambdaQueryWrapper<SysAppEntity>()
+                .in(SysAppEntity::getId, appIds)
+                .eq(SysAppEntity::getIsDelete, 0)
+                .eq(SysAppEntity::getEnabled, 1)
+                .orderByAsc(SysAppEntity::getSort)
+                .orderByAsc(SysAppEntity::getId));
+
+        List<AppRouterVO> appRouterList = new ArrayList<>();
+        Set<Long> handledAppIds = new HashSet<>();
+
+        for (SysAppEntity app : apps) {
+            List<UserMenuVO> appMenus = menusByApp.get(app.getId());
+            if (CollectionUtils.isEmpty(appMenus)) {
+                continue;
+            }
+            handledAppIds.add(app.getId());
+            appRouterList.add(buildOneAppRouter(app, appMenus));
+        }
+
+        for (Long appId : appIds) {
+            if (handledAppIds.contains(appId)) {
+                continue;
+            }
+            List<UserMenuVO> appMenus = menusByApp.get(appId);
+            if (CollectionUtils.isEmpty(appMenus)) {
+                continue;
+            }
+            AppRouterVO appRouter = new AppRouterVO();
+            appRouter.setAppId(appId);
+            appRouter.setAppName("应用" + appId);
+            appRouter.setChildren(buildMenuRouters(appMenus));
+            appRouterList.add(appRouter);
+        }
+        return appRouterList;
+    }
+
+    private AppRouterVO buildOneAppRouter(SysAppEntity app, List<UserMenuVO> appMenus) {
+        AppRouterVO appRouter = new AppRouterVO();
+        appRouter.setAppId(app.getId());
+        appRouter.setAppCode(app.getAppCode());
+        appRouter.setAppName(app.getAppName());
+        appRouter.setAppTitle(app.getAppTitle());
+        appRouter.setIcon(app.getIcon());
+        appRouter.setHomePath(app.getHomePath());
+        appRouter.setChildren(buildMenuRouters(appMenus));
+        return appRouter;
+    }
+
+    private List<RouterVo> buildMenuRouters(List<UserMenuVO> appMenus) {
+        List<UserMenuVO> menuTree = createTreeMenus(0L, appMenus);
+        List<RouterVo> routers = new ArrayList<>();
+        menuToRouter(menuTree, null, routers);
+        return routers;
+    }
+
+    private List<UserMenuVO> toUserMenuVoList(List<MenuEntity> userMenuList) {
+        if (CollectionUtils.isEmpty(userMenuList)) {
+            return Collections.emptyList();
+        }
+        List<UserMenuVO> menuls = new ArrayList<>();
+        userMenuList.forEach(menu -> {
+            UserMenuVO menuVo = new UserMenuVO();
+            BeanUtils.copyProperties(menu, menuVo);
+            if (StrUtil.isNotBlank(menuVo.getMenuName())) {
+                menuVo.setIcon(PingyinUtil.getLowFullSpell(menuVo.getMenuName()));
+            }
+            menuls.add(menuVo);
+        });
+        return menuls;
+    }
+
     /**
      * 是否为parent_view组件
      */

+ 12 - 1
java/storlead-system/storlead-system-biz/src/main/java/com/storlead/system/service/impl/MenuServiceImpl.java

@@ -6,6 +6,7 @@ import com.storlead.system.mapper.MenuMapper;
 import com.storlead.system.service.IMenuService;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
 import java.util.List;
 
 @Service
@@ -23,7 +24,7 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuEntity> impleme
 
     @Override
     public List<MenuEntity> getUserMenuList(Long userId,Long appId,List<Integer> modeTypes,List<Integer> menuTypes) {
-        return this.baseMapper.selectUserMenuList(userId, appId, modeTypes ,menuTypes);
+        return this.baseMapper.selectUserMenuList(userId, appId, toArrayList(modeTypes), toArrayList(menuTypes));
     }
 
     @Override
@@ -42,4 +43,14 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuEntity> impleme
         return this.baseMapper.selectMenuCreateTree();
     }
 
+    /**
+     * MyBatis foreach 在 Java 17+ 下无法反射访问 Arrays$ArrayList,需转为 ArrayList。
+     */
+    private static <T> List<T> toArrayList(List<T> source) {
+        if (source == null || source.isEmpty()) {
+            return source;
+        }
+        return source instanceof ArrayList ? source : new ArrayList<>(source);
+    }
+
 }

+ 36 - 0
java/storlead-system/storlead-system-core/src/main/java/com/storlead/system/pojo/vo/AppRouterVO.java

@@ -0,0 +1,36 @@
+package com.storlead.system.pojo.vo;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * 用户可访问的应用及其菜单路由。
+ */
+@Data
+@JsonInclude(JsonInclude.Include.NON_EMPTY)
+public class AppRouterVO {
+
+    @ApiModelProperty(value = "应用 ID,对应 sys_app.id")
+    private Long appId;
+
+    @ApiModelProperty(value = "应用编码")
+    private String appCode;
+
+    @ApiModelProperty(value = "应用名称")
+    private String appName;
+
+    @ApiModelProperty(value = "前台标题")
+    private String appTitle;
+
+    @ApiModelProperty(value = "应用图标")
+    private String icon;
+
+    @ApiModelProperty(value = "默认首页路由")
+    private String homePath;
+
+    @ApiModelProperty(value = "应用下的菜单路由树")
+    private List<RouterVo> children;
+}