From 5df9fba71492c16e89b63681d8a1cab6bfb198b2 Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Mon, 14 Nov 2022 00:35:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=8A=B6=E6=80=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oms/service/impl/OrderServiceImpl.java | 26 +++++++++---------- .../pms/service/impl/PmsSkuServiceImpl.java | 4 +-- .../com/youlai/common/result/ResultCode.java | 2 +- .../com/youlai/gateway/util/WebFluxUtils.java | 2 +- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java index e3381847c..1af54f7c1 100644 --- a/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java +++ b/mall-oms/oms-boot/src/main/java/com/youlai/mall/oms/service/impl/OrderServiceImpl.java @@ -25,7 +25,7 @@ import com.youlai.common.enums.BusinessTypeEnum; import com.youlai.common.redis.BusinessNoGenerator; import com.youlai.common.result.Result; import com.youlai.common.security.util.SecurityUtils; -import com.youlai.common.web.exception.BusinessException; +import com.youlai.common.web.exception.ApiException; import com.youlai.mall.oms.config.WxPayProperties; import com.youlai.mall.oms.dto.OrderInfoDTO; import com.youlai.mall.oms.enums.OrderStatusEnum; @@ -133,9 +133,9 @@ public class OrderServiceImpl extends ServiceImpl impleme }, threadPoolExecutor); // 获取会员收获地址 + Long memberId = SecurityUtils.getMemberId(); CompletableFuture getMemberAddressFuture = CompletableFuture.runAsync(() -> { RequestContextHolder.setRequestAttributes(attributes); - Long memberId = SecurityUtils.getMemberId(); Result> getMemberAddressResult = memberFeignClient.listMemberAddresses(memberId); List memberAddresses; if (Result.isSuccess(getMemberAddressResult) && (memberAddresses = getMemberAddressResult.getData()) != null) { @@ -208,7 +208,7 @@ public class OrderServiceImpl extends ServiceImpl impleme Assert.isTrue(result, "订单提交失败"); } catch (Exception e) { redisTemplate.opsForValue().set(ORDER_TOKEN_PREFIX + orderToken, orderToken); - throw new BusinessException(e); + throw new ApiException(e); } // 成功响应返回值构建 OrderSubmitVO submitVO = new OrderSubmitVO(); @@ -287,7 +287,7 @@ public class OrderServiceImpl extends ServiceImpl impleme wxPayService.closeOrderV3(order.getOutTradeNo()); } catch (WxPayException e) { log.error(e.getMessage(), e); - throw new BusinessException("微信关单异常"); + throw new ApiException("微信关单异常"); } } // 用户id前补零保证五位,对超出五位的保留后五位 @@ -310,7 +310,7 @@ public class OrderServiceImpl extends ServiceImpl impleme jsapiResult = wxPayService.createOrderV3(TradeTypeEnum.JSAPI, wxRequest); } catch (WxPayException e) { log.error(e.getMessage(), e); - throw new BusinessException("微信统一下单异常"); + throw new ApiException("微信统一下单异常"); } return jsapiResult; } @@ -329,7 +329,7 @@ public class OrderServiceImpl extends ServiceImpl impleme order.setOutTradeNo(null); } catch (WxPayException e) { log.error(e.getMessage(), e); - throw new BusinessException("微信关单异常"); + throw new ApiException("微信关单异常"); } } order.setStatus(OrderStatusEnum.AUTO_CANCEL.getCode()); @@ -341,11 +341,11 @@ public class OrderServiceImpl extends ServiceImpl impleme log.info("订单超时取消,订单ID:{}", id); OmsOrder order = this.getById(id); if (order == null) { - throw new BusinessException("订单不存在"); + throw new ApiException("订单不存在"); } if (!OrderStatusEnum.PENDING_PAYMENT.getCode().equals(order.getStatus())) { - throw new BusinessException("取消失败,订单状态不支持取消"); // 通过自定义异常,将异常信息抛出由异常处理器捕获显示给前端页面 + throw new ApiException("取消失败,订单状态不支持取消"); // 通过自定义异常,将异常信息抛出由异常处理器捕获显示给前端页面 } // 如果已经有outTradeNo了就先进行关单 if (PayTypeEnum.WX_JSAPI.getValue().equals(order.getPayType()) && StrUtil.isNotBlank(order.getOutTradeNo())) { @@ -354,7 +354,7 @@ public class OrderServiceImpl extends ServiceImpl impleme order.setOutTradeNo(null); } catch (WxPayException e) { log.error(e.getMessage(), e); - throw new BusinessException("微信关单异常"); + throw new ApiException("微信关单异常"); } } order.setStatus(OrderStatusEnum.USER_CANCEL.getCode()); @@ -363,7 +363,7 @@ public class OrderServiceImpl extends ServiceImpl impleme // 释放被锁定的库存 Result unlockResult = skuFeignClient.unlockStock(order.getOrderSn()); if (!Result.isSuccess(unlockResult)) { - throw new BusinessException(unlockResult.getMsg()); + throw new ApiException(unlockResult.getMsg()); } } return result; @@ -375,7 +375,7 @@ public class OrderServiceImpl extends ServiceImpl impleme log.info("=======================订单删除,订单ID:{}=======================", id); OmsOrder order = this.getById(id); if (order != null && !OrderStatusEnum.AUTO_CANCEL.getCode().equals(order.getStatus()) && !OrderStatusEnum.USER_CANCEL.getCode().equals(order.getStatus())) { - throw new BusinessException("订单删除失败,订单不存在或订单状态不支持删除"); + throw new ApiException("订单删除失败,订单不存在或订单状态不支持删除"); } return this.removeById(id); } @@ -467,9 +467,7 @@ public class OrderServiceImpl extends ServiceImpl impleme orderItemDTO.setCount(1); // 直接购买商品的数量为1 orderItems.add(orderItemDTO); } else { // 购物车结算 - Long memberId = SecurityUtils.getMemberId(); - log.info("购物车结算获取商品明细的memberId:{}", memberId); - List cartItems = cartService.listCartItemByMemberId(memberId); + List cartItems = cartService.listCartItems(); orderItems = cartItems.stream().filter(CartItemDTO::getChecked).map(cartItem -> { OrderItemDTO orderItemDTO = new OrderItemDTO(); BeanUtil.copyProperties(cartItem, orderItemDTO); diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSkuServiceImpl.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSkuServiceImpl.java index 8820d6523..822bce331 100644 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSkuServiceImpl.java +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSkuServiceImpl.java @@ -6,7 +6,7 @@ import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.youlai.common.web.exception.BusinessException; +import com.youlai.common.web.exception.ApiException; import com.youlai.mall.pms.common.constant.PmsConstants; import com.youlai.mall.pms.mapper.PmsSkuMapper; import com.youlai.mall.pms.pojo.dto.CheckPriceDTO; @@ -127,7 +127,7 @@ public class PmsSkuServiceImpl extends ServiceImpl impleme .setSql("locked_stock_num = locked_stock_num - " + item.getCount()) ); if (!result) { - throw new BusinessException("扣减库存失败,商品" + item.getSkuId() + "库存不足"); + throw new ApiException("扣减库存失败,商品" + item.getSkuId() + "库存不足"); } }); diff --git a/youlai-common/common-core/src/main/java/com/youlai/common/result/ResultCode.java b/youlai-common/common-core/src/main/java/com/youlai/common/result/ResultCode.java index 94c42ac18..656029c72 100644 --- a/youlai-common/common-core/src/main/java/com/youlai/common/result/ResultCode.java +++ b/youlai-common/common-core/src/main/java/com/youlai/common/result/ResultCode.java @@ -25,7 +25,7 @@ public enum ResultCode implements IResultCode, Serializable { USERNAME_OR_PASSWORD_ERROR("A0210", "用户名或密码错误"), PASSWORD_ENTER_EXCEED_LIMIT("A0211", "用户输入密码次数超限"), CLIENT_AUTHENTICATION_FAILED("A0212", "客户端认证失败"), - TOKEN_INVALID_OR_EXPIRED("A0230", "token无效或已过期"), + INVALID_TOKEN("A0230", "token无效或已过期"), TOKEN_ACCESS_FORBIDDEN("A0231", "token已被禁止访问"), AUTHORIZED_ERROR("A0300", "访问权限异常"), diff --git a/youlai-gateway/src/main/java/com/youlai/gateway/util/WebFluxUtils.java b/youlai-gateway/src/main/java/com/youlai/gateway/util/WebFluxUtils.java index 22ad235f7..6bb5214b1 100644 --- a/youlai-gateway/src/main/java/com/youlai/gateway/util/WebFluxUtils.java +++ b/youlai-gateway/src/main/java/com/youlai/gateway/util/WebFluxUtils.java @@ -24,7 +24,7 @@ public class WebFluxUtils { public static Mono writeResponse(ServerHttpResponse response, ResultCode resultCode) { switch (resultCode) { case ACCESS_UNAUTHORIZED: - case TOKEN_INVALID_OR_EXPIRED: + case INVALID_TOKEN: response.setStatusCode(HttpStatus.UNAUTHORIZED); break; case TOKEN_ACCESS_FORBIDDEN: