- 分布式基础篇
- csdn机制问题导致图片丢失,可以查看本人的个人博客:[谷粒商城-P45-P58](https://www.r2coding.vip/articles/2022/05/04/1651673906725.html)
- 1. 三级分类
- 1.1 sql脚本
- 1.2 查出所有分类以及子分类
- 1.2.1 后段代码编写
- 1.2.2前端代码编写
- 1.2.2.1配置网关路由与路径重写
- 1.2.2.2前端代码
- 1.2.3renren-fast相关报错
- javax.validation.constraints不存在
- 1.2.4 请求验证码接口返回200,但是不显示出来
- 1.2.5 跨域问题
- 1.2.5.1 简单请求
- 1.2.6解决跨域
- 1.2.6.1 跨域流程
- 1.2.6.2 方式一: 使用nginx部署为同一域
- 1.2.6.3 方式二: 配置当次请求允许跨域
- 1.3 三级分类-查询-树形结构
- 1.3.1 将 **gulimall-product** 注册到nacos 中 具体配置如下:
- 1.4 前端代码,树形结构的增删改查(P51-P58)
- 1.5 后端代码,树形结构增删改查
三级分类数据脚本
1.2 查出所有分类以及子分类 1.2.1 后段代码编写在gulimall-product的src/main/java/com/rabbitboss/gulimall/product/controller/CategoryController.java`➕以下代码
@RequestMapping("/list/tree")
//@RequiresPermissions("product:category:list")
public R list(@RequestParam Map params){
// PageUtils page = categoryService.queryPage(params);
List entities = categoryService.listWithTree();
return R.ok().put("data", entities);
}
src/main/java/com/rabbitboss/gulimall/product/service/CategoryService.java添加以下代码
ListlistWithTree();
src/main/java/com/rabbitboss/gulimall/product/service/impl/CategoryServiceImpl.java添加以下代码
@Override
public List listWithTree() {
//1.查出所有分类
List entities = baseMapper.selectList(null);
//2。组装成父子树状结构
//2.1找到所有的一级分类
List level1Menu = entities.stream().filter(categoryEntity ->
categoryEntity.getParentCid() == 0
).map(menu -> {
//1。找到子分类
menu.setChildren(getChildrens(menu, entities));
return menu;
}).sorted((menu1, menu2) ->
//2。排序
(menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort())
).collect(Collectors.toList());
return level1Menu;
}
private List getChildrens(CategoryEntity root, List all) {
List children = all.stream().filter((categoryEntity) -> {
return categoryEntity.getParentCid() == root.getCatId();
}).map(categoryEntity -> {
//1。找到子分类
categoryEntity.setChildren(getChildrens(categoryEntity, all));
return categoryEntity;
}).sorted((menu1, menu2) ->
//2。排序
(menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort())
).collect(Collectors.toList());
return children;
}
1.2.2前端代码编写
1.2.2.1配置网关路由与路径重写
接着操作后台 localhost:8001 , 点击系统管理,菜单管理,新增 目录 商品系统 一级菜单
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a2SMyzJ5-1651674156447)(https://b3logfile.com/file/2022/05/solo-fetchupload-2215375846762372900-191c3030.png)]
1.2.2.2前端代码注意地址栏http://localhost:8001/#/product-category 可以注意到product-category我们的/被替换为了-
比如sys-role具体的视图在renren-fast-vue/views/modules/sys/role.vue
所以要自定义我们的product/category视图的话,就是创建mudules/product/category.vue
他要给8080发请求读取数据,但是数据是在10000端口上,如果找到了这个请求改端口那改起来很麻烦。
-
方法1是改vue项目里的全局配置
-
方法2是搭建个网关,让网关路由到10000
Ctrl+Shift+F全局搜索 在static/config/index.js里 window.SITE_CONFIG['baseUrl'] = 'http://localhost:88/api'; 接着让重新登录http://localhost:8001/#/login,验证码是请求88的,所以不显示。而验证码是来源于fast后台的 现在的验证码请求路径为,http://localhost:88/api/captcha.jpg 原始的验证码请求路径:http://localhost:8001/renren-fast/captcha.jpg
-
88是GateWay的端口
那么怎么将88:/api转发到renren-fast上呢?看下面的步骤
# 1.在renren-fast模块中引入common模块 ➡️ pom.xml中引入common,因为common中集成了Nacos# 2.在renren-fast模块的application.yml文件中添加以下内容 application: name: renren-fast cloud: nacos: discovery: server-addr: 127.0.0.1:8848 # 3.在renren-fast的启动类上加上注解 @EnableDiscoveryClient # 4在gulimall-gateway的application.yml添加以下代码 spring: cloud: gateway: routes: - id: admin_route uri: lb://renren-fast predicates: - Path=/api @Configuration public class GulimallCorsConfiguration { @Bean public CorsWebFilter corsWebFilter(){ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration corsConfiguration = new CorsConfiguration(); // 1.配置跨域 corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); // corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedOriginPattern("*"); corsConfiguration.setAllowCredentials(true); source.registerCorsConfiguration("*.xml global-config: db-config: id-type: auto server: port: 10000 com.rabbitboss.gulimall >gulimall-common0.0.1-SNAPSHOT
- 在统计目录下创建 bootstrap.properties配置 nacos 以及绑定命名空间 id
spring.application.name=gulimall-product spring.cloud.nacos.config.server-addr=127.0.0.1:8848 spring.cloud.nacos.config.namespace=f0882d03-cf30-47f9-92e5-19a362b937b6
- 在 gulimall-product 模块的启动类加上 @EnableDiscoveryClient这个注解
前面在解决跨域问题的时候已经加了只要是 api 打头的请求都会转发到 renren-fast模块
- id: admin_route
uri: lb://renren-fast
predicates:
- Path=/api
@RequestMapping("/list/tree")
//@RequiresPermissions("product:category:list")
public R list(@RequestParam Map params){
// PageUtils page = categoryService.queryPage(params);
List entities = categoryService.listWithTree();
return R.ok().put("data", entities);
}
@RequestMapping("/info/{catId}")
//@RequiresPermissions("product:category:info")
public R info(@PathVariable("catId") Long catId){
CategoryEntity category = categoryService.getById(catId);
return R.ok().put("data", category);
}
@RequestMapping("/save")
//@RequiresPermissions("product:category:save")
public R save(@RequestBody CategoryEntity category){
categoryService.save(category);
return R.ok();
}
@RequestMapping("/update")
//@RequiresPermissions("product:category:update")
public R update(@RequestBody CategoryEntity category){
categoryService.updateById(category);
return R.ok();
}
@RequestMapping("/update/sort")
//@RequiresPermissions("product:category:update")
public R update(@RequestBody CategoryEntity[] category){
categoryService.updateBatchById(Arrays.asList(category));
return R.ok();
}
@RequestMapping("/delete")
//@RequiresPermissions("product:category:delete")
public R delete(@RequestBody Long[] catIds){
// 1.检查当前删除的菜单,是否在别的地方引用
// categoryService.removeByIds(Arrays.asList(catIds));
categoryService.removeMenuByIds(Arrays.asList(catIds));
return R.ok();
}
}
- service
public interface CategoryService extends IService{ PageUtils queryPage(Map params); List listWithTree(); void removeMenuByIds(List asList); }
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl implements CategoryService {
@Override
public PageUtils queryPage(Map params) {
IPage page = this.page(
new Query().getPage(params),
new QueryWrapper()
);
return new PageUtils(page);
}
@Override
public List listWithTree() {
//1.查出所有分类
List entities = baseMapper.selectList(null);
//2。组装成父子树状结构
//2.1找到所有的一级分类
List level1Menu = entities.stream().filter(categoryEntity ->
categoryEntity.getParentCid() == 0
).map(menu -> {
//1。找到子分类
menu.setChildren(getChildrens(menu, entities));
return menu;
}).sorted((menu1, menu2) ->
//2。排序
(menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort())
).collect(Collectors.toList());
return level1Menu;
}
@Override
public void removeMenuByIds(List asList) {
//TODO 1.检查当前删除的菜单,是否在别的地方引用
//逻辑删除
baseMapper.deleteBatchIds(asList);
}
private List getChildrens(CategoryEntity root, List all) {
List children = all.stream().filter((categoryEntity) -> {
return categoryEntity.getParentCid() == root.getCatId();
}).map(categoryEntity -> {
//1。找到子分类
categoryEntity.setChildren(getChildrens(categoryEntity, all));
return categoryEntity;
}).sorted((menu1, menu2) ->
//2。排序
(menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort())
).collect(Collectors.toList());
return children;
}
}
- entity
@Data
@TableName("pms_category")
public class CategoryEntity implements Serializable {
private static final long serialVersionUID = 1L;
@TableId
private Long catId;
private String name;
private Long parentCid;
private Integer catLevel;
@TableLogic(value = "1",delval = "0")
private Integer showStatus;
private Integer sort;
private String icon;
private String productUnit;
private Integer productCount;
@TableField(exist = false)
private List children;
}



