后端代码返回子路由配置信息显示错误
我现在返回json数据是这样的
{
"code": 1000,
"message": "successful",
"data": [
{
"path": "/",
"name": "index",
"component": "/index/Index.vue",
"children": [
{
"path": "/test",
"name": "test",
"component": "/test.vue",
"children": [
{
"path": "/test1",
"name": "test1",
"component": "/test1.vue"
}
]
}
],
"meta": {
"title": "江西工业职业技术学院"
}
},
{
"path": "/news/newsList",
"name": "newsList",
"component": "/news-list/NewsList.vue",
"meta": {
"title": "江西工业职业技术学院-新闻列表"
}
},
{
"path": "/news/newsContent",
"name": "newsContent",
"component": "/content/Content.vue",
"meta": {
"title": "江西工业技术学院-新闻详情"
}
},
{
"path": "/admin",
"name": "admin",
"component": "/admin/index/Index.vue",
"meta": {
"title": "后台管理系统-首页"
}
},
{
"path": "/admin/student/studentList",
"name": "studentList",
"component": "/admin/student/StudentList.vue",
"meta": {
"title": "后台管理系统-学生列表"
}
},
{
"path": "/admin/grade/gradeList",
"name": "gradeList",
"component": "/admin/grade/GradeList.vue",
"meta": {
"title": "后台管理系统-班级列表"
}
},
{
"path": "/admin/college/collegeList",
"name": "collegeList",
"component": "/admin/college/CollegeList.vue",
"meta": {
"title": "后台管理系统-学院列表"
}
},
{
"path": "/admin/major/majorList",
"name": "majorList",
"component": "/admin/major/MajorList.vue",
"meta": {
"title": "后台管理系统-专业列表"
}
},
{
"path": "/test",
"name": "test",
"component": "/test.vue",
"children": [
{
"path": "/test1",
"name": "test1",
"component": "/test1.vue"
}
]
},
{
"path": "/test1",
"name": "test1",
"component": "/test1.vue"
}
]
}
children字段有重复,并且子路由的meta跟父路由的meta属性完全一模一样时就只显示父路由的meta,我的Java代码是这样写的
@Service
@Slf4j
public class RouterServiceImpl implements RouterService {
@Resource
private RouterMapper routerMapper;
@Resource
private MetaMapper metaMapper;
@Override
public List<RouterDto> getRouter(Integer jurisdictionId) {
if (NumberUtil.isEmpty(jurisdictionId)) {
throw new CustomException(ResultEnum.notParamError.getCode(),ResultEnum.notParamError.getMessage());
}
if (jurisdictionId <= 0) {
jurisdictionId = 5;
}
List<RouterDto> routerDtoList = routerMapper.getRouter(jurisdictionId);
List<Integer> metaIds = new ArrayList<>();
for (int i = 0; i < routerDtoList.size(); i++) {
RouterDto routerDto = routerDtoList.get(i);
if (!ObjectUtil.isEmpty(routerDto) && !NumberUtil.isEmpty(routerDto.getParentId()) && !Objects.equals(routerDto.getParentId(),StatusCode.status.getCode())) {
// 获取父路由
RouterDto parentRouter = this.getParentRouter(routerDtoList, routerDto.getParentId());
if (!ObjectUtil.isEmpty(parentRouter)) {
// 创建一个新的list保存子路由
List<RouterDto> children = new ArrayList<>();
children.add(routerDto);
// 删除多余的当前元素 这一段不加就是上面json显示的样子,加了也只是去掉了上面json的test那一个并且children还少了,就像下面的json一样
routerDtoList.remove(i);
// 设置子路由进父路由
parentRouter.setChildren(children);
}
}
metaIds.add(routerDto.getMetaId());
}
List<Meta> batchMetaById = metaMapper.getBatchMetaById(metaIds);
for (int i = 0; i < routerDtoList.size(); i++) {
RouterDto routerDto = routerDtoList.get(i);
if (i < batchMetaById.size()) {
routerDto.setMeta(batchMetaById.get(i));
}
}
return routerDtoList;
}
/**
* 获取父路由
* @param routerDtoList 路由列表
* @param parentId 子路由的父路由id
* @return 父路由
* @author guli
* @date 2023-10-23
*/
private RouterDto getParentRouter(List<RouterDto> routerDtoList,Integer parentId) {
for (int i = 0; i < routerDtoList.size(); i++) {
RouterDto routerDto = routerDtoList.get(i);
if (Objects.equals(routerDto.getId(), parentId)) {
return routerDto;
}
}
return null;
}
}
写了删除当前路由的那段代码的json结果
{
"code": 1000,
"message": "successful",
"data": [
{
"path": "/",
"name": "index",
"component": "/index/Index.vue",
"children": [
{
"path": "/test",
"name": "test",
"component": "/test.vue"
}
],
"meta": {
"title": "江西工业职业技术学院"
}
},
{
"path": "/news/newsList",
"name": "newsList",
"component": "/news-list/NewsList.vue",
"meta": {
"title": "江西工业职业技术学院-新闻列表"
}
},
{
"path": "/news/newsContent",
"name": "newsContent",
"component": "/content/Content.vue",
"meta": {
"title": "江西工业技术学院-新闻详情"
}
},
{
"path": "/admin",
"name": "admin",
"component": "/admin/index/Index.vue",
"meta": {
"title": "后台管理系统-首页"
}
},
{
"path": "/admin/student/studentList",
"name": "studentList",
"component": "/admin/student/StudentList.vue",
"meta": {
"title": "后台管理系统-学生列表"
}
},
{
"path": "/admin/grade/gradeList",
"name": "gradeList",
"component": "/admin/grade/GradeList.vue",
"meta": {
"title": "后台管理系统-班级列表"
}
},
{
"path": "/admin/college/collegeList",
"name": "collegeList",
"component": "/admin/college/CollegeList.vue",
"meta": {
"title": "后台管理系统-学院列表"
}
},
{
"path": "/admin/major/majorList",
"name": "majorList",
"component": "/admin/major/MajorList.vue",
"meta": {
"title": "后台管理系统-专业列表"
}
},
{
"path": "/test1",
"name": "test1",
"component": "/test1.vue"
}
]
}