首先要修改一下前端的category-cascader.vue和brand-select.vue文件,需要导入pubsub-js做组件间的通信,如下:
然后js还会报一个错误,修改category-cascader.vue文件,把原来的this去了,如下:
brand-select.vue文件也一样,把原来的this去了,如下:
接着添加后端接口:
CategoryBrandRelationController类添加如下接口:
/**
* 查询分类关联的品牌
*
* @param catId
* @return
*/
@GetMapping("/brands/list")
public R relationBrandsList(@RequestParam(value = "catId", required = true) Long catId) {
List<BrandEntity> vos = categoryBrandRelationService.getBrandsByCatId(catId);
List<BrandVo> collect = vos.stream().map(item -> {
BrandVo brandVo = new BrandVo();
brandVo.setBrandId(item.getBrandId());
brandVo.setBrandName(item.getName());
return brandVo;
}).collect(Collectors.toList());
return R.ok().put("data", collect);
}
CategoryBrandRelationService接口添加如下方法:
/**
* 查询分类关联的品牌
*
* @param catId
* @return
*/
List<BrandEntity> getBrandsByCatId(Long catId);
CategoryBrandRelationServiceImpl类添加方法的实现,如下:
@Autowired
private CategoryBrandRelationDao relationDao;
@Autowired
private BrandService brandService;
@Override
public List<BrandEntity> getBrandsByCatId(Long catId) {
List<CategoryBrandRelationEntity> categoryBrandRelationEntities = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
List<BrandEntity> collect = categoryBrandRelationEntities.stream().map(item -> {
Long brandId = item.getBrandId();
BrandEntity brandEntity = brandService.getById(brandId);
return brandEntity;
}).collect(Collectors.toList());
return collect;
}
重启服务测试,效果如下:
可以发现在发布商品模块中,选择了手机1分类,可以相关品牌华为1了。
查询分类关联的品牌功能完成。