首页 > 其他分享 >谷粒商城--三级分类

谷粒商城--三级分类

时间:2023-05-26 10:35:16浏览次数:38  
标签:菜单 -- private renren api 谷粒 id 商城 localhost

谷粒商城--三级分类

一、树形数据返回

1、递归查询树形结构商品信息

查询的实体类,要实现树形,实体类需要有pId(父id),nId(子id)加上忽略字段children(子类集合)。

public class CategoryEntity implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * 分类id
	 */
	@TableId
	private Long catId;
	/**
	 * 分类名称
	 */
	private String name;
	/**
	 * 父分类id
	 */
	private Long parentCid;
	/**
	 * 层级
	 */
	private Integer catLevel;
	/**
	 * 是否显示[0-不显示,1显示]
	 */
	private Integer showStatus;
	/**
	 * 排序
	 */
	private Integer sort;
	/**
	 * 图标地址
	 */
	private String icon;
	/**
	 * 计量单位
	 */
	private String productUnit;
	/**
	 * 商品数量
	 */
	private Integer productCount;

	@TableField(exist = false)
	private List<CategoryEntity> children;

}

2、查询出所有待分类数据

List<CategoryEntity> categoryEntityList = this.lambdaQuery().list();

3、取出一级菜单

List<CategoryEntity> rootEntityList = categoryEntityList.stream()
                .filter(i -> i.getParentCid() == 0) // 取出一级菜单
                .peek(i-> i.setChildren(getChildren(i,categoryEntityList))) // 为每级菜单赋值子菜单
                .peek(i->{if(Objects.isNull(i.getSort())){i.setSort(0);}}) // 空值处理
                .sorted(Comparator.comparing(CategoryEntity::getSort)) // 排序
                .collect(Collectors.toList());

4、递归查询所有菜单的子菜单

private List<CategoryEntity> getChildren(CategoryEntity nowCategory, List<CategoryEntity> categoryEntityList) {
    return categoryEntityList.stream() // 返回子菜单
            .filter(i -> i.getParentCid().equals(nowCategory.getCatId())) // 获取当前菜单的子菜单
            .peek(i->i.setChildren(getChildren(i,categoryEntityList))) // 把获取到的子菜单递归此方法
            .peek(i->{if(Objects.isNull(i.getSort())){i.setSort(0);}}) // 空值处理
            .sorted(Comparator.comparing(CategoryEntity::getSort)) // 排序
            .collect(Collectors.toList());
}

二、配置网关路由与路径重写

1、新建目录菜单

菜单指定路由url

2、创建模块以及对应的vue

<template>
  <div>
    <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
  </div>
</template>

<script>
// 这里可以导入其他文件(比如:组件,工具 js,第三方插件 js,json 文件,图片文件等等)
// 例如:import  《组件名称》  from '《组件路径》 ';

export default {
  name: 'category',
  data () {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  // import 引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  // 方法集合
  methods: {
    handleNodeClick (data) {
      console.log(data)
    },
    getMenus () {
      this.$http({
        url: this.$http.adornUrl('/product/category/tree/list'),
        method: 'get'
      }).then(({data}) => {
        console.log(data)
        // this.data = data.data
      })
    }
  },
  // 生命周期 - 创建之前
  beforeCreate () {
  },
  // 生命周期 - 创建完成(可以访问当前this 实例)
  created () {
    this.getMenus()
  }
}
</script>

<style scoped>
</style>

3、修改index.js中基础路径

// api接口请求地址
window.SITE_CONFIG['baseUrl'] = 'http://localhost:8080/renren-fast';

此时发送的http请求路径不符合要求

http://localhost:8080/renren-fast/product/category/list/tree

修改api请求接口为网关地址方便访问各种服务,进行扩展

// api接口请求地址
window.SITE_CONFIG['baseUrl'] = 'http://localhost:88/api'

此时实际请求的地址(如验证码)

http://localhost:88/api/captcha.jpg?uuid=a73e24f4-1dce-4aa3-8842-639e79c510ce

4、后台注册nacos

直接请求的网关的相关内容,我们希望的时网关给我们进行路由转发到对应的服务中,而不是直接处理我们的请求。

要想网关给我们做转发,需要网关知道这个服务,将他添加到nacos中。

  • 依赖

  • bootstrap.yml

  • 主启动类 @EnableDiscoveryClient

5、路由转发配置

spring:
  application:
    name: gulimall-gateway
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        namespace: df63e8e1-2b80-42b5-9db0-8aaa4ba6bac3
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: admin-router #id唯一
          uri: lb://renren-fast # 路由转发的地址 lb:表示负载均衡到转发的地址
          predicates: # 断言
            - Path=/api/** # 断言规则: 路径中包含api

此时请求验证码接口404

http://localhost:88/api/captcha.jpg?uuid=02fa4bdd-020d-4774-873a-3e16cfa64286

网关进行转发后,实际请求的服务接口为

http://renren-fast:8080/api/captcha.ipg?uuid=02fa4bdd-020d-4774-873a-3e16cfa64286

默认需要的访问接口为

http://renren-fast:8080/renren-fast/captcha.ipg?uuid=02fa4bdd-020d-4774-873a-3e16cfa64286

6、断言匹配后使用过滤器重写请求路径

spring:
  application:
    name: gulimall-gateway
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        namespace: df63e8e1-2b80-42b5-9db0-8aaa4ba6bac3
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: admin-router 
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}
            # 匹配api开头的路径重写为renren-fast路径
            # 后面的renren-fast 是servlet的请求地址前缀

此时请求验证码接口正常返回

此时发现跨域问题

三、跨域配置

nginx代理配置跨域

网关配置跨域处理

@Configuration
public class GulimallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        System.out.println("加载跨域");
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();

        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}

注释人人自带跨域

四、前端页面请求树形结构数据

1、网关路由配置

配置网关路由到product服务

范围小的路由应该写在前面,否则去product的请求会被转发到admin后台服务

spring:
  application:
    name: gulimall-gateway
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        namespace: df63e8e1-2b80-42b5-9db0-8aaa4ba6bac3
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: product-router #id唯一
          uri: lb://gulimall-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}
        - id: admin-router #id唯一
          uri: lb://renren-test
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>.*),/renren-test/$\{segment}

标签:菜单,--,private,renren,api,谷粒,id,商城,localhost
From: https://www.cnblogs.com/hwjShl/p/17418585.html

相关文章

  • Python 函数重载
    函数重载是指可以使用相同的函数名,但是函数参数类型和/或数量不同的多个函数。Python不支持函数重载,因为Python是一种动态类型语言,函数参数类型不需要在代码中声明,并且Python中的函数参数数量也可以是可变的。这意味着在同一个作用域内定义两个或两个以上具有相同名称但参数......
  • 揭秘百度IM消息中台的全量用户消息推送技术改造实践
    本文内容由百度技术团队分享,原题“基于公共信箱的全量消息实现”,为了帮助理解,有较多修订、内容重组和重新排版。1、引言百度的IM消息中台为百度APP以及厂内百度系产品提供即时通讯的能力,提供包括私聊、群聊、聊天室、直播弹幕等用户沟通场景,并帮助业务通过消息推送触达用户。如......
  • 如何在Angular应用程序中插入自定义 CSS?这里有答案!
    KendoUIforAngular是专用于Angular开发的专业级Angular组件,telerik致力于提供纯粹的高性能AngularUI组件,无需任何jQuery依赖关系。KendoUIR12023正式版下载Telerik_KendoUI产品技术交流群:726377843    欢迎一起进群讨论为什么需要在Angular应用程序中插入自定义C......
  • 肖sir__实践题___测试用例题
    实践面试题1、手淘浏览店铺页15s,可以完成任务,放发奖励。请设计测试用 2、用户在pc中选择时间范围后,需要将相应的表格数据下载,请根据这个功能设计功能用例 3、用例设计:某程序实现如下功能:输入3个数据A,B,C,输出以A.B.C为边长组成的三角形的面积。(1<AB,C<100)等价类和边......
  • 打卡第三十五天
    矩阵的乘法运算一、1。建立一个整数矩阵类matrix2.建立该整数矩阵类matrix构造函数;建立一个*(乘号)的运算符重载;建立输出函数voiddisplay()3.主函数输入矩阵对象二、三、#include<iostream>#include<iomanip>usingnamespacestd;classmatrix{private:introw;i......
  • js数据解构
    1、变量比值多可以设置变量默认值 解构let[a,b,c,d='aaa']=[12,34,24]2、变量比值少可以用剩余参数解构let[a,...b]=[11,22,44,55]//a11b[22,44,55]3、按需赋值let[a,,b]=[5,6,9]4、数组嵌套解构let[a,b,arr]=[1,2,[5,8,9]]let[a,b,[c,d,e]]=[1,2,[5,8,9]]5、......
  • 4万多条糗事百科网站数据ACCESS\EXCEL数据库
    这个ACCESS数据库采集的是糗事百科小清新网站的内容,而且内容大于400字的将不收集(内容太长的大多是裹脚布),我要的是浓缩的精华。如果你需要实时采集糗事百科的应用程序,也可以联系我获得。 本数据库是由MicrosoftAccess2000创建的MDB数据库文件,您需要使用MicrosoftAccess......
  • EasyExcel导入对失败数据进行标红导出
    模板渲染工具类publicclassExcelUtils{/***模板表头样式*@paramtemplateName"classpath:template/"模板文件名称*@paramdata数据*@paramwriteHandler自定额填充策略*/publicstaticStringcommonImport(Stringtemplate......
  • 皕杰报表为什么不采用web端设计器
    皕杰报表的设计器采用的是针对操作系统的本地端应用程序,是一种针对操作系统的nativeapplication,这种应用程序有诸多的优点和很多的好处,在这里就不一一列举了,现在就皕杰报表设计器当初的技术选型时“为啥不采用web端设计器”做一个简单的说明,主要就是如下5个方面的因素:本地设计器......
  • Spring Web 参数验证常见错误
    案例1:对象参数校验失效在构建Web服务时,我们一般都会对一个HTTP请求的Body内容进行校验,例如我们来看这样一个案例及对应代码。当开发一个学籍管理系统时,我们会提供了一个API接口去添加学生的相关信息,其对象定义参考下面的代码:(https://www.java567.com,搜"spring") importlo......