将 pms_catelog.sql的语句在pms.catagory中执行。
一级菜单-》二级菜单-》三级菜单
在服务中查询商品分类,以树形结构返回。
找到catagorycontroler
修改/list 为 /list/tree
新增方法
catagoryService.listWithTree();
会在接口中新增方法,实现类中需要覆盖该方法,
@Override
public List<CategoryEntity> listWithTree() {
// 1.查询商品分类信息
List<CategoryEntity> entities = baseMapper.selectList(null);
// 2.以树形结构返回
List<CategoryEntity> level1Entites = entities.stream().filter((entitie) -> {
return entitie.getParentCid() == 0;
}).map((menu)->{
menu.setChildren(getChilderns(menu, entities));
return menu;
}).sorted((menu1, menu2)->{
return (menu1.getSort() == null ? 0 : menu1.getSort())- (menu2.getSort() == null ? 0 : menu2.getSort());
}).collect(Collectors.toList());
return level1Entites;
}
商品类entity中添加子类属性:
@TableField(exist=false)
private List<CategoryEntity> children;
// 递归查找子菜单
private List<CategoryEntity> getChilderns(CategoryEntity root, List<CategoryEntity> all) {
List<CategoryEntity> children = all.stream().filter((entity) -> {
return entity.getParentCid() == root.getCatId();
}).map((menu)->{
menu.setChildren(getChilderns(menu, all));
return menu;
}).sorted((menu1, menu2)->{
return (menu1.getSort() == null ? 0 : menu1.getSort())- (menu2.getSort() == null ? 0 : menu2.getSort());
}).collect(Collectors.toList());
return children;
}
利用人人fast后台管理系统,新增商品系统-》分类维护, 目录-》菜单。
为分类维护菜单添加 路由:product/category
renren-fast-vue中src/views/modules 新建一个product文件夹,创建category.vue文件
创建vue组件,套用elementui树形控件。模仿调用后端api模板,请求/product/category/list/tree
的url
由于renrenfast-vue调用的是renrenfast,而不是谷粒product服务,因此将renrenfast注册到gateway中,通过88端口网关来访问url才能生效。
配置网关路由:
spring:
cloud:
gateway:
routes:
- id: admin-route
uri: lb://renren-fast
predicates:
- Path=/api/
filters:
*- RewritePath=/api/(?
同时修改renren-fast-vue中baseurl:’http://localhost:88/api‘;
再次访问前端项目,可以获取验证码,但无法登录
Access to XMLHttpRequest at ‘http://localhost:88/api/sys/login‘ from origin ‘http://localhost:8001‘ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
原因:跨域请求不被允许
解决跨域:
1.nginx部署为同一域
2.服务器配置响应头
gateway同一配置 ,config/
@Configuration
public class GulimallCorsConfiguration {
@Bean
public CorsWebFilter corsWebFilter(){
UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
// 1.配置跨域
config.addAllowedHeader("*");
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.setAllowCredentials(true);
corsConfigurationSource.registerCorsConfiguration("/**", config);
return new CorsWebFilter(corsConfigurationSource);
}
}
renren-fast中也配置了跨域,需要注释
gateway需要修改商品服务路由,同时需要将商品服务配置到配置中心。
三级商品菜单删除功能:
mybait plus 逻辑删除
前端删除部分…
品牌管理: