Commit f778297b7396b1dd6f2d14a3ac4234bf4b8c7ca0

Authored by chenyonghong
1 parent c85971ed

fix -> 发送IM消息 -> 补充消息业务类型从redis获取失败从db查询

fw-hermes-service/src/main/java/cn/fw/hermes/service/biz/MessageBizService.java
... ... @@ -2,6 +2,7 @@ package cn.fw.hermes.service.biz;
2 2  
3 3 import cn.fw.common.util.MessageCodeBuilder;
4 4 import cn.fw.hermes.common.constant.ActionStatusEnum;
  5 +import cn.fw.hermes.common.constant.Constant;
5 6 import cn.fw.hermes.common.constant.RedisKey;
6 7 import cn.fw.hermes.common.constant.RedisKeyCache;
7 8 import cn.fw.hermes.common.utils.StringUtils;
... ... @@ -9,6 +10,7 @@ import cn.fw.hermes.common.utils.UUIDUtils;
9 10 import cn.fw.hermes.common.utils.im.RedisKeyUtil;
10 11 import cn.fw.hermes.common.utils.im.TXCloudUtils;
11 12 import cn.fw.hermes.domain.db.Account;
  13 +import cn.fw.hermes.domain.db.MessageBusinessType;
12 14 import cn.fw.hermes.domain.db.SysMsg;
13 15 import cn.fw.hermes.domain.dto.AdminConfigDto;
14 16 import cn.fw.hermes.domain.dto.MsgBodyDto;
... ... @@ -78,7 +80,7 @@ public class MessageBizService {
78 80 msgSubject.setCondition(condition);
79 81 msgSubject.setMsgBodyDto(msgBodyDto);
80 82 redisTemplate.opsForList()
81   - .leftPush(RedisKey.ASYN_MESSAGE_TARGET.getCode(), JSON.toJSONString(msgSubject));
  83 + .leftPush(RedisKey.ASYN_MESSAGE_TARGET.getCode(), JSON.toJSONString(msgSubject));
82 84 return messageId;
83 85 }
84 86  
... ... @@ -113,23 +115,23 @@ public class MessageBizService {
113 115 if (msgPara.isUseOfflineMsg()) {
114 116 OfflinePushInfo pushInfo = msgPara.getOfflinePushInfo();
115 117 info = TXCloudUtils.getOfflinePushInfo(pushInfo.getTitle(), pushInfo.getDesc(),
116   - JSON.toJSONString(pushInfo.getExt()));
  118 + JSON.toJSONString(pushInfo.getExt()));
117 119 }
118 120 //获取管理员签名
119 121 String adminKey = RedisKeyUtil.getAdminkey(adminConfigDto.getAdminId());
120 122 String adminSig = accountBizService.getSig(adminConfigDto.getAdminId(), 360 * 24 * 10, adminKey)
121   - .getSig();
  123 + .getSig();
122 124 //调用IM发送消息接口发送消息
123 125 JSONObject jsonObject = TXCloudUtils
124   - .openimSendMsg(msgBodyDto.getFromAccount(), msgBodyDto.getToAccount(),
125   - msgBodyDto.getMsgBody(), adminSig,
126   - adminConfigDto.getAdminId(), adminConfigDto.getAppId(), msgPara.getMsgLifeTime(),
127   - msgPara.getMsgTypeEnum(), info);
  126 + .openimSendMsg(msgBodyDto.getFromAccount(), msgBodyDto.getToAccount(),
  127 + msgBodyDto.getMsgBody(), adminSig,
  128 + adminConfigDto.getAdminId(), adminConfigDto.getAppId(), msgPara.getMsgLifeTime(),
  129 + msgPara.getMsgTypeEnum(), info);
128 130 //是否发送成功
129 131 BV.isTrue(jsonObject.get("ActionStatus").equals(ActionStatusEnum.OK.getCode()) && 0 == jsonObject.getInteger("ErrorCode"),
130   - MessageCodeBuilder
131   - .messageCode(jsonObject.getInteger("ErrorCode"), "调用IM消息发送接口失败"),
132   - "调用IM消息发送接口失败");
  132 + MessageCodeBuilder
  133 + .messageCode(jsonObject.getInteger("ErrorCode"), "调用IM消息发送接口失败"),
  134 + "调用IM消息发送接口失败");
133 135 SysMsg sysMsg = msgBodyDto.getSysMsg();
134 136 sysMsg.setMessageId(msgPara.getMessageId());
135 137 sysMsg.setMsgStatusDesc(ActionStatusEnum.OK.getMsg());
... ... @@ -155,7 +157,7 @@ public class MessageBizService {
155 157 } catch (Exception ex) {
156 158 //放入redis队列
157 159 redisTemplate.opsForList()
158   - .leftPush(RedisKey.SAVE_MESSAGE_FAILED.getCode(), JSON.toJSONString(sysMsg));
  160 + .leftPush(RedisKey.SAVE_MESSAGE_FAILED.getCode(), JSON.toJSONString(sysMsg));
159 161 }
160 162 return result;
161 163 }
... ... @@ -172,18 +174,18 @@ public class MessageBizService {
172 174 BV.isTrue(msgBodyDto.getMsgBody() != null, "消息内容不合法");
173 175 //如果消息是自定义消息,则检查对应的消息业务类型是否存在
174 176 if (msgBodyDto.getMsgTypeEnum().getCode().equals(MsgTypeEnum.CUSTOM.getCode())) {
175   - BV.notNull(msgPara.getCustomContent().getExt(),"扩展字段参数不能为空");
  177 + BV.notNull(msgPara.getCustomContent().getExt(), "扩展字段参数不能为空");
176 178 this.checkExt(msgPara);
177 179 }
178 180 //获取发送方用户识别码
179 181 String fromAccount = (String) redisTemplate.opsForHash()
180   - .get(RedisKeyCache.SYS_KEY, msgPara.getBusinessType() == null ?
181   - BusinessType.SYS_NOTICE.getCode().toString()
182   - : msgPara.getBusinessType().getCode().toString());
  182 + .get(RedisKeyCache.SYS_KEY, msgPara.getBusinessType() == null ?
  183 + BusinessType.SYS_NOTICE.getCode().toString()
  184 + : msgPara.getBusinessType().getCode().toString());
183 185 BV.isTrue(!StringUtils.isEmpty(fromAccount), "发送方用户不存在");
184 186 //获取接收方用户识别码
185 187 Optional<Account> optional = accountService.queryAccountByUserId(msgPara.getUserId(),
186   - msgPara.getIsStaff() ? UserTypeEnum.B.getValue() : UserTypeEnum.C.getValue());
  188 + msgPara.getIsStaff() ? UserTypeEnum.B.getValue() : UserTypeEnum.C.getValue());
187 189 String toAccount;
188 190 //如果消息接收人未注册,将该用户注册
189 191 if (!optional.isPresent()) {
... ... @@ -211,7 +213,12 @@ public class MessageBizService {
211 213 Map<String, Object> extMap = msgPara.getCustomContent().getExt();
212 214 BV.notNull(extMap.get("type"), "消息业务类型不能为空");
213 215 String type = extMap.get("type").toString();
214   - BV.isTrue(messageBusinessTypeBizService.getTypeFromRedis(type), () -> "消息业务类型不正确");
  216 + Boolean flag = messageBusinessTypeBizService.getTypeFromRedis(type);
  217 + if (!flag) {
  218 + Optional<MessageBusinessType> optional = messageBusinessTypeBizService.getMessageBusinessType(type);
  219 + BV.isTrue(optional.isPresent(), () -> "消息业务类型不正确");
  220 + redisTemplate.opsForHash().put(RedisKey.MESSAGE_BUSINESS_TYPE.getCode(), optional.get().getType(),"1");
  221 + }
215 222 }
216 223  
217 224 }
... ...
fw-hermes-service/src/main/java/cn/fw/hermes/service/biz/MessageBusinessTypeBizService.java
... ... @@ -7,8 +7,10 @@ import cn.fw.hermes.service.data.MessageBusinessTypeService;
7 7  
8 8 import java.util.List;
9 9 import java.util.Map;
  10 +import java.util.Optional;
10 11 import java.util.stream.Collectors;
11 12  
  13 +import com.baomidou.mybatisplus.core.toolkit.Wrappers;
12 14 import lombok.RequiredArgsConstructor;
13 15 import lombok.extern.slf4j.Slf4j;
14 16 import org.springframework.data.redis.core.StringRedisTemplate;
... ... @@ -62,9 +64,9 @@ public class MessageBusinessTypeBizService {
62 64 */
63 65 public Boolean cacheAllTypeToRedis() {
64 66 List<MessageBusinessType> typeList = messageBusinessTypeService
65   - .queryAllMessageType();
  67 + .queryAllMessageType();
66 68 Map<String, String> map = typeList.stream()
67   - .collect(Collectors.toMap(MessageBusinessType::getType, (r) -> "1"));
  69 + .collect(Collectors.toMap(MessageBusinessType::getType, (r) -> "1"));
68 70 redisTemplate.opsForHash().putAll(RedisKey.MESSAGE_BUSINESS_TYPE.getCode(), map);
69 71 log.info("缓存消息业务类型到redis操作完成");
70 72 return true;
... ... @@ -100,9 +102,20 @@ public class MessageBusinessTypeBizService {
100 102 */
101 103 public Boolean getTypeFromRedis(String key) {
102 104 String messageType = (String) redisTemplate.opsForHash()
103   - .get(RedisKey.MESSAGE_BUSINESS_TYPE.getCode(), key);
  105 + .get(RedisKey.MESSAGE_BUSINESS_TYPE.getCode(), key);
104 106 return !StringUtils.isEmpty(messageType);
105 107 }
106 108  
  109 + /**
  110 + * 通过type查询业务类型
  111 + *
  112 + * @param type 类型
  113 + * @return 业务类型
  114 + */
  115 + public Optional<MessageBusinessType> getMessageBusinessType(String type) {
  116 + return Optional.ofNullable(messageBusinessTypeService.getOne(
  117 + Wrappers.<MessageBusinessType>lambdaQuery().eq(MessageBusinessType::getType, type)));
  118 + }
  119 +
107 120  
108 121 }
109 122 \ No newline at end of file
... ...
fw-hermes-service/src/main/java/cn/fw/hermes/service/mq/MessageProducer.java
... ... @@ -20,7 +20,7 @@ public class MessageProducer {
20 20 final String msg = "消息发送mq";
21 21 try {
22 22 log.info("{} : body [{}]", msg, messageResult);
23   - rocketMQTemplate.syncSend(MessageResult.TOPIC, messageResult);
  23 + rocketMQTemplate.syncSend(MessageResult.TOPIC + ":*", messageResult);
24 24 } catch (Exception e) {
25 25 log.error("{} 消息结果发送失败 messageId[{}]", msg, messageResult.getMessageId());
26 26 }
... ...