查询购物车

This commit is contained in:
Bunny 2023-12-28 13:44:51 +08:00
parent 329d74a21c
commit 90a52ac342
13 changed files with 151 additions and 37 deletions

View File

@ -4,24 +4,20 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="6ee93b58-a7e0-45a0-9e98-4c363b192afb" name="Changes" comment="创建购物车"> <list default="true" id="6ee93b58-a7e0-45a0-9e98-4c363b192afb" name="Changes" comment="添加购物车">
<change afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/java/cn/bunny/getway/config/RedisConfig.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/spzx-service-client/service-product-client/src/main/java/cn/bunny/web/ProductFeignClient.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/controller/CartController.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/service/CartService.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/service/impl/CartServiceImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-common/common-service/src/main/java/cn/bunny/anno/EnableUserLoginAuthInterceptor.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-common/common-service/src/main/java/cn/bunny/anno/EnableUserWebMvcConfiguration.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-common/common-service/src/main/java/cn/bunny/interceptor/UserLoginAuthInterceptor.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-common/common-service/src/main/java/cn/bunny/interceptor/UserLoginAuthInterceptor.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-server-gateway/src/main/java/cn/bunny/getway/filter/AuthGlobalFilter.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/java/cn/bunny/getway/filter/AuthGlobalFilter.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-server-gateway/src/main/resources/application-dev.yml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/resources/application-dev.yml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/spzx-server-gateway/src/main/resources/application-dev.yml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-server-gateway/src/main/resources/application-dev.yml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service-client/service-product-client/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service-client/service-product-client/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service-client/service-product-client/src/main/java/cn/bunny/web/App.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-cart/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-cart/pom.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/spzx-service/service-cart/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-cart/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-cart/src/main/resources/application-dev.yml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/resources/application-dev.yml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/CartApplication.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/CartApplication.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/resources/application-dev.yml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/resources/application-dev.yml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/controller/CartController.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/controller/CartController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/User.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/User.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/service/CartService.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/service/CartService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/controller/UserinfoController.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/controller/UserinfoController.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/service/impl/CartServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-cart/src/main/java/cn/bunny/web/service/impl/CartServiceImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/service/impl/UserinfoServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-user/src/main/java/cn/bunny/user/service/impl/UserinfoServiceImpl.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/controller/ProductController.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/controller/ProductController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-user/src/main/resources/application-dev.yml" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-user/src/main/resources/application-dev.yml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/ProductService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/spzx-service/service-product/src/main/java/cn/bunny/web/product/service/imp/ProductServiceImpl.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -32,8 +28,8 @@
<option name="RECENT_TEMPLATES"> <option name="RECENT_TEMPLATES">
<list> <list>
<option value="mybatis-mapper" /> <option value="mybatis-mapper" />
<option value="Interface" />
<option value="Class" /> <option value="Class" />
<option value="Interface" />
</list> </list>
</option> </option>
</component> </component>
@ -128,7 +124,20 @@
</set> </set>
</option> </option>
</component> </component>
<component name="RunManager" selected="Spring Boot.ServerGetWayApplication"> <component name="RunManager" selected="Spring Boot.CartApplication">
<configuration name="CartApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" temporary="true" nameIsGenerated="true">
<module name="service-cart" />
<option name="SPRING_BOOT_MAIN_CLASS" value="cn.bunny.web.CartApplication" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="cn.bunny.web.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="ProductApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" temporary="true" nameIsGenerated="true"> <configuration name="ProductApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" temporary="true" nameIsGenerated="true">
<module name="service-product" /> <module name="service-product" />
<option name="SPRING_BOOT_MAIN_CLASS" value="cn.bunny.web.product.ProductApplication" /> <option name="SPRING_BOOT_MAIN_CLASS" value="cn.bunny.web.product.ProductApplication" />
@ -171,8 +180,9 @@
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Spring Boot.ServerGetWayApplication" /> <item itemvalue="Spring Boot.ServerGetWayApplication" />
<item itemvalue="Spring Boot.User" />
<item itemvalue="Spring Boot.ProductApplication" /> <item itemvalue="Spring Boot.ProductApplication" />
<item itemvalue="Spring Boot.CartApplication" />
<item itemvalue="Spring Boot.User" />
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
@ -189,7 +199,7 @@
<workItem from="1703549556395" duration="26425000" /> <workItem from="1703549556395" duration="26425000" />
<workItem from="1703654409796" duration="10591000" /> <workItem from="1703654409796" duration="10591000" />
<workItem from="1703684112039" duration="2403000" /> <workItem from="1703684112039" duration="2403000" />
<workItem from="1703722925328" duration="9980000" /> <workItem from="1703722925328" duration="13331000" />
</task> </task>
<task id="LOCAL-00001" summary="前端接口初始化;显示商品页面"> <task id="LOCAL-00001" summary="前端接口初始化;显示商品页面">
<option name="closed" value="true" /> <option name="closed" value="true" />
@ -319,7 +329,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1703667374773</updated> <updated>1703667374773</updated>
</task> </task>
<option name="localTasksCounter" value="17" /> <task id="LOCAL-00017" summary="添加购物车">
<option name="closed" value="true" />
<created>1703738845213</created>
<option name="number" value="00017" />
<option name="presentableId" value="LOCAL-00017" />
<option name="project" value="LOCAL" />
<updated>1703738845213</updated>
</task>
<option name="localTasksCounter" value="18" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@ -350,7 +368,8 @@
<MESSAGE value="获取用户信息-未测试" /> <MESSAGE value="获取用户信息-未测试" />
<MESSAGE value="获取用户信息并跳转" /> <MESSAGE value="获取用户信息并跳转" />
<MESSAGE value="创建购物车" /> <MESSAGE value="创建购物车" />
<option name="LAST_COMMIT_MESSAGE" value="创建购物车" /> <MESSAGE value="添加购物车" />
<option name="LAST_COMMIT_MESSAGE" value="添加购物车" />
</component> </component>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<pin-to-top-manager> <pin-to-top-manager>

View File

@ -31,6 +31,10 @@ spring:
uri: lb://service-user uri: lb://service-user
predicates: predicates:
- Path=/*/user/** - Path=/*/user/**
- id: service-cart
uri: lb://service-cart
predicates:
- Path=/api/order/cart/**
datasource: datasource:
type: com.zaxxer.hikari.HikariDataSource type: com.zaxxer.hikari.HikariDataSource

View File

@ -1,4 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
@ -24,5 +24,30 @@
<version>3.8.1</version> <version>3.8.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>cn.bunny</groupId>
<artifactId>common-util</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cn.bunny</groupId>
<artifactId>spzx-model</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- openfeign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- loadbalancer依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,13 +0,0 @@
package cn.bunny.web;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}

View File

@ -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);
}

View File

@ -29,5 +29,12 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> </dependency>
<dependency>
<groupId>cn.bunny.web</groupId>
<artifactId>service-product-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,11 +1,14 @@
package cn.bunny.web; package cn.bunny.web;
import cn.bunny.anno.EnableUserWebMvcConfiguration;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients;
// com.atguigu.spzx.cart;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) // 排除数据库的自动化配置Cart微服务不需要访问数据库 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) // 排除数据库的自动化配置Cart微服务不需要访问数据库
@EnableFeignClients(basePackages = {"cn.bunny"})
@EnableUserWebMvcConfiguration
public class CartApplication { public class CartApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(CartApplication.class, args); SpringApplication.run(CartApplication.class, args);

View File

@ -1,5 +1,6 @@
package cn.bunny.web.controller; 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.Result;
import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum; import cn.bunny.common.spzx.model.vo.common.ResultCodeEnum;
import cn.bunny.web.service.CartService; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController @RestController
@RequestMapping("api/order/cart") @RequestMapping("api/order/cart")
public class CartController { public class CartController {
@ -24,4 +27,11 @@ public class CartController {
cartService.addToCart(skuId, skuNum); cartService.addToCart(skuId, skuNum);
return Result.build(null, ResultCodeEnum.SUCCESS); return Result.build(null, ResultCodeEnum.SUCCESS);
} }
@Operation(summary = "查询购物车")
@GetMapping("auth/cartList")
public Result<List<CartInfo>> cartList() {
List<CartInfo> cartInfoList = cartService.getCartList();
return Result.build(cartInfoList, ResultCodeEnum.SUCCESS);
}
} }

View File

@ -1,6 +1,13 @@
package cn.bunny.web.service; package cn.bunny.web.service;
import cn.bunny.common.spzx.model.entity.h5.CartInfo;
import java.util.List;
public interface CartService { public interface CartService {
// 添加购物车 // 添加购物车
void addToCart(Long skuId, Integer skuNum); void addToCart(Long skuId, Integer skuNum);
// 查询购物车
List<CartInfo> getCartList();
} }

View File

@ -3,18 +3,25 @@ package cn.bunny.web.service.impl;
import cn.bunny.common.AuthContextUtil; import cn.bunny.common.AuthContextUtil;
import cn.bunny.common.spzx.model.entity.h5.CartInfo; import cn.bunny.common.spzx.model.entity.h5.CartInfo;
import cn.bunny.common.spzx.model.entity.product.ProductSku; import cn.bunny.common.spzx.model.entity.product.ProductSku;
import cn.bunny.web.ProductFeignClient;
import cn.bunny.web.service.CartService; import cn.bunny.web.service.CartService;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public class CartServiceImpl implements CartService { public class CartServiceImpl implements CartService {
@Resource @Resource
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
@Resource
private ProductFeignClient productFeignClient;
// 定义key user:cart:userId // 定义key user:cart:userId
private String getCartKey(Long userId) { private String getCartKey(Long userId) {
@ -46,7 +53,8 @@ public class CartServiceImpl implements CartService {
// 远程调用实现通过nacos + openFeign 实现根据skuId获取商品sku信息 // 远程调用实现通过nacos + openFeign 实现根据skuId获取商品sku信息
cartInfo = new CartInfo(); cartInfo = new CartInfo();
// 远程调用实现根据skuId获取商品sku信息 // 远程调用实现根据skuId获取商品sku信息
ProductSku productSku = null; // ProductSku productSku = productFeignClient.getBySkuId(skuId).getData();
ProductSku productSku = productFeignClient.getBySkuId(skuId);
cartInfo.setCartPrice(productSku.getSalePrice()); cartInfo.setCartPrice(productSku.getSalePrice());
cartInfo.setSkuNum(skuNum); cartInfo.setSkuNum(skuNum);
cartInfo.setSkuId(skuId); cartInfo.setSkuId(skuId);
@ -60,4 +68,20 @@ public class CartServiceImpl implements CartService {
redisTemplate.opsForHash().put(cartKey, String.valueOf(skuId), JSON.toJSONString(cartInfo)); redisTemplate.opsForHash().put(cartKey, String.valueOf(skuId), JSON.toJSONString(cartInfo));
} }
// 查询购物车
@Override
public List<CartInfo> 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<Object> ---> List<cartInfo>
if (CollectionUtils.isEmpty(valueList)) {
return (List<CartInfo>) valueList.stream().map(cartInfoJSON -> JSON.parseObject(cartInfoJSON.toString(), CartInfo.class))
.collect(Collectors.toList());
}
return new ArrayList<>();
}
} }

View File

@ -39,4 +39,10 @@ public class ProductController {
ProductItemVo productItemVo = productService.itemBySkuId(skuId); ProductItemVo productItemVo = productService.itemBySkuId(skuId);
return Result.build(productItemVo, ResultCodeEnum.SUCCESS); return Result.build(productItemVo, ResultCodeEnum.SUCCESS);
} }
@Operation(summary = "远程调用根据skuId返回sku信息")
@GetMapping("getBySkuId/{skuId}")
public ProductSku getBySkuId(@PathVariable Long skuId) {
return productService.getBySkuId(skuId);
}
} }

View File

@ -16,4 +16,7 @@ public interface ProductService {
// 商品详情 // 商品详情
ProductItemVo itemBySkuId(Long skuId); ProductItemVo itemBySkuId(Long skuId);
// 远程调用根据skuId返回sku信息
ProductSku getBySkuId(Long skuId);
} }

View File

@ -89,4 +89,10 @@ public class ProductServiceImpl implements ProductService {
return productItemVo; return productItemVo;
} }
// 远程调用根据skuId返回sku信息
@Override
public ProductSku getBySkuId(Long skuId) {
return productSkuMapper.getById(skuId);
}
} }