尚硅谷 2020 谷粒商城项目 笔记(五) 产品分类 树形结构 拖拽功能 批量操作

目录

1、前端项目配置访问后端项目统一API路径

1.1、renren-fast-vue 项目修改 \static\config\index.js

1.2、将 rest-fast 项目加入 gateway 网关

1.3、gateway 微服务开启跨域配置

2、增删改查,拖拽功能,批量操作

2.1、renren-fast-vue 页面

2.2、renren-fast 使用 MyBatis Plus 提供的默认API


1、前端项目配置访问后端项目统一API路径

1.1、renren-fast-vue 项目修改 \static\config\index.js

baseUrl = http://localhost:88/api

尚硅谷 2020 谷粒商城项目 笔记(五) 产品分类 树形结构 拖拽功能 批量操作_第1张图片

1.2、将 rest-fast 项目加入 gateway 网关

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

1.3、gateway 微服务开启跨域配置

package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 跨域
 *
 * @author xhua
 * @date 2020/05/21 20:21
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1、配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);

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

        return new CorsWebFilter(source);
    }
}

2、增删改查,拖拽功能,批量操作

2.1、renren-fast-vue 页面

category.vue





2.2、renren-fast 使用 MyBatis Plus 提供的默认API

CategoryController.java

package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 商品三级分类
 *
 * @author xerxes
 * @email [email protected]
 * @date 2020-05-13 21:09:30
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类以及子分类,以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    public R list() {

        List entityList = categoryService.listWithTree();

        return R.ok().put("data", entityList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
//    @RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", 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 updateSort(@RequestBody CategoryEntity[] category) {
        categoryService.updateBatchById(Arrays.asList(category));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds) {

        categoryService.removeMenuByIds(Arrays.asList(catIds));

        return R.ok();
    }

}

service

@Override
public List listWithTree() {
	// 1、查出所有分类
	List categoryEntities = baseMapper.selectList(null);

	// 2、组装成父子的树形结构
	// 2.1、找到所有的一级分类
	List level1Menus = categoryEntities.stream()
			.filter((categoryEntity) -> {
				return 0 == categoryEntity.getParentCid();
			}).map((menu) -> {
				menu.setChildren(getChildren(menu, categoryEntities));
				return menu;
			}).sorted((menu1, menu2) -> {
				return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
			}).collect(Collectors.toList());
	
	return level1Menus;
}

/*
 * 递归查找所有菜单的子菜单
 * */
private List getChildren(CategoryEntity root, List all) {

	List children = all.stream().filter(categoryEntity -> {
		return categoryEntity.getParentCid() == root.getCatId();
	}).map((categoryEntity) -> {
		// .找到子菜单
		categoryEntity.setChildren(getChildren(categoryEntity, all));
		return categoryEntity;
	}).sorted((menu1, menu2) -> {
		// 2.菜单排序
		return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
	}).collect(Collectors.toList());

	return children;
}

 

你可能感兴趣的:(vue,java)