数据库表结构如下:
public class MiniProgramAction {
@TableId
private Integer id;
private Integer parentId = 0;
private String actionName = ""; // 动作名称
private String icon = ""; // 图标
private String href = ""; // 链接地址
private Integer sort = 0;
private Byte type; // 菜单类型 0首页,1 应用
public MiniProgramAction() {
}
public MiniProgramAction(Integer parentId, String actionValue, String actionName, Integer sort) {
this.parentId = parentId;
this.actionValue = actionValue;
this.actionName = actionName;
this.sort = sort;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getActionName() {
return actionName;
}
public void setActionName(String actionName) {
this.actionName = actionName;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public String getActionValue() {
return actionValue;
}
public void setActionValue(String actionValue) {
this.actionValue = actionValue;
}
public Byte getType() {
return type;
}
public void setType(Byte type) {
this.type = type;
}
}
业务数据
public class GetMenusVo {
private long id; // 菜单ID
private String mname; // 菜单名称
private String icon; // 菜单图标
private String href;
private long parentId; // 父菜单ID
private byte type; // 菜单类型 0:首页 1 应用
public List<GetMenusVo> subs; // 子菜单列表
public GetMenusVo(long id, String mname, String icon, String href, long parentId, byte type) {
this.id = id;
this.mname = mname;
this.icon = icon;
this.href = href;
this.parentId = parentId;
this.type = type;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public long getParentId() {
return parentId;
}
public void setParentId(long parentId) {
this.parentId = parentId;
}
public byte getType() {
return type;
}
public void setType(byte type) {
this.type = type;
}
public List<GetMenusVo> getSubs() {
return subs;
}
public void setSubs(List<GetMenusVo> subs) {
this.subs = subs;
}
}
Java 业务处理
@Override
public List<GetMenusVo> getMenus() {
int roleId = PmasThreadLocalUtil.get().getRoleId(); // 获取当前用户的角色id
// 获取角色授权菜单
List<MiniProgramAction> menuList = miniProgramActionMapper.findByRoleId(roleId);
List<GetMenusVo> menus = menuList.stream().map(e -> new GetMenusVo(e.getId(), e.getActionName(), e.getIcon(), e.getHref(),
e.getParentId(), e.getType())).collect(Collectors.toList());
//获取父节点
return menus.stream().filter(m -> m.getParentId() == 0).peek(m -> m.setSubs(getSubs(m, menus))).collect(Collectors.toList());
}
/**
* 查询子节点
*/
private List<GetMenusVo> getSubs(GetMenusVo root, List<GetMenusVo> menus) {
return menus.stream().filter(m -> {
return Objects.equals(m.getParentId(), root.getId());
}).peek(m -> m.setSubs(getSubs(m, menus))).collect(Collectors.toList());
}
controller
/**
* 获取应用菜单
*/
@PostMapping("/getMenus")
public List<GetMenusVo> getMenus() {
return pmasUserService.getMenus();
}
调用响应:
[{"id":1000,"mname":"首页","href":null,"parentId":0,"subs":[]},{"id":2000,"mname":"概览","href":null,"parentId":0,"subs":[{"id":2001,"mname":"收入概览","href":null,"parentId":2000,"subs":[]},{"id":2002,"mname":"车流概览","href":null,"parentId":2000,"subs":[]},{"id":2003,"mname":"固定车辆概览","href":null,"parentId":2000,"subs":[]},{"id":2004,"mname":"监控","href":null,"parentId":2000,"subs":[]}]},{"id":3000,"mname":"应用","href":null,"parentId":0,"subs":[{"id":3001,"mname":"车辆","href":null,"parentId":3000,"subs":[]},{"id":3002,"mname":"车辆","href":null,"parentId":3000,"subs":[]},{"id":3003,"mname":"车主","href":null,"parentId":3000,"subs":[]}]}