feat: 添加订单商品关联格口ID并优化微信授权逻辑

在`shop_order_goods`表中添加`cell_id`字段,用于关联订单商品与柜子格口。同时优化微信授权逻辑,改进token提取与处理方式,并增加日志记录。此外,调整订单服务中柜子格口的验证逻辑,确保订单商品已绑定柜子格口。
This commit is contained in:
dzq 2025-04-18 15:55:11 +08:00
parent ea22f44e41
commit 00bb295cb0
4 changed files with 33 additions and 16 deletions

View File

@ -67,23 +67,32 @@ public class ShopController {
} }
@GetMapping("/wechatAuth") @GetMapping("/wechatAuth")
public RedirectView wechatAuthRedirect(HttpServletRequest request, @RequestParam String token) { public RedirectView wechatAuthRedirect(HttpServletRequest request) {
/*java.util.StringJoiner joiner = new java.util.StringJoiner("&"); /*java.util.StringJoiner joiner = new java.util.StringJoiner("&");
request.getParameterMap().forEach((key, values) -> { request.getParameterMap().forEach((key, values) -> {
joiner.add(key + "=" + String.join(",", values)); joiner.add(key + "=" + String.join(",", values));
log.info("wechatAuth key: {} value: {}", key, values[0]);
}); });
log.info("wechatAuth 参数:{}", joiner.toString());*/ log.info("wechatAuth 参数:{}", joiner.toString());*/
String state = ""; // 从请求参数中提取包含token的参数名参数名本身可能包含"token="
if (StringUtils.isNoneBlank(token)) { String token = request.getParameterMap().keySet().stream()
state = "token_" + token; .filter(key -> key.contains("token="))
} .findFirst().orElse("");
/*try {
state = URLEncoder.encode(state, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
log.error("wechatAuth 编码失败", e);
}*/
log.info("wechatAuth token{}", token);
String state = "";
if (StringUtils.isNotBlank(token)) {
// 清洗token参数值原参数名格式为"token=xxx"
token = token.replace("token=", "");
// 构造带有token标识的state参数用于授权后回传
state = "token_" + token;
} else {
// 默认state参数保持微信要求的标准格式
state = "state";
}
// 构造微信网页授权URL
String authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize" String authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize"
+ "?appid=" + WeixinConstants.appid + "?appid=" + WeixinConstants.appid
+ "&redirect_uri=http%3A%2F%2Fwxshop.ab98.cn%2Fshop" + "&redirect_uri=http%3A%2F%2Fwxshop.ab98.cn%2Fshop"

View File

@ -73,6 +73,7 @@ public class OrderApplicationService {
throw new ApiException(ErrorCode.Client.COMMON_FORBIDDEN_TO_CALL, "订单状态不允许操作"); throw new ApiException(ErrorCode.Client.COMMON_FORBIDDEN_TO_CALL, "订单状态不允许操作");
} }
// 获取并验证订单商品信息
ShopOrderGoodsEntity goodsEntity = orderGoodsService.getById(orderGoodsId); ShopOrderGoodsEntity goodsEntity = orderGoodsService.getById(orderGoodsId);
if (null == goodsEntity || !orderId.equals(goodsEntity.getOrderId())) { if (null == goodsEntity || !orderId.equals(goodsEntity.getOrderId())) {
throw new ApiException(ErrorCode.Client.COMMON_FORBIDDEN_TO_CALL, "订单商品不存在"); throw new ApiException(ErrorCode.Client.COMMON_FORBIDDEN_TO_CALL, "订单商品不存在");
@ -80,19 +81,19 @@ public class OrderApplicationService {
if (goodsEntity.getStatus() != 1 && goodsEntity.getStatus()!= 5) { if (goodsEntity.getStatus() != 1 && goodsEntity.getStatus()!= 5) {
throw new ApiException(ErrorCode.Client.COMMON_FORBIDDEN_TO_CALL, "订单商品状态不允许操作"); throw new ApiException(ErrorCode.Client.COMMON_FORBIDDEN_TO_CALL, "订单商品状态不允许操作");
} }
if (goodsEntity.getCellId() == null) {
List<CabinetCellEntity> cabinetCellEntityList = cabinetCellService.selectByGoodsId(goodsEntity.getGoodsId()); throw new ApiException(ErrorCode.Client.COMMON_FORBIDDEN_TO_CALL, "订单商品未绑定柜子");
if (null == cabinetCellEntityList || cabinetCellEntityList.isEmpty()) {
throw new ApiException(ErrorCode.Client.COMMON_FORBIDDEN_TO_CALL, "商品未绑定柜子");
} }
CabinetCellEntity cabinetCellEntity = cabinetCellEntityList.get(0);
// 获取柜子单元和智能柜信息
CabinetCellEntity cabinetCellEntity = cabinetCellService.getById(goodsEntity.getCellId());
SmartCabinetEntity smartCabinet = smartCabinetService.getById(cabinetCellEntity.getCabinetId()); SmartCabinetEntity smartCabinet = smartCabinetService.getById(cabinetCellEntity.getCabinetId());
if (null == smartCabinet) { if (null == smartCabinet) {
throw new ApiException(ErrorCode.Client.COMMON_FORBIDDEN_TO_CALL, "柜子不存在"); throw new ApiException(ErrorCode.Client.COMMON_FORBIDDEN_TO_CALL, "柜子不存在");
} }
// 发送指令 // 构造MQTT开柜指令
// 指令格式8A + 锁控编号(2位HEX) + 引脚号(2位HEX) + 操作码11
String mqttDate = "8A"; String mqttDate = "8A";
mqttDate += String.format("%02X", smartCabinet.getLockControlNo()); mqttDate += String.format("%02X", smartCabinet.getLockControlNo());
mqttDate += String.format("%02X", cabinetCellEntity.getPinNo()); mqttDate += String.format("%02X", cabinetCellEntity.getPinNo());

View File

@ -40,6 +40,10 @@ public class ShopOrderGoodsEntity extends BaseEntity<ShopOrderGoodsEntity> {
@TableField("goods_id") @TableField("goods_id")
private Long goodsId; private Long goodsId;
@ApiModelProperty("关联格口ID")
@TableField("cell_id")
private Long cellId;
@ApiModelProperty("购买数量") @ApiModelProperty("购买数量")
@TableField("quantity") @TableField("quantity")
private Integer quantity; private Integer quantity;

View File

@ -69,3 +69,6 @@ ADD COLUMN `userid` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_c
ALTER TABLE `shop_order` ALTER TABLE `shop_order`
ADD COLUMN `name` varchar(30) DEFAULT NULL COMMENT '成员名称' AFTER `mobile`; ADD COLUMN `name` varchar(30) DEFAULT NULL COMMENT '成员名称' AFTER `mobile`;
ALTER TABLE `shop_order_goods`
ADD COLUMN `cell_id` BIGINT NULL COMMENT '格口ID' AFTER `goods_id`;