package com.yunhai.cms.service.impl; import com.yunhai.platform.apistd.BizException; import com.yunhai.platform.openapi.base.YschBaseOpenApi; import com.yunhai.platform.utils.exception.ErrorCode; import com.yunhai.platform.utils.model.BoaModule; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** * 用户中心代理(Notice UserCenterService 同款逻辑平移): * 模块菜单树来自基础平台 SDK(YschBaseOpenApi.getUserModule),层级由用户中心 {@code BoaModule.children} * 直接下发,本侧仅映射为前端 layout 契约(modId/page/children)。 */ @Slf4j @Service public class CmsUserCenterService { /** * 当前用户已授权模块树(左侧菜单)。 */ public List> getUserModuleTree(Integer userId, Integer schId) { if (userId == null) { throw new BizException(ErrorCode.PARAM_ERROR, "用户ID不得为空"); } List modules; try { modules = YschBaseOpenApi.getUserModule(userId, schId); } catch (Exception e) { // 平台不可达时菜单降级为空(登录/站点能力不受影响),避免整页不可用 log.warn("[cms] 获取用户模块失败, schId={}: {}", schId, e.getMessage()); return Collections.emptyList(); } if (modules == null || modules.isEmpty()) { return Collections.emptyList(); } return toFrontendTree(modules); } /** * BoaModule 树 → 前端 layout 期望的字段形状(递归,只改名不组树;Notice/svc-hall 同款口径)。
* 层级与兄弟顺序均由用户中心决定(customId 升序),本侧不重排;叶子 children 为空列表。 */ private List> toFrontendTree(List modules) { List> result = new ArrayList<>(); if (modules == null || modules.isEmpty()) { return result; } for (BoaModule m : modules) { Map node = new LinkedHashMap<>(); node.put("id", m.getId()); node.put("modId", m.getId()); node.put("name", m.getName()); node.put("parentId", m.getParentId() != null ? m.getParentId() : 0); node.put("page", m.getPath() != null ? m.getPath() : ""); node.put("level", m.getLevel() != null ? m.getLevel() : 0); node.put("icon", m.getIcon() != null ? m.getIcon() : ""); node.put("orderNumber", m.getSortNo() != null ? m.getSortNo() : 0); node.put("usable", true); node.put("children", toFrontendTree(m.getChildren())); result.add(node); } return result; } }