refactor: 自定义异常状态优化

This commit is contained in:
haoxr 2022-11-14 00:35:12 +08:00
parent 6d493b6f45
commit 5df9fba714
4 changed files with 16 additions and 18 deletions

View File

@ -25,7 +25,7 @@ import com.youlai.common.enums.BusinessTypeEnum;
import com.youlai.common.redis.BusinessNoGenerator; import com.youlai.common.redis.BusinessNoGenerator;
import com.youlai.common.result.Result; import com.youlai.common.result.Result;
import com.youlai.common.security.util.SecurityUtils; 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.config.WxPayProperties;
import com.youlai.mall.oms.dto.OrderInfoDTO; import com.youlai.mall.oms.dto.OrderInfoDTO;
import com.youlai.mall.oms.enums.OrderStatusEnum; import com.youlai.mall.oms.enums.OrderStatusEnum;
@ -133,9 +133,9 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
}, threadPoolExecutor); }, threadPoolExecutor);
// 获取会员收获地址 // 获取会员收获地址
Long memberId = SecurityUtils.getMemberId();
CompletableFuture<Void> getMemberAddressFuture = CompletableFuture.runAsync(() -> { CompletableFuture<Void> getMemberAddressFuture = CompletableFuture.runAsync(() -> {
RequestContextHolder.setRequestAttributes(attributes); RequestContextHolder.setRequestAttributes(attributes);
Long memberId = SecurityUtils.getMemberId();
Result<List<MemberAddressDTO>> getMemberAddressResult = memberFeignClient.listMemberAddresses(memberId); Result<List<MemberAddressDTO>> getMemberAddressResult = memberFeignClient.listMemberAddresses(memberId);
List<MemberAddressDTO> memberAddresses; List<MemberAddressDTO> memberAddresses;
if (Result.isSuccess(getMemberAddressResult) && (memberAddresses = getMemberAddressResult.getData()) != null) { if (Result.isSuccess(getMemberAddressResult) && (memberAddresses = getMemberAddressResult.getData()) != null) {
@ -208,7 +208,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
Assert.isTrue(result, "订单提交失败"); Assert.isTrue(result, "订单提交失败");
} catch (Exception e) { } catch (Exception e) {
redisTemplate.opsForValue().set(ORDER_TOKEN_PREFIX + orderToken, orderToken); redisTemplate.opsForValue().set(ORDER_TOKEN_PREFIX + orderToken, orderToken);
throw new BusinessException(e); throw new ApiException(e);
} }
// 成功响应返回值构建 // 成功响应返回值构建
OrderSubmitVO submitVO = new OrderSubmitVO(); OrderSubmitVO submitVO = new OrderSubmitVO();
@ -287,7 +287,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
wxPayService.closeOrderV3(order.getOutTradeNo()); wxPayService.closeOrderV3(order.getOutTradeNo());
} catch (WxPayException e) { } catch (WxPayException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
throw new BusinessException("微信关单异常"); throw new ApiException("微信关单异常");
} }
} }
// 用户id前补零保证五位对超出五位的保留后五位 // 用户id前补零保证五位对超出五位的保留后五位
@ -310,7 +310,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
jsapiResult = wxPayService.createOrderV3(TradeTypeEnum.JSAPI, wxRequest); jsapiResult = wxPayService.createOrderV3(TradeTypeEnum.JSAPI, wxRequest);
} catch (WxPayException e) { } catch (WxPayException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
throw new BusinessException("微信统一下单异常"); throw new ApiException("微信统一下单异常");
} }
return jsapiResult; return jsapiResult;
} }
@ -329,7 +329,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
order.setOutTradeNo(null); order.setOutTradeNo(null);
} catch (WxPayException e) { } catch (WxPayException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
throw new BusinessException("微信关单异常"); throw new ApiException("微信关单异常");
} }
} }
order.setStatus(OrderStatusEnum.AUTO_CANCEL.getCode()); order.setStatus(OrderStatusEnum.AUTO_CANCEL.getCode());
@ -341,11 +341,11 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
log.info("订单超时取消订单ID{}", id); log.info("订单超时取消订单ID{}", id);
OmsOrder order = this.getById(id); OmsOrder order = this.getById(id);
if (order == null) { if (order == null) {
throw new BusinessException("订单不存在"); throw new ApiException("订单不存在");
} }
if (!OrderStatusEnum.PENDING_PAYMENT.getCode().equals(order.getStatus())) { if (!OrderStatusEnum.PENDING_PAYMENT.getCode().equals(order.getStatus())) {
throw new BusinessException("取消失败,订单状态不支持取消"); // 通过自定义异常将异常信息抛出由异常处理器捕获显示给前端页面 throw new ApiException("取消失败,订单状态不支持取消"); // 通过自定义异常将异常信息抛出由异常处理器捕获显示给前端页面
} }
// 如果已经有outTradeNo了就先进行关单 // 如果已经有outTradeNo了就先进行关单
if (PayTypeEnum.WX_JSAPI.getValue().equals(order.getPayType()) && StrUtil.isNotBlank(order.getOutTradeNo())) { if (PayTypeEnum.WX_JSAPI.getValue().equals(order.getPayType()) && StrUtil.isNotBlank(order.getOutTradeNo())) {
@ -354,7 +354,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
order.setOutTradeNo(null); order.setOutTradeNo(null);
} catch (WxPayException e) { } catch (WxPayException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
throw new BusinessException("微信关单异常"); throw new ApiException("微信关单异常");
} }
} }
order.setStatus(OrderStatusEnum.USER_CANCEL.getCode()); order.setStatus(OrderStatusEnum.USER_CANCEL.getCode());
@ -363,7 +363,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
// 释放被锁定的库存 // 释放被锁定的库存
Result<?> unlockResult = skuFeignClient.unlockStock(order.getOrderSn()); Result<?> unlockResult = skuFeignClient.unlockStock(order.getOrderSn());
if (!Result.isSuccess(unlockResult)) { if (!Result.isSuccess(unlockResult)) {
throw new BusinessException(unlockResult.getMsg()); throw new ApiException(unlockResult.getMsg());
} }
} }
return result; return result;
@ -375,7 +375,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
log.info("=======================订单删除订单ID{}=======================", id); log.info("=======================订单删除订单ID{}=======================", id);
OmsOrder order = this.getById(id); OmsOrder order = this.getById(id);
if (order != null && !OrderStatusEnum.AUTO_CANCEL.getCode().equals(order.getStatus()) && !OrderStatusEnum.USER_CANCEL.getCode().equals(order.getStatus())) { 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); return this.removeById(id);
} }
@ -467,9 +467,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
orderItemDTO.setCount(1); // 直接购买商品的数量为1 orderItemDTO.setCount(1); // 直接购买商品的数量为1
orderItems.add(orderItemDTO); orderItems.add(orderItemDTO);
} else { // 购物车结算 } else { // 购物车结算
Long memberId = SecurityUtils.getMemberId(); List<CartItemDTO> cartItems = cartService.listCartItems();
log.info("购物车结算获取商品明细的memberId:{}", memberId);
List<CartItemDTO> cartItems = cartService.listCartItemByMemberId(memberId);
orderItems = cartItems.stream().filter(CartItemDTO::getChecked).map(cartItem -> { orderItems = cartItems.stream().filter(CartItemDTO::getChecked).map(cartItem -> {
OrderItemDTO orderItemDTO = new OrderItemDTO(); OrderItemDTO orderItemDTO = new OrderItemDTO();
BeanUtil.copyProperties(cartItem, orderItemDTO); BeanUtil.copyProperties(cartItem, orderItemDTO);

View File

@ -6,7 +6,7 @@ import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 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.common.constant.PmsConstants;
import com.youlai.mall.pms.mapper.PmsSkuMapper; import com.youlai.mall.pms.mapper.PmsSkuMapper;
import com.youlai.mall.pms.pojo.dto.CheckPriceDTO; import com.youlai.mall.pms.pojo.dto.CheckPriceDTO;
@ -127,7 +127,7 @@ public class PmsSkuServiceImpl extends ServiceImpl<PmsSkuMapper, PmsSku> impleme
.setSql("locked_stock_num = locked_stock_num - " + item.getCount()) .setSql("locked_stock_num = locked_stock_num - " + item.getCount())
); );
if (!result) { if (!result) {
throw new BusinessException("扣减库存失败,商品" + item.getSkuId() + "库存不足"); throw new ApiException("扣减库存失败,商品" + item.getSkuId() + "库存不足");
} }
}); });

View File

@ -25,7 +25,7 @@ public enum ResultCode implements IResultCode, Serializable {
USERNAME_OR_PASSWORD_ERROR("A0210", "用户名或密码错误"), USERNAME_OR_PASSWORD_ERROR("A0210", "用户名或密码错误"),
PASSWORD_ENTER_EXCEED_LIMIT("A0211", "用户输入密码次数超限"), PASSWORD_ENTER_EXCEED_LIMIT("A0211", "用户输入密码次数超限"),
CLIENT_AUTHENTICATION_FAILED("A0212", "客户端认证失败"), CLIENT_AUTHENTICATION_FAILED("A0212", "客户端认证失败"),
TOKEN_INVALID_OR_EXPIRED("A0230", "token无效或已过期"), INVALID_TOKEN("A0230", "token无效或已过期"),
TOKEN_ACCESS_FORBIDDEN("A0231", "token已被禁止访问"), TOKEN_ACCESS_FORBIDDEN("A0231", "token已被禁止访问"),
AUTHORIZED_ERROR("A0300", "访问权限异常"), AUTHORIZED_ERROR("A0300", "访问权限异常"),

View File

@ -24,7 +24,7 @@ public class WebFluxUtils {
public static Mono<Void> writeResponse(ServerHttpResponse response, ResultCode resultCode) { public static Mono<Void> writeResponse(ServerHttpResponse response, ResultCode resultCode) {
switch (resultCode) { switch (resultCode) {
case ACCESS_UNAUTHORIZED: case ACCESS_UNAUTHORIZED:
case TOKEN_INVALID_OR_EXPIRED: case INVALID_TOKEN:
response.setStatusCode(HttpStatus.UNAUTHORIZED); response.setStatusCode(HttpStatus.UNAUTHORIZED);
break; break;
case TOKEN_ACCESS_FORBIDDEN: case TOKEN_ACCESS_FORBIDDEN: