feat: 菜单添加路由参数设置
This commit is contained in:
@@ -4,20 +4,23 @@ import com.youlai.system.model.entity.SysMenu;
|
||||
import com.youlai.system.model.form.MenuForm;
|
||||
import com.youlai.system.model.vo.MenuVO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
/**
|
||||
* 菜单对象转换器
|
||||
*
|
||||
* @author haoxr
|
||||
* @since 2022/7/29
|
||||
* @author Ray Hao
|
||||
* @since 2024/5/26
|
||||
*/
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface MenuConverter {
|
||||
|
||||
MenuVO entity2Vo(SysMenu entity);
|
||||
|
||||
@Mapping(target = "params", ignore = true)
|
||||
MenuForm convertToForm(SysMenu entity);
|
||||
|
||||
@Mapping(target = "params", ignore = true)
|
||||
SysMenu convertToEntity(MenuForm menuForm);
|
||||
|
||||
}
|
||||
@@ -78,4 +78,9 @@ public class RouteBO {
|
||||
*/
|
||||
private Integer keepAlive;
|
||||
|
||||
/**
|
||||
* 【菜单】路由参数
|
||||
*/
|
||||
private String params;
|
||||
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.youlai.system.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
|
||||
import com.youlai.system.common.base.BaseEntity;
|
||||
import com.youlai.system.common.enums.MenuTypeEnum;
|
||||
@@ -91,7 +89,7 @@ public class SysMenu extends BaseEntity {
|
||||
/**
|
||||
* 路由参数
|
||||
*/
|
||||
@TableField(updateStrategy = FieldStrategy.ALWAYS)
|
||||
private String params;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.youlai.system.model.form;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.youlai.system.common.enums.MenuTypeEnum;
|
||||
import com.youlai.system.common.model.KeyValue;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Schema(description = "菜单表单对象")
|
||||
@Data
|
||||
@@ -52,6 +52,6 @@ public class MenuForm {
|
||||
private Integer alwaysShow;
|
||||
|
||||
@Schema(description = "路由参数")
|
||||
private List<KeyValue> paramList;
|
||||
private List<KeyValue> params;
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 菜单路由视图对象
|
||||
@@ -55,6 +56,9 @@ public class RouteVO {
|
||||
@Schema(description = "【目录】只有一个子路由是否始终显示", example = "true")
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private Boolean alwaysShow;
|
||||
|
||||
@Schema(description = "路由参数")
|
||||
private Map<String,String> params;
|
||||
}
|
||||
|
||||
@Schema(description = "子路由列表")
|
||||
|
||||
@@ -8,6 +8,8 @@ import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.youlai.system.common.constant.SystemConstants;
|
||||
import com.youlai.system.common.enums.MenuTypeEnum;
|
||||
import com.youlai.system.common.enums.StatusEnum;
|
||||
@@ -31,6 +33,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -141,7 +144,8 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
@Cacheable(cacheNames = "menu", key = "'routes'")
|
||||
public List<RouteVO> listRoutes() {
|
||||
List<RouteBO> menuList = this.baseMapper.listRoutes();
|
||||
return buildRoutes(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
List<RouteVO> routes = buildRoutes(SystemConstants.ROOT_NODE_ID, menuList);
|
||||
return routes;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,6 +195,17 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
}
|
||||
meta.setAlwaysShow(ObjectUtil.equals(routeBO.getAlwaysShow(), 1));
|
||||
|
||||
String paramsJson = routeBO.getParams();
|
||||
// 将 JSON 字符串转换为 Map<String, String>
|
||||
if (StrUtil.isNotBlank(paramsJson)) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
Map<String, String> paramMap = objectMapper.readValue(paramsJson, new TypeReference<>() {});
|
||||
meta.setParams(paramMap);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("解析参数失败", e);
|
||||
}
|
||||
}
|
||||
routeVO.setMeta(meta);
|
||||
return routeVO;
|
||||
}
|
||||
@@ -219,9 +234,13 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
String treePath = generateMenuTreePath(menuForm.getParentId());
|
||||
entity.setTreePath(treePath);
|
||||
|
||||
List<KeyValue> paramList = menuForm.getParamList();
|
||||
if (CollectionUtil.isNotEmpty(paramList)) {
|
||||
entity.setParams(JSONUtil.toJsonStr(paramList));
|
||||
List<KeyValue> params = menuForm.getParams();
|
||||
// 路由参数 [{key:"id",value:"1"},{key:"name",value:"张三"}] 转换为 [{"id":"1"},{"name":"张三"}]
|
||||
if (CollectionUtil.isNotEmpty(params)) {
|
||||
entity.setParams(JSONUtil.toJsonStr(params.stream()
|
||||
.collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue))));
|
||||
}else{
|
||||
entity.setParams(null);
|
||||
}
|
||||
|
||||
boolean result = this.saveOrUpdate(entity);
|
||||
@@ -288,11 +307,26 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
SysMenu entity = this.getById(id);
|
||||
Assert.isTrue(entity != null, "菜单不存在");
|
||||
MenuForm formData = menuConverter.convertToForm(entity);
|
||||
// 路由参数字符串 {"id":"1","name":"张三"} 转换为 [{key:"id", value:"1"}, {key:"name", value:"张三"}]
|
||||
String params = entity.getParams();
|
||||
if (StrUtil.isNotBlank(params)) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
// 解析 JSON 字符串为 Map<String, String>
|
||||
Map<String, String> paramMap = objectMapper.readValue(params, new TypeReference<>() {});
|
||||
|
||||
if(StrUtil.isNotBlank(entity.getParams())){
|
||||
List<KeyValue> params = JSONUtil.toList(JSONUtil.parseArray(entity.getParams()), KeyValue.class);
|
||||
formData.setParamList(params);
|
||||
// 转换为 List<KeyValue> 格式 [{key:"id", value:"1"}, {key:"name", value:"张三"}]
|
||||
List<KeyValue> transformedList = paramMap.entrySet().stream()
|
||||
.map(entry -> new KeyValue(entry.getKey(), entry.getValue()))
|
||||
.toList();
|
||||
|
||||
// 将转换后的列表存入 MenuForm
|
||||
formData.setParams(transformedList);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("解析参数失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
return formData;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
<result property="type" column="type" jdbcType="OTHER"/>
|
||||
<result property="alwaysShow" column="always_show" jdbcType="INTEGER"/>
|
||||
<result property="keepAlive" column="keep_alive" jdbcType="INTEGER"/>
|
||||
<result property="params" column="params" jdbcType="VARCHAR"/>
|
||||
<collection property="roles" ofType="string" javaType="list">
|
||||
<result column="code"/>
|
||||
</collection>
|
||||
@@ -38,7 +39,8 @@
|
||||
t1.type,
|
||||
t3.code,
|
||||
t1.always_show,
|
||||
t1.keep_alive
|
||||
t1.keep_alive,
|
||||
t1.params
|
||||
FROM
|
||||
sys_menu t1
|
||||
LEFT JOIN sys_role_menu t2 ON t1.id = t2.menu_id
|
||||
|
||||
Reference in New Issue
Block a user