parent
0e658cc8f0
commit
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,7 +1,9 @@
|
||||||
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;
|
||||||
|
@ -11,6 +13,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;
|
||||||
|
|
||||||
@Tag(name = "购物车接口")
|
@Tag(name = "购物车接口")
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("api/order/cart")
|
@RequestMapping("api/order/cart")
|
||||||
|
@ -25,4 +29,11 @@ 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);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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,11 @@ public interface CartService {
|
||||||
* @param skuNum 商品数量
|
* @param skuNum 商品数量
|
||||||
*/
|
*/
|
||||||
void addToCart(Long skuId, Integer skuNum);
|
void addToCart(Long skuId, Integer skuNum);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询购物车
|
||||||
|
*
|
||||||
|
* @return 购物车列表
|
||||||
|
*/
|
||||||
|
List<CartInfo> getCartList();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,10 @@ 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.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class CartServiceImpl implements CartService {
|
public class CartServiceImpl implements CartService {
|
||||||
|
@ -61,4 +65,27 @@ 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<>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue