|  | @@ -3,7 +3,9 @@ package com.zanxiang.game.module.manage.service.impl;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 | 
	
		
			
				|  |  |  import com.zanxiang.erp.security.util.SecurityUtil;
 | 
	
		
			
				|  |  | +import com.zanxiang.game.back.base.pojo.enums.OrderStatusEnum;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.module.manage.constant.RedisKeyConstant;
 | 
	
		
			
				|  |  | +import com.zanxiang.game.module.manage.enums.ExpireTimeEnum;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.module.manage.enums.KfRoomMsgOwnerEnum;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.module.manage.enums.KfRoomMsgTypeEnum;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.module.manage.enums.KfWebSocketMsgEnum;
 | 
	
	
		
			
				|  | @@ -13,6 +15,7 @@ import com.zanxiang.game.module.manage.pojo.dto.PayApplicationDTO;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.module.manage.service.*;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.module.manage.service.api.KfWxApiService;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.module.manage.utils.FileUtil;
 | 
	
		
			
				|  |  | +import com.zanxiang.game.module.manage.utils.RedisUtil;
 | 
	
		
			
				|  |  |  import com.zanxiang.game.module.mybatis.entity.*;
 | 
	
		
			
				|  |  |  import com.zanxiang.module.oss.service.IOssService;
 | 
	
		
			
				|  |  |  import com.zanxiang.module.util.JsonUtil;
 | 
	
	
		
			
				|  | @@ -84,6 +87,9 @@ public class KfAppletMsgServiceImpl implements IKfAppletMsgService {
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private IKfRoomMsgService kfRoomMsgService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private RedisUtil<String> redisUtil;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public void appletMsg(String postData) {
 | 
	
		
			
				|  |  |          log.error("接收到SDK转发的小程序消息, postData : {}", postData);
 | 
	
	
		
			
				|  | @@ -94,10 +100,9 @@ public class KfAppletMsgServiceImpl implements IKfAppletMsgService {
 | 
	
		
			
				|  |  |          if (gameApplet == null) {
 | 
	
		
			
				|  |  |              return;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        //用户进入会话事件
 | 
	
		
			
				|  |  | -        if (Objects.equals(kfAppletMsgDTO.getMsgType(), KfAppletMsgDTO.MSG_TYPE_EVENT)
 | 
	
		
			
				|  |  | -                && Objects.equals(kfAppletMsgDTO.getEvent(), KfAppletMsgDTO.EVENT_USER_ENTER_TEMP_SESSION)) {
 | 
	
		
			
				|  |  | -            this.kfSessionUserUpdateSave(kfAppletMsgDTO, gameApplet);
 | 
	
		
			
				|  |  | +        //事件消息
 | 
	
		
			
				|  |  | +        if (Objects.equals(kfAppletMsgDTO.getMsgType(), KfAppletMsgDTO.MSG_TYPE_EVENT)) {
 | 
	
		
			
				|  |  | +            this.eventMsgHandle(gameApplet, kfAppletMsgDTO);
 | 
	
		
			
				|  |  |              return;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          //非玩家消息, 不做处理
 | 
	
	
		
			
				|  | @@ -109,9 +114,26 @@ public class KfAppletMsgServiceImpl implements IKfAppletMsgService {
 | 
	
		
			
				|  |  |                  .eq(KfRoom::getOpenId, kfAppletMsgDTO.getFromUserName())
 | 
	
		
			
				|  |  |                  .eq(KfRoom::getGameId, gameApplet.getGameId())
 | 
	
		
			
				|  |  |                  .eq(KfRoom::getOnline, Boolean.TRUE));
 | 
	
		
			
				|  |  | -        //消息存储
 | 
	
		
			
				|  |  | +        //构造房间消息
 | 
	
		
			
				|  |  |          KfWebSocketMsgDTO.MsgContentBean msgContent = this.getMsgContent(kfAppletMsgDTO);
 | 
	
		
			
				|  |  |          KfRoomMsg kfRoomMsg = this.transform(kfAppletMsgDTO, gameApplet, kfRoom, postData, msgContent);
 | 
	
		
			
				|  |  | +        //判断是否请求支付链接
 | 
	
		
			
				|  |  | +        String orderId = redisUtil.getCache(RedisKeyConstant.GAME_CUSTOM_PAY + kfAppletMsgDTO.getFromUserName());
 | 
	
		
			
				|  |  | +        if (Strings.isNotBlank(orderId) && Objects.equals(kfAppletMsgDTO.getContent(), KfAppletMsgDTO.MSG_CONTENT_PAY)) {
 | 
	
		
			
				|  |  | +            Order order = orderService.getOne(new LambdaQueryWrapper<Order>()
 | 
	
		
			
				|  |  | +                    .eq(Order::getOrderId, orderId)
 | 
	
		
			
				|  |  | +                    .and(qw -> qw.eq(Order::getStatus, OrderStatusEnum.READY_PAY.getValue())
 | 
	
		
			
				|  |  | +                            .or().eq(Order::getStatus, OrderStatusEnum.WAIT_PAY.getValue())
 | 
	
		
			
				|  |  | +                    )
 | 
	
		
			
				|  |  | +            );
 | 
	
		
			
				|  |  | +            if (order != null) {
 | 
	
		
			
				|  |  | +                this.sendCustomPayMessage(gameApplet.getGameId(), kfAppletMsgDTO.getFromUserName(), kfRoom, order);
 | 
	
		
			
				|  |  | +                kfRoomMsg.setReadStatus(Boolean.TRUE);
 | 
	
		
			
				|  |  | +                kfRoomMsgService.save(kfRoomMsg);
 | 
	
		
			
				|  |  | +                return;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //保存房间消息
 | 
	
		
			
				|  |  |          kfRoomMsgService.save(kfRoomMsg);
 | 
	
		
			
				|  |  |          //玩家状态更新为待接入状态
 | 
	
		
			
				|  |  |          if (kfRoom == null) {
 | 
	
	
		
			
				|  | @@ -125,6 +147,37 @@ public class KfAppletMsgServiceImpl implements IKfAppletMsgService {
 | 
	
		
			
				|  |  |          this.pushMessage(this.transform(kfRoom, gameApplet.getGameId(), kfRoomMsg, msgContent));
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    private void eventMsgHandle(GameApplet gameApplet, KfAppletMsgDTO kfAppletMsgDTO) {
 | 
	
		
			
				|  |  | +        //事件类型
 | 
	
		
			
				|  |  | +        String event = kfAppletMsgDTO.getEvent();
 | 
	
		
			
				|  |  | +        //非玩家打开会话消息, 不处理
 | 
	
		
			
				|  |  | +        if (!Objects.equals(event, KfAppletMsgDTO.EVENT_USER_ENTER_TEMP_SESSION)) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //保存玩家信息
 | 
	
		
			
				|  |  | +        this.kfSessionUserUpdateSave(kfAppletMsgDTO, gameApplet);
 | 
	
		
			
				|  |  | +        //判断玩家是否存在客服支付订单
 | 
	
		
			
				|  |  | +        String customPaySign = RedisKeyConstant.GAME_CUSTOM_PAY_SIGN + kfAppletMsgDTO.getFromUserName();
 | 
	
		
			
				|  |  | +        String orderId = redisUtil.getCache(customPaySign);
 | 
	
		
			
				|  |  | +        if (Strings.isBlank(orderId)) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //查询订单
 | 
	
		
			
				|  |  | +        Order order = orderService.getOne(new LambdaQueryWrapper<Order>()
 | 
	
		
			
				|  |  | +                .eq(Order::getOrderId, orderId)
 | 
	
		
			
				|  |  | +                .and(qw -> qw.eq(Order::getStatus, OrderStatusEnum.READY_PAY.getValue())
 | 
	
		
			
				|  |  | +                        .or().eq(Order::getStatus, OrderStatusEnum.WAIT_PAY.getValue())
 | 
	
		
			
				|  |  | +                )
 | 
	
		
			
				|  |  | +        );
 | 
	
		
			
				|  |  | +        if (order == null) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        this.sendCustomPayMessage(gameApplet.getGameId(), kfAppletMsgDTO.getFromUserName(), null, order);
 | 
	
		
			
				|  |  | +        redisUtil.deleteCache(customPaySign);
 | 
	
		
			
				|  |  | +        redisUtil.setCache(RedisKeyConstant.GAME_CUSTOM_PAY + kfAppletMsgDTO.getFromUserName(),
 | 
	
		
			
				|  |  | +                orderId, ExpireTimeEnum.FIVE_MIN.getTime());
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      private KfWebSocketMsgDTO transform(KfRoom kfRoom, Long gameId, KfRoomMsg kfRoomMsg, KfWebSocketMsgDTO.MsgContentBean msgContent) {
 | 
	
		
			
				|  |  |          //消息类型
 | 
	
		
			
				|  |  |          KfWebSocketMsgEnum kfWebSocketMsgEnum = kfRoom == null ? KfWebSocketMsgEnum.WEBSOCKET_MSG_WAIT_LIST
 | 
	
	
		
			
				|  | @@ -238,7 +291,7 @@ public class KfAppletMsgServiceImpl implements IKfAppletMsgService {
 | 
	
		
			
				|  |  |          return this.realmName + "image/" + fileName;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    private void sendSystemMessage(Long gameId, String openId, Order order) {
 | 
	
		
			
				|  |  | +    private void sendCustomPayMessage(Long gameId, String openId, KfRoom kfRoom, Order order) {
 | 
	
		
			
				|  |  |          //查询订单支付方式
 | 
	
		
			
				|  |  |          GamePayWay gamePayWay = gamePayWayService.getById(order.getGamePayWayId());
 | 
	
		
			
				|  |  |          //查询支付应用信息
 | 
	
	
		
			
				|  | @@ -265,12 +318,31 @@ public class KfAppletMsgServiceImpl implements IKfAppletMsgService {
 | 
	
		
			
				|  |  |          linkMap.put("description", "点我充值" + amount + "元,用于购买" + amount + "元档充值");
 | 
	
		
			
				|  |  |          linkMap.put("url", url);
 | 
	
		
			
				|  |  |          linkMap.put("thumb_url", thumbUrl);
 | 
	
		
			
				|  |  | -        //发送客服消息
 | 
	
		
			
				|  |  | +        //发送消息
 | 
	
		
			
				|  |  |          Map<String, Object> msgParamMap = new HashMap<>(3);
 | 
	
		
			
				|  |  |          msgParamMap.put("touser", openId);
 | 
	
		
			
				|  |  |          msgParamMap.put("msgtype", KfRoomMsgTypeEnum.KF_MSG_TYPE_LINK.getValue());
 | 
	
		
			
				|  |  |          msgParamMap.put(KfRoomMsgTypeEnum.KF_MSG_TYPE_LINK.getValue(), linkMap);
 | 
	
		
			
				|  |  |          kfWxApiService.sendCustomMessageApi(gameId, msgParamMap);
 | 
	
		
			
				|  |  | +        //返回发送的消息内容
 | 
	
		
			
				|  |  | +        kfRoomMsgService.save(this.transform(openId, gameId, kfRoom, JsonUtil.toString(msgParamMap)));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private KfRoomMsg transform(String openId, Long gameId, KfRoom kfRoom, String msgContent) {
 | 
	
		
			
				|  |  | +        return KfRoomMsg.builder()
 | 
	
		
			
				|  |  | +                .msgId(UUID.randomUUID().toString().replace("-", ""))
 | 
	
		
			
				|  |  | +                .msgType(KfRoomMsgTypeEnum.KF_MSG_TYPE_LINK.getValue())
 | 
	
		
			
				|  |  | +                .gameId(gameId)
 | 
	
		
			
				|  |  | +                .openId(openId)
 | 
	
		
			
				|  |  | +                .readStatus(kfRoom != null)
 | 
	
		
			
				|  |  | +                .roomId(kfRoom == null ? null : kfRoom.getId())
 | 
	
		
			
				|  |  | +                .kfUserId(kfRoom == null ? null : kfRoom.getKfUserId())
 | 
	
		
			
				|  |  | +                .msgOwner(KfRoomMsgOwnerEnum.KF_MSG_OWNER_SYSTEM.getValue())
 | 
	
		
			
				|  |  | +                .content(msgContent)
 | 
	
		
			
				|  |  | +                .source(msgContent)
 | 
	
		
			
				|  |  | +                .createTime(LocalDateTime.now())
 | 
	
		
			
				|  |  | +                .updateTime(LocalDateTime.now())
 | 
	
		
			
				|  |  | +                .build();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 |