feat(0): [java]-[wechat-ferry-mvn]-1.对消息类型为49的XML进行解析转JSON
2.调用第三方服务时通过配置文件决定成功状态码
This commit is contained in:
parent
06cee96313
commit
adcc05dc7c
@ -1,6 +1,7 @@
|
|||||||
package com.wechat.ferry.config;
|
package com.wechat.ferry.config;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@ -63,4 +64,9 @@ public class WeChatFerryProperties {
|
|||||||
*/
|
*/
|
||||||
private List<String> sendMsgBackFwdUrls;
|
private List<String> sendMsgBackFwdUrls;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用第三方服务客户端成功状态码
|
||||||
|
*/
|
||||||
|
private Map<String, String> thirdPartyOkCodes;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package com.wechat.ferry.entity.dto;
|
package com.wechat.ferry.entity.dto;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@ -54,6 +57,21 @@ public class WxPpMsgDTO {
|
|||||||
@ApiModelProperty(value = "消息内容")
|
@ApiModelProperty(value = "消息内容")
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转为JSON的内容
|
||||||
|
* 对应DLL中的content原始内容
|
||||||
|
*/
|
||||||
|
@ApiModelProperty(value = "转为JSON的内容")
|
||||||
|
private JSONObject jsonContent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 引用内容
|
||||||
|
* 对应DLL中的content原始内容
|
||||||
|
*/
|
||||||
|
@JsonIgnore
|
||||||
|
@ApiModelProperty(value = "消息内容XML")
|
||||||
|
private String quoteContent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息发送者
|
* 消息发送者
|
||||||
*/
|
*/
|
||||||
|
@ -15,15 +15,29 @@ import lombok.Data;
|
|||||||
public class WxPpSendImageMsgResp {
|
public class WxPpSendImageMsgResp {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 类型编号
|
* 状态码
|
||||||
*/
|
*/
|
||||||
@ApiModelProperty(value = "类型编号")
|
@ApiModelProperty(value = "状态码")
|
||||||
private Integer id;
|
private String code;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 类型名称
|
* 返回信息
|
||||||
*/
|
*/
|
||||||
@ApiModelProperty(value = "类型名称")
|
@ApiModelProperty(value = "返回信息")
|
||||||
private String name;
|
private String msg;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片地址
|
||||||
|
*/
|
||||||
|
@ApiModelProperty(value = "图片地址")
|
||||||
|
private String path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息接收人
|
||||||
|
* 消息接收人,私聊为 wxid(wxid_xxxxxxxxxxxxxx)
|
||||||
|
* 群聊为 roomid(xxxxxxxxxx@chatroom)
|
||||||
|
*/
|
||||||
|
@ApiModelProperty(value = "消息接收人")
|
||||||
|
private String recipient;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
package com.wechat.ferry.enums;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 枚举-消息类型
|
|
||||||
*
|
|
||||||
* @author chandler
|
|
||||||
* @date 2024/10/01 15:55
|
|
||||||
*/
|
|
||||||
@Getter
|
|
||||||
@AllArgsConstructor
|
|
||||||
public enum WeChatMsgTypeEnum {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 0-未知
|
|
||||||
*/
|
|
||||||
UNKNOWN("0", "未知"),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 未匹配上
|
|
||||||
*/
|
|
||||||
UN_MATCH("", null),
|
|
||||||
|
|
||||||
// 结束
|
|
||||||
;
|
|
||||||
|
|
||||||
private final String code;
|
|
||||||
private final String name;
|
|
||||||
|
|
||||||
}
|
|
53
clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/enums/WxPpMsgTypeEnum.java
vendored
Normal file
53
clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/enums/WxPpMsgTypeEnum.java
vendored
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package com.wechat.ferry.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 枚举-本服务定义消息类型
|
||||||
|
*
|
||||||
|
* @author chandler
|
||||||
|
* @date 2024/10/01 15:55
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum WxPpMsgTypeEnum {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 0-未知
|
||||||
|
*/
|
||||||
|
UNKNOWN("0", "未知", "文本"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1-文本
|
||||||
|
*/
|
||||||
|
TEXT("1", "文本", "文本"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 2-表情符号
|
||||||
|
*/
|
||||||
|
EMOJI("2", "表情符号", "1"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 3-表情包(表情图片)
|
||||||
|
*/
|
||||||
|
EMOJI_IMAGE("3", "表情包(表情图片)", "47"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 4-引用消息
|
||||||
|
*/
|
||||||
|
QUOTE("4", "引用消息", "49"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 未匹配上
|
||||||
|
*/
|
||||||
|
UN_MATCH("", null, "文本"),
|
||||||
|
|
||||||
|
// 结束
|
||||||
|
;
|
||||||
|
|
||||||
|
private final String code;
|
||||||
|
private final String name;
|
||||||
|
private final String dllCode;
|
||||||
|
|
||||||
|
}
|
@ -8,6 +8,8 @@ import java.util.Map;
|
|||||||
import java.util.concurrent.ArrayBlockingQueue;
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.sun.jna.Native;
|
import com.sun.jna.Native;
|
||||||
import com.wechat.ferry.entity.dto.WxPpMsgDTO;
|
import com.wechat.ferry.entity.dto.WxPpMsgDTO;
|
||||||
@ -27,6 +29,7 @@ import com.wechat.ferry.entity.proto.Wcf.WxMsg;
|
|||||||
import com.wechat.ferry.enums.SexEnum;
|
import com.wechat.ferry.enums.SexEnum;
|
||||||
import com.wechat.ferry.service.SDK;
|
import com.wechat.ferry.service.SDK;
|
||||||
import com.wechat.ferry.utils.HttpClientUtil;
|
import com.wechat.ferry.utils.HttpClientUtil;
|
||||||
|
import com.wechat.ferry.utils.XmlJsonConvertUtil;
|
||||||
|
|
||||||
import io.sisu.nng.Socket;
|
import io.sisu.nng.Socket;
|
||||||
import io.sisu.nng.pair.Pair1Socket;
|
import io.sisu.nng.pair.Pair1Socket;
|
||||||
@ -573,12 +576,36 @@ public class WeChatSocketClient {
|
|||||||
dto.setType(msg.getType());
|
dto.setType(msg.getType());
|
||||||
dto.setTs(msg.getTs());
|
dto.setTs(msg.getTs());
|
||||||
dto.setRoomId(msg.getRoomid());
|
dto.setRoomId(msg.getRoomid());
|
||||||
dto.setContent(content);
|
|
||||||
dto.setSender(msg.getSender());
|
dto.setSender(msg.getSender());
|
||||||
dto.setSign(msg.getSign());
|
dto.setSign(msg.getSign());
|
||||||
dto.setThumb(msg.getThumb());
|
dto.setThumb(msg.getThumb());
|
||||||
dto.setExtra(msg.getExtra());
|
dto.setExtra(msg.getExtra());
|
||||||
dto.setXml(xml);
|
dto.setXml(xml);
|
||||||
|
// 根据消息类型判断 引用-49
|
||||||
|
if (!ObjectUtils.isEmpty(msg.getContent()) && "49".equals("" + msg.getType())) {
|
||||||
|
try {
|
||||||
|
dto.setQuoteContent(content);
|
||||||
|
JSONObject json = XmlJsonConvertUtil.xml2Json(content);
|
||||||
|
// 获取第一层级的JSONObject
|
||||||
|
JSONObject level1 = json.getJSONObject("msg");
|
||||||
|
if (!ObjectUtils.isEmpty(level1)) {
|
||||||
|
// 获取第二层级的JSONObject
|
||||||
|
JSONObject level2 = level1.getJSONObject("appmsg");
|
||||||
|
if (!ObjectUtils.isEmpty(level2)) {
|
||||||
|
// 获取field字段的值
|
||||||
|
String fieldValue = level2.getString("title");
|
||||||
|
dto.setContent(fieldValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dto.setJsonContent(json);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("XML提取报错:", e);
|
||||||
|
// 报错就使用原值
|
||||||
|
dto.setContent(content);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dto.setContent(content);
|
||||||
|
}
|
||||||
|
|
||||||
String jsonString = JSONObject.toJSONString(dto);
|
String jsonString = JSONObject.toJSONString(dto);
|
||||||
try {
|
try {
|
||||||
|
@ -285,6 +285,7 @@ public class WeChatDllServiceImpl implements WeChatDllService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxPpSendImageMsgResp sendImageMsg(WxPpSendImageMsgReq request) {
|
public WxPpSendImageMsgResp sendImageMsg(WxPpSendImageMsgReq request) {
|
||||||
|
WxPpSendImageMsgResp resp = new WxPpSendImageMsgResp();
|
||||||
int state = wechatSocketClient.sendImage(request.getPath(), request.getRecipient());
|
int state = wechatSocketClient.sendImage(request.getPath(), request.getRecipient());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.wechat.ferry.service.impl;
|
package com.wechat.ferry.service.impl;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -62,12 +64,12 @@ public class WeChatMsgServiceImpl implements WeChatMsgService {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
String responseStr = HttpClientUtil.doPostJson(receiveMsgFwdUrl, jsonString);
|
String responseStr = HttpClientUtil.doPostJson(receiveMsgFwdUrl, jsonString);
|
||||||
if (ObjectUtils.isEmpty(responseStr) || !JSONObject.parseObject(responseStr).getString("code").equals("200")) {
|
if (judgeSuccess(responseStr)) {
|
||||||
log.error("消息转发外部接口,获取响应状态失败!-URL:{}", receiveMsgFwdUrl);
|
log.error("[接收消息]-消息转发外部接口,获取响应状态失败!-URL:{}", receiveMsgFwdUrl);
|
||||||
}
|
}
|
||||||
log.debug("[接收消息]-[转发接收到的消息]-转发消息至:{}", receiveMsgFwdUrl);
|
log.debug("[接收消息]-[转发接收到的消息]-转发消息至:{}", receiveMsgFwdUrl);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("消息转发接口[{}]异常:", receiveMsgFwdUrl, e);
|
log.error("[接收消息]-消息转发接口[{}]服务异常:", receiveMsgFwdUrl, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,10 +80,12 @@ public class WeChatMsgServiceImpl implements WeChatMsgService {
|
|||||||
boolean passFlag = false;
|
boolean passFlag = false;
|
||||||
if (!ObjectUtils.isEmpty(responseStr)) {
|
if (!ObjectUtils.isEmpty(responseStr)) {
|
||||||
JSONObject jSONObject = JSONObject.parseObject(responseStr);
|
JSONObject jSONObject = JSONObject.parseObject(responseStr);
|
||||||
if (!ObjectUtils.isEmpty(jSONObject)) {
|
if (!ObjectUtils.isEmpty(jSONObject) && !CollectionUtils.isEmpty(weChatFerryProperties.getThirdPartyOkCodes())) {
|
||||||
if (!ObjectUtils.isEmpty(jSONObject.get("code"))) {
|
Map<String, String> codeMap = weChatFerryProperties.getThirdPartyOkCodes();
|
||||||
if (jSONObject.get("code").equals("200")) {
|
for (Map.Entry<String, String> entry : codeMap.entrySet()) {
|
||||||
return true;
|
if (!ObjectUtils.isEmpty(jSONObject.get(entry.getKey())) && jSONObject.get(entry.getKey()).equals(entry.getValue())) {
|
||||||
|
passFlag = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,12 @@ import org.dom4j.Document;
|
|||||||
import org.dom4j.DocumentException;
|
import org.dom4j.DocumentException;
|
||||||
import org.dom4j.DocumentHelper;
|
import org.dom4j.DocumentHelper;
|
||||||
import org.dom4j.Element;
|
import org.dom4j.Element;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONArray;
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.util.CollectionUtils;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class XmlJsonConvertUtil {
|
public class XmlJsonConvertUtil {
|
||||||
@ -52,6 +52,14 @@ public class XmlJsonConvertUtil {
|
|||||||
try {
|
try {
|
||||||
Document doc = DocumentHelper.parseText(xmlStr);
|
Document doc = DocumentHelper.parseText(xmlStr);
|
||||||
dom4j2Json(doc.getRootElement(), json);
|
dom4j2Json(doc.getRootElement(), json);
|
||||||
|
|
||||||
|
// 存放根节点
|
||||||
|
if (!ObjectUtils.isEmpty(json)) {
|
||||||
|
Element rootElement = doc.getRootElement();
|
||||||
|
JSONObject wrapperJson = new JSONObject();
|
||||||
|
wrapperJson.put(rootElement.getName(), json);
|
||||||
|
json = wrapperJson;
|
||||||
|
}
|
||||||
} catch (DocumentException e) {
|
} catch (DocumentException e) {
|
||||||
log.error("转换失败:", e);
|
log.error("转换失败:", e);
|
||||||
}
|
}
|
||||||
@ -142,10 +150,10 @@ public class XmlJsonConvertUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String xml = "<msgsource></msgsource>";
|
String xml = "<msg><appmsg appid=\"\" sdkver=\"0\"><title>就是个网页哇</title><des /><username /></appmsg></msg>";
|
||||||
|
|
||||||
log.info("xml格式化前:{}", xml);
|
log.info("xml格式化前:{}", xml);
|
||||||
xml = xml.replaceAll(">[\\s\\p{Zs}]*<","><");
|
xml = xml.replaceAll(">[\\s\\p{Zs}]*<", "><");
|
||||||
log.info("xml格式化后:{}", xml);
|
log.info("xml格式化后:{}", xml);
|
||||||
JSONObject json = xml2Json(xml);
|
JSONObject json = xml2Json(xml);
|
||||||
System.out.println("xml2Json:" + json.toJSONString());
|
System.out.println("xml2Json:" + json.toJSONString());
|
||||||
|
@ -38,14 +38,18 @@ wechat:
|
|||||||
receive-msg-fwd-switch: false
|
receive-msg-fwd-switch: false
|
||||||
# 接收消息转发URL
|
# 接收消息转发URL
|
||||||
receive-msg-fwd-urls:
|
receive-msg-fwd-urls:
|
||||||
- http://
|
- http://localhost:9001/msg
|
||||||
# 发送消息前转发开关
|
# 发送消息前转发开关
|
||||||
send-msg-front-fwd-switch: false
|
send-msg-front-fwd-switch: false
|
||||||
# 发送消息前转发URL
|
# 发送消息前转发URL
|
||||||
send-msg-front-fwd-urls:
|
send-msg-front-fwd-urls:
|
||||||
- http://
|
- http://localhost:9001/msg
|
||||||
# 发送消息后转发开关
|
# 发送消息后转发开关
|
||||||
send-msg-back-fwd-switch: false
|
send-msg-back-fwd-switch: false
|
||||||
# 发送消息后转发URL
|
# 发送消息后转发URL
|
||||||
send-msg-back-fwd-urls:
|
send-msg-back-fwd-urls:
|
||||||
- http://
|
- http://localhost:9001/msg
|
||||||
|
# 调用第三方服务客户端成功状态码
|
||||||
|
third-party-ok-codes:
|
||||||
|
# key:状态码字段 val:状态码值
|
||||||
|
code: '200'
|
||||||
|
Loading…
Reference in New Issue
Block a user