diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 65dd89a..80d75e9 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,24 +4,20 @@ - + + + + @@ -189,7 +199,7 @@ - + - @@ -350,7 +368,8 @@ - diff --git a/spzx-server-gateway/src/main/resources/application-dev.yml b/spzx-server-gateway/src/main/resources/application-dev.yml index 1be62cd..90c1dc1 100644 --- a/spzx-server-gateway/src/main/resources/application-dev.yml +++ b/spzx-server-gateway/src/main/resources/application-dev.yml @@ -31,6 +31,10 @@ spring: uri: lb://service-user predicates: - Path=/*/user/** + - id: service-cart + uri: lb://service-cart + predicates: + - Path=/api/order/cart/** datasource: type: com.zaxxer.hikari.HikariDataSource diff --git a/spzx-service-client/service-product-client/pom.xml b/spzx-service-client/service-product-client/pom.xml index 952421a..46efba7 100644 --- a/spzx-service-client/service-product-client/pom.xml +++ b/spzx-service-client/service-product-client/pom.xml @@ -1,4 +1,4 @@ - 4.0.0 @@ -24,5 +24,30 @@ 3.8.1 test + + cn.bunny + common-util + 1.0-SNAPSHOT + provided + + + + cn.bunny + spzx-model + 1.0-SNAPSHOT + provided + + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + + org.springframework.cloud + spring-cloud-loadbalancer + diff --git a/spzx-service-client/service-product-client/src/main/java/cn/bunny/web/App.java b/spzx-service-client/service-product-client/src/main/java/cn/bunny/web/App.java deleted file mode 100644 index dc5b5b5..0000000 --- a/spzx-service-client/service-product-client/src/main/java/cn/bunny/web/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package cn.bunny.web; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} diff --git a/spzx-service-client/service-product-client/src/main/java/cn/bunny/web/ProductFeignClient.java b/spzx-service-client/service-product-client/src/main/java/cn/bunny/web/ProductFeignClient.java new file mode 100644 index 0000000..93a3ecb --- /dev/null +++ b/spzx-service-client/service-product-client/src/main/java/cn/bunny/web/ProductFeignClient.java @@ -0,0 +1,13 @@ +package cn.bunny.web; + +import cn.bunny.common.spzx.model.entity.product.ProductSku; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +@FeignClient(value = "service-product") +public interface ProductFeignClient { + + @GetMapping("/api/product/getBySkuId/{skuId}") + ProductSku getBySkuId(@PathVariable("skuId") Long skuId); +} diff --git a/spzx-service/service-cart/pom.xml b/spzx-service/service-cart/pom.xml index 8d50c9c..7a42f16 100644 --- a/spzx-service/service-cart/pom.xml +++ b/spzx-service/service-cart/pom.xml @@ -29,5 +29,12 @@ org.springframework.boot spring-boot-starter-data-redis + + + cn.bunny.web + service-product-client + 1.0-SNAPSHOT + + diff --git a/spzx-service/service-cart/src/main/java/cn/bunny/web/CartApplication.java b/spzx-service/service-cart/src/main/java/cn/bunny/web/CartApplication.java index 31db64e..1092ec9 100644 --- a/spzx-service/service-cart/src/main/java/cn/bunny/web/CartApplication.java +++ b/spzx-service/service-cart/src/main/java/cn/bunny/web/CartApplication.java @@ -1,11 +1,14 @@ package cn.bunny.web; +import cn.bunny.anno.EnableUserWebMvcConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.cloud.openfeign.EnableFeignClients; -// com.atguigu.spzx.cart; @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) // 排除数据库的自动化配置,Cart微服务不需要访问数据库 +@EnableFeignClients(basePackages = {"cn.bunny"}) +@EnableUserWebMvcConfiguration public class CartApplication { public static void main(String[] args) { SpringApplication.run(CartApplication.class, args); diff --git a/spzx-service/service-cart/src/main/java/cn/bunny/web/controller/CartController.java b/spzx-service/service-cart/src/main/java/cn/bunny/web/controller/CartController.java index e79de8f..5e6ffec 100644 --- a/spzx-service/service-cart/src/main/java/cn/bunny/web/controller/CartController.java +++ b/spzx-service/service-cart/src/main/java/cn/bunny/web/controller/CartController.java @@ -1,5 +1,6 @@ package cn.bunny.web.controller; +import cn.bunny.common.spzx.model.entity.h5.CartInfo; import cn.bunny.common.spzx.model.vo.common.Result; import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum; import cn.bunny.web.service.CartService; @@ -10,6 +11,8 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + @RestController @RequestMapping("api/order/cart") public class CartController { @@ -24,4 +27,11 @@ public class CartController { cartService.addToCart(skuId, skuNum); return Result.build(null, ResultCodeEnum.SUCCESS); } + + @Operation(summary = "查询购物车") + @GetMapping("auth/cartList") + public Result> cartList() { + List cartInfoList = cartService.getCartList(); + return Result.build(cartInfoList, ResultCodeEnum.SUCCESS); + } } diff --git a/spzx-service/service-cart/src/main/java/cn/bunny/web/service/CartService.java b/spzx-service/service-cart/src/main/java/cn/bunny/web/service/CartService.java index fc8c433..cc14982 100644 --- a/spzx-service/service-cart/src/main/java/cn/bunny/web/service/CartService.java +++ b/spzx-service/service-cart/src/main/java/cn/bunny/web/service/CartService.java @@ -1,6 +1,13 @@ package cn.bunny.web.service; +import cn.bunny.common.spzx.model.entity.h5.CartInfo; + +import java.util.List; + public interface CartService { // 添加购物车 void addToCart(Long skuId, Integer skuNum); + + // 查询购物车 + List getCartList(); } diff --git a/spzx-service/service-cart/src/main/java/cn/bunny/web/service/impl/CartServiceImpl.java b/spzx-service/service-cart/src/main/java/cn/bunny/web/service/impl/CartServiceImpl.java index 23e9c76..4bfe44f 100644 --- a/spzx-service/service-cart/src/main/java/cn/bunny/web/service/impl/CartServiceImpl.java +++ b/spzx-service/service-cart/src/main/java/cn/bunny/web/service/impl/CartServiceImpl.java @@ -3,18 +3,25 @@ package cn.bunny.web.service.impl; import cn.bunny.common.AuthContextUtil; import cn.bunny.common.spzx.model.entity.h5.CartInfo; import cn.bunny.common.spzx.model.entity.product.ProductSku; +import cn.bunny.web.ProductFeignClient; import cn.bunny.web.service.CartService; import com.alibaba.fastjson.JSON; import jakarta.annotation.Resource; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; +import java.util.ArrayList; import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; @Service public class CartServiceImpl implements CartService { @Resource private RedisTemplate redisTemplate; + @Resource + private ProductFeignClient productFeignClient; // 定义key user:cart:userId private String getCartKey(Long userId) { @@ -46,7 +53,8 @@ public class CartServiceImpl implements CartService { // 远程调用实现:通过nacos + openFeign 实现,根据skuId获取商品sku信息 cartInfo = new CartInfo(); // 远程调用实现:根据skuId获取商品sku信息 - ProductSku productSku = null; + // ProductSku productSku = productFeignClient.getBySkuId(skuId).getData(); + ProductSku productSku = productFeignClient.getBySkuId(skuId); cartInfo.setCartPrice(productSku.getSalePrice()); cartInfo.setSkuNum(skuNum); cartInfo.setSkuId(skuId); @@ -60,4 +68,20 @@ public class CartServiceImpl implements CartService { redisTemplate.opsForHash().put(cartKey, String.valueOf(skuId), JSON.toJSONString(cartInfo)); } + + // 查询购物车 + @Override + public List getCartList() { + // 1. 构建查询的Redis里面的key值,根据当前userId + Long userId = AuthContextUtil.getUserInfo().getId(); + String cartKey = this.getCartKey(userId); + // 2. 根据key从Redis里面hash类型获取所有value值 cartInfo + List valueList = redisTemplate.opsForHash().values(cartKey); + // List ---> List + if (CollectionUtils.isEmpty(valueList)) { + return (List) valueList.stream().map(cartInfoJSON -> JSON.parseObject(cartInfoJSON.toString(), CartInfo.class)) + .collect(Collectors.toList()); + } + return new ArrayList<>(); + } } diff --git a/spzx-service/service-product/src/main/java/cn/bunny/web/product/controller/ProductController.java b/spzx-service/service-product/src/main/java/cn/bunny/web/product/controller/ProductController.java index a61a146..c95039c 100644 --- a/spzx-service/service-product/src/main/java/cn/bunny/web/product/controller/ProductController.java +++ b/spzx-service/service-product/src/main/java/cn/bunny/web/product/controller/ProductController.java @@ -39,4 +39,10 @@ public class ProductController { ProductItemVo productItemVo = productService.itemBySkuId(skuId); return Result.build(productItemVo, ResultCodeEnum.SUCCESS); } + + @Operation(summary = "远程调用:根据skuId返回sku信息") + @GetMapping("getBySkuId/{skuId}") + public ProductSku getBySkuId(@PathVariable Long skuId) { + return productService.getBySkuId(skuId); + } } diff --git a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java index 9deb3dc..98ce517 100644 --- a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java +++ b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java @@ -16,4 +16,7 @@ public interface ProductService { // 商品详情 ProductItemVo itemBySkuId(Long skuId); + + // 远程调用:根据skuId返回sku信息 + ProductSku getBySkuId(Long skuId); } diff --git a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java index a33b4db..415b099 100644 --- a/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java +++ b/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java @@ -89,4 +89,10 @@ public class ProductServiceImpl implements ProductService { return productItemVo; } + + // 远程调用:根据skuId返回sku信息 + @Override + public ProductSku getBySkuId(Long skuId) { + return productSkuMapper.getById(skuId); + } }