Compare commits
5 Commits
0e658cc8f0
...
b8c745ff37
Author | SHA1 | Date |
---|---|---|
|
b8c745ff37 | |
|
55d0612a31 | |
|
1dc696935d | |
|
cd1b13900e | |
|
c92f246341 |
|
@ -47,6 +47,10 @@ spring:
|
||||||
uri: lb://service-order
|
uri: lb://service-order
|
||||||
predicates:
|
predicates:
|
||||||
- Path=/*/order/**
|
- Path=/*/order/**
|
||||||
|
- id: service-cart
|
||||||
|
uri: lb://service-cart
|
||||||
|
predicates:
|
||||||
|
- Path=/*/order/cart/**
|
||||||
data:
|
data:
|
||||||
redis:
|
redis:
|
||||||
host: ${bunny.redis.host}
|
host: ${bunny.redis.host}
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
package com.atguigu.cart.controller;
|
package com.atguigu.cart.controller;
|
||||||
|
|
||||||
import com.atguigu.cart.service.CartService;
|
import com.atguigu.cart.service.CartService;
|
||||||
|
import com.atguigu.spzx.model.entity.h5.CartInfo;
|
||||||
import com.atguigu.spzx.model.vo.result.Result;
|
import com.atguigu.spzx.model.vo.result.Result;
|
||||||
|
import com.atguigu.spzx.model.vo.result.ResultCodeEnum;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import java.util.List;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@Tag(name = "购物车接口")
|
@Tag(name = "购物车接口")
|
||||||
@RestController
|
@RestController
|
||||||
|
@ -25,4 +26,40 @@ public class CartController {
|
||||||
cartService.addToCart(skuId, skuNum);
|
cartService.addToCart(skuId, skuNum);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "查询购物车")
|
||||||
|
@GetMapping("auth/cartList")
|
||||||
|
public Result<List<CartInfo>> cartList() {
|
||||||
|
List<CartInfo> cartInfoList = cartService.getCartList();
|
||||||
|
return Result.build(cartInfoList, ResultCodeEnum.SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "删除购物车商品")
|
||||||
|
@DeleteMapping("auth/deleteCart/{skuId}")
|
||||||
|
public Result<String> deleteCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId) {
|
||||||
|
cartService.deleteCart(skuId);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "更新购物车商品选中状态")
|
||||||
|
@GetMapping("/auth/checkCart/{skuId}/{isChecked}")
|
||||||
|
public Result<String> checkCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable(value = "skuId") Long skuId,
|
||||||
|
@Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true) @PathVariable(value = "isChecked") Integer isChecked) {
|
||||||
|
cartService.checkCart(skuId, isChecked);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "更新购物车商品全部选中状态")
|
||||||
|
@GetMapping("/auth/allCheckCart/{isChecked}")
|
||||||
|
public Result<String> allCheckCart(@Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true) @PathVariable(value = "isChecked") Integer isChecked) {
|
||||||
|
cartService.allCheckCart(isChecked);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "清空购物车")
|
||||||
|
@GetMapping("/auth/clearCart")
|
||||||
|
public Result<String> clearCart() {
|
||||||
|
cartService.clearCart();
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,9 @@
|
||||||
package com.atguigu.cart.service;
|
package com.atguigu.cart.service;
|
||||||
|
|
||||||
|
import com.atguigu.spzx.model.entity.h5.CartInfo;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface CartService {
|
public interface CartService {
|
||||||
/**
|
/**
|
||||||
* 添加购物车
|
* 添加购物车
|
||||||
|
@ -8,4 +12,38 @@ public interface CartService {
|
||||||
* @param skuNum 商品数量
|
* @param skuNum 商品数量
|
||||||
*/
|
*/
|
||||||
void addToCart(Long skuId, Integer skuNum);
|
void addToCart(Long skuId, Integer skuNum);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询购物车
|
||||||
|
*
|
||||||
|
* @return 购物车列表
|
||||||
|
*/
|
||||||
|
List<CartInfo> getCartList();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除购物车商品
|
||||||
|
*
|
||||||
|
* @param skuId 商品的ID值
|
||||||
|
*/
|
||||||
|
void deleteCart(Long skuId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新购物车商品选中状态
|
||||||
|
*
|
||||||
|
* @param skuId 商品的ID值
|
||||||
|
* @param isChecked 是否选中
|
||||||
|
*/
|
||||||
|
void checkCart(Long skuId, Integer isChecked);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新购物车商品全部选中状态
|
||||||
|
*
|
||||||
|
* @param isChecked 是否选中
|
||||||
|
*/
|
||||||
|
void allCheckCart(Integer isChecked);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空购物车
|
||||||
|
*/
|
||||||
|
void clearCart();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.atguigu.cart.service.impl;
|
package com.atguigu.cart.service.impl;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
|
||||||
import com.atguigu.cart.service.CartService;
|
import com.atguigu.cart.service.CartService;
|
||||||
import com.atguigu.context.BaseContext;
|
import com.atguigu.context.BaseContext;
|
||||||
import com.atguigu.feign.product.ProductFeignClient;
|
import com.atguigu.feign.product.ProductFeignClient;
|
||||||
|
@ -11,7 +12,11 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
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 java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class CartServiceImpl implements CartService {
|
public class CartServiceImpl implements CartService {
|
||||||
|
@ -61,4 +66,95 @@ 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询购物车
|
||||||
|
*
|
||||||
|
* @return 购物车列表
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<CartInfo> getCartList() {
|
||||||
|
// 获取当前登录的用户信息
|
||||||
|
Long userId = BaseContext.getUserInfo().getId();
|
||||||
|
String cartKey = userId.toString();
|
||||||
|
|
||||||
|
// 获取数据
|
||||||
|
List<Object> cartInfoList = redisTemplate.opsForHash().values(cartKey);
|
||||||
|
|
||||||
|
if (!CollectionUtils.isEmpty(cartInfoList)) {
|
||||||
|
return cartInfoList.stream().map(cartInfoJSON -> JSON.parseObject(cartInfoJSON.toString(), CartInfo.class))
|
||||||
|
.sorted((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除购物车商品
|
||||||
|
*
|
||||||
|
* @param skuId 商品的ID值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void deleteCart(Long skuId) {
|
||||||
|
// 获取当前登录的用户数据
|
||||||
|
Long userId = BaseContext.getUserInfo().getId();
|
||||||
|
String cartKey = userId.toString();
|
||||||
|
|
||||||
|
// 获取缓存对象
|
||||||
|
redisTemplate.opsForHash().delete(cartKey, String.valueOf(skuId));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新购物车商品选中状态
|
||||||
|
*
|
||||||
|
* @param skuId 商品的ID值
|
||||||
|
* @param isChecked 是否选中
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void checkCart(Long skuId, Integer isChecked) {
|
||||||
|
// 获取当前登录的用户数据
|
||||||
|
Long userId = BaseContext.getUserInfo().getId();
|
||||||
|
String cartKey = userId.toString();
|
||||||
|
|
||||||
|
Boolean hasKey = redisTemplate.opsForHash().hasKey(cartKey, String.valueOf(skuId));
|
||||||
|
if (hasKey) {
|
||||||
|
String cartInfoJSON = Objects.requireNonNull(redisTemplate.opsForHash().get(cartKey, String.valueOf(skuId))).toString();
|
||||||
|
CartInfo cartInfo = JSON.parseObject(cartInfoJSON, CartInfo.class);
|
||||||
|
cartInfo.setIsChecked(isChecked);
|
||||||
|
redisTemplate.opsForHash().put(cartKey, String.valueOf(skuId), JSON.toJSONString(cartInfo));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新购物车商品全部选中状态
|
||||||
|
*
|
||||||
|
* @param isChecked 是否选中
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void allCheckCart(Integer isChecked) {
|
||||||
|
// 获取当前登录的用户数据
|
||||||
|
Long userId = BaseContext.getUserInfo().getId();
|
||||||
|
String cartKey = userId.toString();
|
||||||
|
|
||||||
|
// 获取所有的购物项数据
|
||||||
|
List<Object> objectList = redisTemplate.opsForHash().values(cartKey);
|
||||||
|
if (!CollectionUtils.isEmpty(objectList)) {
|
||||||
|
objectList.stream().map(cartInfoJSON -> {
|
||||||
|
CartInfo cartInfo = JSON.parseObject(cartInfoJSON.toString(), CartInfo.class);
|
||||||
|
cartInfo.setIsChecked(isChecked);
|
||||||
|
return cartInfo;
|
||||||
|
}).forEach(cartInfo -> redisTemplate.opsForHash().put(cartKey, String.valueOf(cartInfo.getSkuId()), JSON.toJSONString(cartInfo)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空购物车
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void clearCart() {
|
||||||
|
Long userId = BaseContext.getUserInfo().getId();
|
||||||
|
String cartKey = userId.toString();
|
||||||
|
redisTemplate.delete(cartKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue