完成商品服务的三级分类——上

所谓商品三级分类:

70.jpg

这个在数据库应该以树的形式存放;事先我已经忘数据库导了一千多条数据;


一、编写后端获取树接口

/product/category/list/tree

在service层的树结构实现代码:

public List<CategoryEntity> listWithTree() {
    //查出所有的CategoryEntity列表后,构建Tree,并实现排序
    return buildTree(baseMapper.selectList(null), 0L);
}

private List<CategoryEntity> buildTree(List<CategoryEntity> list, Long parentCid){
    return list.stream().filter(ce -> parentCid.equals(ce.getParentCid()))
            .map(ce -> {
                ce.setChildren(buildTree(list, ce.getCatId()));
                return ce;
            }).sorted((cate1, cate2) -> (cate1.getSort()==null?0:cate1.getSort()) - (cate2.getSort()==null?0:cate2.getSort()))
            .collect(Collectors.toList());
}

树结构数据的实现方式有很多种,这是我个人比较喜欢的方式;

72.jpg

注意,只有三层!


二、将 renren-fast 后台启动并注册到注册中心

1、pom.xml:

<dependency>
   <groupId>com.jiguiquan.zidanmall</groupId>
   <artifactId>zidanmall-common</artifactId>
   <version>1.0-SNAPSHOT</version>
</dependency>

2、application.yml:

spring:
  application:
    name: renren-fast
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

3、主启动类增加: @EnableDiscoveryClient   


三、启动renren-fast-vue 前端页面

启动网关项目后,网关端口为88

1、使用npm启动renren-fast-vue的前端项目:

为了防止端口冲突,我已经将默认的端口改为了8100:

因为后端的微服务以后会很多,我们不可能调用不同的微服务时候,访问不同的服务器地址,因为我们这时候就要用到Gateway网关帮助我们统一转发:

而且在做web项目时,头疼的跨域问题,我们也可以放在网关中解决,一劳永逸!

2、将前端项目访问的基础 baseUrl 地址改为网关地址:

73.jpg

3、启动后,我们发现,连验证码都获取不到:

74.jpg

显然网关这个服务下是没有提供这个功能接口的,功能接口在 renren-fast 后端服务下,所以我们需要网关帮我们转发:

4、在网关中进行如下配置,路由转发和路径重写:

相关断言和过滤器的文档地址:

https://cloud.spring.io/spring-cloud-gateway/reference/html/#the-path-route-predicate-factory

https://cloud.spring.io/spring-cloud-gateway/reference/html/#the-rewritepath-gatewayfilter-factory

spring:
  application:
    name: zidanmall-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      routes:
        - id: product_route   #这个是商品服务的,我先完成了
          uri: lb://zidanmall-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?<segment>/?.*), /$\{segment}

        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>/?.*), /renren-fast/$\{segment}

5、再次启动,验证码就可以获取到了,说明网关也正常工作了

75.jpg


四、使用网关统一解决跨域问题

当我们尝试登陆的时候:

76.jpg

发现,登陆请求的预检 OPTION 请求遇到了跨域问题!

我们上面说过,可以在网关中统一解决所有服务的跨域问题

1、在网关下创建一个跨域配置文件 com.jiguiquan.zidanmall.gateway.config.ZidanmallCorsConfiguration

@Configuration
public class ZidanmallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //配置跨域信息
        corsConfiguration.addAllowedHeader("*");   //允许所有头跨域
        corsConfiguration.addAllowedMethod("*");   //允许所有请求方式跨域
        corsConfiguration.addAllowedOrigin("*");   //允许所有请求来源(域)进行跨域
        corsConfiguration.setAllowCredentials(true);  //允许携带cookie进行跨域

        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}

2、重启网关后,测试登陆:

发现还有一些小问题:

77.jpg

是说,我们的跨域配置重复了:只允许一个,肯定是renren-fast中自己也做了跨域配置;

3、不要犹豫,果断注释掉 renren-fast 原项目中的跨域配置:

78.jpg

4、之后登陆就可以正常了:


五、完成 商品系统–分类维护 菜单模块

1、新建 商品系统–分类维护 菜单

81.jpg

此处回发现一个规律,菜单URL对应的 product/category ,在路径上会被替换为 product-category 

2、知道这样的规律后,我们在 views/modules 目录下创建 /product/category.vue 文件

82.jpg

使用我另有一篇文章写得快速生成vue文件的模板:

http://www.jiguiquan.com/archives/1159

3、三级分类管理,我们肯定是要用树形结构展示,我们使用element-ui的组件库完成:

https://element.eleme.cn/#/zh-CN/component/tree

只有第一次使用,我记录下element-ui之类的组件库的使用:

83.jpg

粘贴在代码中的样子:

84.jpg

4、在生命周期 created完成后,钓友们能够 getmenus() 方法获取三级分类 Tree 数据:

85.jpg

然后,我们就可以在页面中看到已经获取到列表了:

86.jpg


六、完成三级分类的删除功能

1、新增和删除等操作,我们的实现方式选择比较友好的 scoped slot 方式

87.jpg

直接使用示例代码中的 span 片段

88.jpg

2、使用属性进行精细化处理:

90.jpg

效果如下:

89.jpg


增删改的逻辑放在下一篇记录:

商品服务的三级分类——下

jiguiquan@163.com

文章作者信息...

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐