81 lines
3.5 KiB
Java
81 lines
3.5 KiB
Java
package com.yunhai.svchall.controller.api;
|
||
|
||
import com.yunhai.platform.openapi.client.YschBaseOpenApi;
|
||
import com.yunhai.platform.utils.model.BoaModule;
|
||
import com.yunhai.platform.utils.model.BoaUser;
|
||
import org.springframework.web.bind.annotation.GetMapping;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.Collections;
|
||
import java.util.LinkedHashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* 平台代理域接口(Notice/CMS BaseApiController 同款契约):GetUserInfo / GetUserMod。
|
||
* GetUserInfo 直接返回 CAS 会话注入的 BoaUser;GetUserMod 经 Tier1 SDK 取当前用户在
|
||
* 本应用(appKey 按 yh.open 身份)下已授权模块并组树,是前端左侧菜单与页头用户信息的数据源。
|
||
* 返回裸对象,由统一响应包装为 {code, msg, data}。
|
||
*/
|
||
@RestController
|
||
@RequestMapping("/api/base")
|
||
public class BaseApiController {
|
||
|
||
/** 当前登录用户信息(页头用户名/头像) */
|
||
@GetMapping("GetUserInfo")
|
||
public BoaUser getUserInfo(BoaUser authUserWithDept) {
|
||
return authUserWithDept;
|
||
}
|
||
|
||
/** 当前用户已授权模块列表(左侧菜单树) */
|
||
@GetMapping("GetUserMod")
|
||
public List<Map<String, Object>> getUserMod(BoaUser authUserWithDept) {
|
||
Integer schId = authUserWithDept.getCurrentRole() != null
|
||
? authUserWithDept.getCurrentRole().getSchId() : null;
|
||
List<BoaModule> modules = YschBaseOpenApi.getUserModule(authUserWithDept.getId(), schId);
|
||
if (modules == null || modules.isEmpty()) {
|
||
return Collections.emptyList();
|
||
}
|
||
return buildModuleTree(modules);
|
||
}
|
||
|
||
/** 扁平模块列表组树(Notice UserCenterService.buildModuleTree 同构:modId/page/children) */
|
||
private static List<Map<String, Object>> buildModuleTree(List<BoaModule> modules) {
|
||
Map<Integer, BoaModule> moduleMap = new LinkedHashMap<>();
|
||
for (BoaModule m : modules) {
|
||
moduleMap.put(m.getId(), m);
|
||
}
|
||
Map<Integer, Map<String, Object>> nodeMap = new LinkedHashMap<>();
|
||
for (BoaModule m : modules) {
|
||
Map<String, Object> 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", new ArrayList<>());
|
||
nodeMap.put(m.getId(), node);
|
||
}
|
||
List<Map<String, Object>> roots = new ArrayList<>();
|
||
for (BoaModule m : modules) {
|
||
Map<String, Object> node = nodeMap.get(m.getId());
|
||
int parentId = m.getParentId() != null ? m.getParentId() : 0;
|
||
if (parentId == 0 || !moduleMap.containsKey(parentId)) {
|
||
roots.add(node);
|
||
} else {
|
||
@SuppressWarnings("unchecked")
|
||
List<Map<String, Object>> children =
|
||
(List<Map<String, Object>>) nodeMap.get(parentId).get("children");
|
||
children.add(node);
|
||
}
|
||
}
|
||
return roots;
|
||
}
|
||
}
|