谷粒商城 页面展示数据 网关+跨域+element-ui使用+后端对数据的树形查询
·
树形查询所有数据
@Override
public List<CategoryEntity> listWithTree() {
List<CategoryEntity> categoryEntities = categoryDao.selectList(null);
// 一级分类
return categoryEntities.stream()
.filter(categoryEntity -> categoryEntity.getParentCid() == 0) // 过滤出一级目录
.peek(categoryEntity -> categoryEntity.setChildren(getChildren(categoryEntity, categoryEntities)))//为每个目录添加子目录
.sorted(Comparator.comparingInt(e -> (e.getSort() == null ? 0 : e.getSort())))//排序(注意null)
.collect(Collectors.toList());// 收集流到集合中返回
}
private List<CategoryEntity> getChildren(CategoryEntity f, List<CategoryEntity> all) {// 递归查找
if (f.getCatLevel() == 3) return null;
if(f.getChildren()!=null) return f.getChildren();
return all.stream()
.filter(categoryEntity -> Objects.equals(categoryEntity.getParentCid(), f.getCatId()))
.peek(categoryEntity -> categoryEntity.setChildren(getChildren(categoryEntity,all)))
.sorted(Comparator.comparingInt(e -> (e.getSort() == null ? 0 : e.getSort())))
.collect(Collectors.toList());
}
element-ui
需求:利用element-ui提供的组件实现树形展示的效果

- 添加标签
- 添加脚本

3. 对data进行处理
http返回的是整个数据,需要将data中的数据取出来,然后传到组件中

第一层data代表所有的数据
第二层data是方法返回的R类,它是以键值对的方式储存查询到的数据
第三层data是真正的数据
网关
需求:将所有请求转发到网关上,最后由网关负责分发到指定微服务上
前端配置发送路径
- 配置所有前端请求的前缀是api/**

- 配置请求的后缀

网关
配置网关路由,让网关根据需求发送到指定微服务
gateway:
routes:
- id: test_route
uri: https://www.baidu.com
predicates:
- Query=url,baidu
- id: qq_route
uri: https://www.qq.com
predicates:
- Query=url,qq
- id: product_category
uri: lb://product
predicates:
- Path=/api/product/**
filters: # 通过过滤器改变请求前缀,/api/** ---> /
- RewritePath=/api/(?<segment>.*),/$\{segment}
- id: admin_route
uri: lb://renren-fast
predicates:
- Path=/api/**
filters:
- RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}
跨域请求
请求页面的url和请求的url,只要有 协议,域名,端口号 任意一处不同就是跨域请求,被拒绝,然后403。
解决:每次跨域请求都会先发出一个option请求,询问是否可以跨域,我们要做的就是响应允许跨域
- 直接在网关配置允许跨域,然后删除人人开源自带的跨域处理,在config文件夹下
globalcors:
cors-configurations:
'[/**]':
allowCredentials: true
allowedHeaders: "*"
allowedOrigins: "*"
allowedMethods: "*"
@Configuration
public class CorsConfig implements WebMvcConfigurer {
// 注释掉
// @Override
// public void addCorsMappings(CorsRegistry registry) {
// registry.addMapping("/**")
//
// .allowCredentials(true)
// .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
// .maxAge(3600);
// }
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)