✅P267_商城业务-订单服务-订单确认页数据获取
开启远程服务调用功能
启动类上加@EnableFeignClients
注解
@SpringBootApplication
@MapperScan("com.gyz.cfmall.order.dao")
@EnableRabbit
@EnableDiscoveryClient
@EnableRedisHttpSession
@EnableFeignClients
public class CfmallOrderApplication {
public static void main(String[] args) {
SpringApplication.run(CfmallOrderApplication.class, args);
}
}
远程获取会员地址
Feign
cfmall-order/src/main/java/com/gyz/cfmall/order/feign/MemberFeignService.java
package com.gyz.cfmall.order.feign;
import com.gyz.cfmall.order.vo.MemberAddressVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
/**
* @author gong_yz
* @Description
* @Date 2023/4/18
*/
@FeignClient("cfmall-member")
public interface MemberFeignService {
/**
* 查询当前用户的全部收货地址
*
* @param memberId
* @return
*/
@GetMapping(value = "/member/memberreceiveaddress/{memberId}/address")
List<MemberAddressVo> getAddress(@PathVariable("memberId") Long memberId);
}
业务实现
cfmall-member/src/main/java/com/gyz/cfmall/member/controller/MemberReceiveAddressController.java
@Autowired
private MemberReceiveAddressService memberReceiveAddressService;
/**
* 根据会员id查询会员的所有地址
* @param memberId
* @return
*/
@GetMapping(value = "/{memberId}/address")
public List<MemberReceiveAddressEntity> getAddress(@PathVariable("memberId") Long memberId) {
List<MemberReceiveAddressEntity> addressList = memberReceiveAddressService.getAddress(memberId);
return addressList;
}
cfmall-member/src/main/java/com/gyz/cfmall/member/service/impl/MemberReceiveAddressServiceImpl.java
@Override
public List<MemberReceiveAddressEntity> getAddress(Long memberId) {
List<MemberReceiveAddressEntity> addressList = this.baseMapper.selectList
(new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", memberId));
return addressList;
}
远程查询购物车商品项
Feign
cfmall-order/src/main/java/com/gyz/cfmall/order/feign/CartFeignService.java
package com.gyz.cfmall.order.feign;
import com.gyz.cfmall.order.vo.OrderItemVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
/**
* @author gong_yz
* @Description
* @Date 2024-01-09
*/
@FeignClient("cfmall-cart")
public interface CartFeignService {
/**
* 查询当前用户购物车选中的商品项
*
* @return
*/
@GetMapping(value = "/currentUserCartItems")
List<OrderItemVo> getCurrentCartItems();
}
业务实现
com.gyz.cfmall.controller.CartController#getCurrentCartItems
@Resource
private CartService cartService;
/**
* 获取当前用户的购物车商品项
* @return
*/
@GetMapping(value = "/currentUserCartItems")
@ResponseBody
public List<CartItemVo> getCurrentCartItems() {
List<CartItemVo> cartItemVoList = cartService.getUserCartItems();
return cartItemVoList;
}
com.gyz.cfmall.service.impl.CartServiceImpl#getUserCartItems
@Override
public List<CartItemVo> getUserCartItems() {
List<CartItemVo> cartItemVoList = new ArrayList<>();
//获取当前用户登录的信息
UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
//如果用户未登录直接返回null
if (userInfoTo.getUserId() == null) {
return null;
} else {
//获取购物车项
String cartKey = CART_PREFIX + userInfoTo.getUserId();
//获取所有的
List<CartItemVo> cartItems = this.getCartItems(cartKey);
if (cartItems == null) {
throw new CartExceptionHandler();
}
//筛选出选中的
cartItemVoList = cartItems.stream()
.filter(items -> items.getCheck())
.map(item -> {
//更新为最新的价格(查询数据库)
BigDecimal price = productFeignService.getPrice(item.getSkuId());
item.setPrice(price);
return item;
})
.collect(Collectors.toList());
}
return cartItemVoList;
}
com.gyz.cfmall.service.impl.CartServiceImpl#getCartItems
/**
* 获取购物车里面的数据
* @param cartKey
* @return
*/
private List<CartItemVo> getCartItems(String cartKey) {
//获取购物车里面的所有商品
BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(cartKey);
List<Object> values = operations.values();
if (values != null && values.size() > 0) {
List<CartItemVo> cartItemVoStream = values.stream().map((obj) -> {
String str = (String) obj;
CartItemVo cartItem = JSON.parseObject(str, CartItemVo.class);
return cartItem;
}).collect(Collectors.toList());
return cartItemVoStream;
}
return null;
}
com.gyz.cfmall.exception.CartExceptionHandler
public class CartExceptionHandler extends RuntimeException {
}
远程查询商品价格
com.gyz.cfmall.feign.ProductFeignService#getPrice
package com.gyz.cfmall.feign;
import com.gyz.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.math.BigDecimal;
import java.util.List;
/**
* @author gong_yz
* @Description
*/
@FeignClient("cfmall-product")
public interface ProductFeignService {
/**
* 根据skuId查询当前商品的最新价格
* @param skuId
* @return
*/
@GetMapping(value = "/product/skuinfo/{skuId}/price")
BigDecimal getPrice(@PathVariable("skuId") Long skuId);
//其它代码略...
}
com.gyz.cfmall.product.controller.SkuInfoController#getPrice
@Autowired
private SkuInfoService skuInfoService;
/**
* 根据skuId查询当前商品的价格
* @param skuId
* @return
*/
@GetMapping(value = "/{skuId}/price")
public BigDecimal getPrice(@PathVariable("skuId") Long skuId) {
//获取当前商品的信息
SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
//获取商品的价格
BigDecimal price = skuInfo.getPrice();
return price;
}
获取总额方法
getTotal()、getPayPrice()
package com.gyz.cfmall.order.vo;
import lombok.Getter;
import lombok.Setter;
import java.math.BigDecimal;
import java.util.List;
/**
* @author gong_yz
* @Description 订单确认页VO
* @Date 2024-01-09
*/
public class OrderConfirmVo {
/**
* 会员收获地址列表
*/
@Getter
@Setter
List<MemberAddressVo> memberAddressVos;
/**
* 所有选中的购物项
*/
@Getter
@Setter
List<OrderItemVo> items;
/**
* 优惠券(会员积分)
*/
@Getter
@Setter
private Integer integration;
/**
* 防止重复提交的令牌
*/
@Getter
@Setter
private BigDecimal total;
@Getter
@Setter
private BigDecimal payPrice;
/**
* 订单总额
*
* @return
*/
public BigDecimal getTotal() {
BigDecimal totalNum = BigDecimal.ZERO;
if (items != null && items.size() > 0) {
for (OrderItemVo item : items) {
//计算当前商品的总价格
BigDecimal itemPrice = item.getPrice().multiply(new BigDecimal(item.getCount().toString()));
//再计算全部商品的总价格
totalNum = totalNum.add(itemPrice);
}
}
return totalNum;
}
/**
* 应付价格
*/
public BigDecimal getPayPrice() {
return getTotal();
}
}
令牌(Token)
为了防止用户重复提交提订单,编写一个令牌(Token)
package com.gyz.cfmall.order.vo;
import lombok.Getter;
import lombok.Setter;
import java.math.BigDecimal;
import java.util.List;
/**
* @author gong_yz
* @Description 订单确认页VO
*/
public class OrderConfirmVo {
//省略其它代码...
/**
* 防止重复提交的令牌
*/
@Getter
@Setter
private String orderToken;
}