diff --git a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/controller/app/CartController.java b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/controller/app/CartController.java index 94f74f882..d47f8358d 100644 --- a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/controller/app/CartController.java +++ b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/controller/app/CartController.java @@ -12,6 +12,7 @@ import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; +import java.util.Collections; import java.util.List; /** @@ -32,16 +33,20 @@ public class CartController { @ApiOperation(value = "查询购物车") @GetMapping @ApiOperationSupport(order = 1) - public Result getCart() { - Long memberId = JwtUtils.getUserId(); - List result = cartService.listCartItemByMemberId(memberId); - return Result.success(result); + public Result getCart() { + try { + Long memberId = JwtUtils.getUserId(); + List result = cartService.listCartItemByMemberId(memberId); + return Result.success((T) result); + } catch (Exception e) { + return Result.success((T) Collections.EMPTY_LIST); + } } @ApiOperation(value = "删除购物车") @DeleteMapping @ApiOperationSupport(order = 2) - public Result deleteCart() { + public Result deleteCart() { boolean result = cartService.deleteCart(); return Result.judge(result); } @@ -50,7 +55,7 @@ public class CartController { @ApiImplicitParam(name = "skuId", value = "SKU ID", required = true, paramType = "param", dataType = "Long") @PostMapping @ApiOperationSupport(order = 3) - public Result addCartItem(@RequestParam Long skuId) { + public Result addCartItem(@RequestParam Long skuId) { cartService.addCartItem(skuId); return Result.success(); } @@ -58,7 +63,7 @@ public class CartController { @ApiOperation(value = "更新购物车商品") @PutMapping("/skuId/{skuId}") @ApiOperationSupport(order = 4) - public Result updateCartItem(@PathVariable Long skuId,@RequestBody CartItemDTO cartItem) { + public Result updateCartItem(@PathVariable Long skuId, @RequestBody CartItemDTO cartItem) { cartItem.setSkuId(skuId); boolean result = cartService.updateCartItem(cartItem); return Result.judge(result); @@ -68,7 +73,7 @@ public class CartController { @ApiImplicitParam(name = "skuId", value = "SKU ID", required = true, paramType = "param", dataType = "Long") @DeleteMapping("/skuId/{skuId}") @ApiOperationSupport(order = 5) - public Result removeCartItem(@PathVariable Long skuId) { + public Result removeCartItem(@PathVariable Long skuId) { boolean result = cartService.removeCartItem(skuId); return Result.judge(result); } @@ -77,7 +82,7 @@ public class CartController { @ApiImplicitParam(name = "checked", value = "全选/全不选", required = true, paramType = "param", dataType = "Boolean") @PatchMapping("/_check") @ApiOperationSupport(order = 6) - public Result check(boolean checked) { + public Result check(boolean checked) { boolean result = cartService.checkAll(checked); return Result.judge(result); } diff --git a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/CartServiceImpl.java b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/CartServiceImpl.java index 7903a9892..47cbea4d5 100644 --- a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/CartServiceImpl.java +++ b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/CartServiceImpl.java @@ -1,6 +1,8 @@ package com.youlai.mall.oms.service.impl; import cn.hutool.core.lang.Assert; +import com.youlai.common.result.ResultCode; +import com.youlai.common.web.exception.BizException; import com.youlai.common.web.util.JwtUtils; import com.youlai.mall.oms.constant.OmsConstants; import com.youlai.mall.oms.pojo.dto.CartItemDTO; @@ -57,7 +59,12 @@ public class CartServiceImpl implements ICartService { */ @Override public boolean addCartItem(Long skuId) { - Long memberId = JwtUtils.getUserId(); + Long memberId; + try { + memberId = JwtUtils.getUserId(); + } catch (Exception e) { + throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED); + } BoundHashOperations cartHashOperations = getCartHashOperations(memberId); String hKey = skuId + ""; @@ -98,7 +105,12 @@ public class CartServiceImpl implements ICartService { */ @Override public boolean updateCartItem(CartItemDTO cartItem) { - Long memberId = JwtUtils.getUserId(); + Long memberId; + try { + memberId = JwtUtils.getUserId(); + } catch (Exception e) { + throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED); + } BoundHashOperations cartHashOperations = getCartHashOperations(memberId); String hKey = cartItem.getSkuId() + ""; if (cartHashOperations.get(hKey) != null) { @@ -119,7 +131,12 @@ public class CartServiceImpl implements ICartService { */ @Override public boolean removeCartItem(Long skuId) { - Long memberId = JwtUtils.getUserId(); + Long memberId; + try { + memberId = JwtUtils.getUserId(); + } catch (Exception e) { + throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED); + } BoundHashOperations cartHashOperations = getCartHashOperations(memberId); String hKey = skuId + ""; cartHashOperations.delete(hKey); @@ -132,7 +149,12 @@ public class CartServiceImpl implements ICartService { */ @Override public boolean checkAll(boolean checked) { - Long memberId = JwtUtils.getUserId(); + Long memberId; + try { + memberId = JwtUtils.getUserId(); + } catch (Exception e) { + throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED); + } BoundHashOperations cartHashOperations = getCartHashOperations(memberId); for (Object value : cartHashOperations.values()) { CartItemDTO cartItem = (CartItemDTO) value; @@ -150,7 +172,12 @@ public class CartServiceImpl implements ICartService { */ @Override public boolean removeCheckedItem() { - Long memberId = JwtUtils.getUserId(); + Long memberId; + try { + memberId = JwtUtils.getUserId(); + } catch (Exception e) { + throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED); + } BoundHashOperations cartHashOperations = getCartHashOperations(memberId); for (Object value : cartHashOperations.values()) { CartItemDTO cartItem = (CartItemDTO) value;