feat(0): [java]-[wcferry-mvn]-1.引入JSON格式化依赖,2.编写XML和JSON转换工具类
This commit is contained in:
parent
66d6655f50
commit
3c39723468
11
clients/java/wcferry-mvn/pom.xml
vendored
11
clients/java/wcferry-mvn/pom.xml
vendored
@ -48,6 +48,17 @@
|
||||
<artifactId>springfox-boot-starter</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
<!-- Alibaba Fastjson -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>2.0.52</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dom4j</groupId>
|
||||
<artifactId>dom4j</artifactId>
|
||||
<version>2.1.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.protobuf</groupId>
|
||||
|
@ -8,10 +8,10 @@ import java.util.Map;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
import com.iamteer.service.SDK;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.iamteer.entity.Wcf;
|
||||
import com.iamteer.entity.Wcf.DbQuery;
|
||||
import com.iamteer.entity.Wcf.DbRow;
|
||||
@ -25,11 +25,15 @@ import com.iamteer.entity.Wcf.RpcContact;
|
||||
import com.iamteer.entity.Wcf.UserInfo;
|
||||
import com.iamteer.entity.Wcf.Verification;
|
||||
import com.iamteer.entity.Wcf.WxMsg;
|
||||
import com.iamteer.entity.vo.response.WxMsgResp;
|
||||
import com.iamteer.service.SDK;
|
||||
import com.sun.jna.Native;
|
||||
|
||||
import io.sisu.nng.Socket;
|
||||
import io.sisu.nng.pair.Pair1Socket;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class Client {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Client.class);
|
||||
@ -78,7 +82,8 @@ public class Client {
|
||||
cmdSocket = new Pair1Socket();
|
||||
cmdSocket.dial(url);
|
||||
// logger.info("请点击登录微信");
|
||||
while (!isLogin()) { // 直到登录成功
|
||||
while (!isLogin()) {
|
||||
// 直到登录成功
|
||||
waitMs(1000);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -514,8 +519,22 @@ public class Client {
|
||||
}
|
||||
|
||||
public void printWxMsg(WxMsg msg) {
|
||||
logger.info("{}[{}]:{}:{}:{}\n{}", msg.getSender(), msg.getRoomid(), msg.getId(), msg.getType(),
|
||||
msg.getXml().replace("\n", "").replace("\t", ""), msg.getContent());
|
||||
WxMsgResp wxMsgResp = new WxMsgResp();
|
||||
wxMsgResp.setIsSelf(msg.getIsSelf());
|
||||
wxMsgResp.setIsGroup(msg.getIsGroup());
|
||||
wxMsgResp.setId(msg.getId());
|
||||
wxMsgResp.setType(msg.getType());
|
||||
wxMsgResp.setTs(msg.getTs());
|
||||
wxMsgResp.setRoomId(msg.getRoomid());
|
||||
wxMsgResp.setContent(msg.getContent());
|
||||
wxMsgResp.setSender(msg.getSender());
|
||||
wxMsgResp.setSign(msg.getSign());
|
||||
wxMsgResp.setThumb(msg.getThumb());
|
||||
wxMsgResp.setExtra(msg.getExtra());
|
||||
wxMsgResp.setXml(msg.getXml().replace("\n", "").replace("\t", ""));
|
||||
|
||||
String jsonString = JSONObject.toJSONString(wxMsgResp);
|
||||
log.info("收到消息: {}", jsonString);
|
||||
}
|
||||
|
||||
private String bytesToHex(byte[] bytes) {
|
||||
|
3
clients/java/wcferry-mvn/src/main/java/com/iamteer/entity/dto/.gitkeep
vendored
Normal file
3
clients/java/wcferry-mvn/src/main/java/com/iamteer/entity/dto/.gitkeep
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Ignore everything in this directory
|
||||
*
|
||||
# Except this file !.gitkeep
|
87
clients/java/wcferry-mvn/src/main/java/com/iamteer/entity/vo/response/WxMsgResp.java
vendored
Normal file
87
clients/java/wcferry-mvn/src/main/java/com/iamteer/entity/vo/response/WxMsgResp.java
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
package com.iamteer.entity.vo.response;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* DTO-微信消息
|
||||
*
|
||||
* @author chandler
|
||||
* @date 2024-09-26 19:56
|
||||
*/
|
||||
@Data
|
||||
public class WxMsgResp {
|
||||
|
||||
/**
|
||||
* 是否自己发送的
|
||||
*/
|
||||
@ApiModelProperty(value = "是否自己发送的")
|
||||
private Boolean isSelf;
|
||||
|
||||
/**
|
||||
* 是否群消息
|
||||
*/
|
||||
@ApiModelProperty(value = "是否群消息")
|
||||
private Boolean isGroup;
|
||||
|
||||
/**
|
||||
* 消息id
|
||||
*/
|
||||
@ApiModelProperty(value = "消息id")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 消息类型
|
||||
*/
|
||||
@ApiModelProperty(value = "消息类型")
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 消息类型
|
||||
*/
|
||||
@ApiModelProperty(value = "消息类型")
|
||||
private Integer ts;
|
||||
|
||||
/**
|
||||
* 群id(如果是群消息的话)
|
||||
*/
|
||||
@ApiModelProperty(value = "群id(如果是群消息的话)")
|
||||
private String roomId;
|
||||
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
@ApiModelProperty(value = "消息内容")
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 消息发送者
|
||||
*/
|
||||
@ApiModelProperty(value = "消息发送者")
|
||||
private String sender;
|
||||
|
||||
/**
|
||||
* 签名
|
||||
*/
|
||||
@ApiModelProperty(value = "签名")
|
||||
private String sign;
|
||||
|
||||
/**
|
||||
* 缩略图
|
||||
*/
|
||||
@ApiModelProperty(value = "缩略图")
|
||||
private String thumb;
|
||||
|
||||
/**
|
||||
* 附加内容
|
||||
*/
|
||||
@ApiModelProperty(value = "附加内容")
|
||||
private String extra;
|
||||
|
||||
/**
|
||||
* 消息xml
|
||||
*/
|
||||
@ApiModelProperty(value = "消息xml")
|
||||
private String xml;
|
||||
|
||||
}
|
130
clients/java/wcferry-mvn/src/main/java/com/iamteer/utils/XmlJsonConvertUtil.java
vendored
Normal file
130
clients/java/wcferry-mvn/src/main/java/com/iamteer/utils/XmlJsonConvertUtil.java
vendored
Normal file
@ -0,0 +1,130 @@
|
||||
package com.iamteer.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.List;
|
||||
|
||||
import org.dom4j.Attribute;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.DocumentHelper;
|
||||
import org.dom4j.Element;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class XmlJsonConvertUtil {
|
||||
|
||||
public static String readFile(String path) {
|
||||
String str = "";
|
||||
try {
|
||||
File file = new File(path);
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
FileChannel fc = fis.getChannel();
|
||||
ByteBuffer bb = ByteBuffer.allocate(new Long(file.length()).intValue());
|
||||
// fc向buffer中读入数据
|
||||
fc.read(bb);
|
||||
bb.flip();
|
||||
str = new String(bb.array(), "UTF8");
|
||||
fc.close();
|
||||
fis.close();
|
||||
} catch (Exception e) {
|
||||
log.error("异常:{} ", e.getMessage());
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* xml转json
|
||||
*
|
||||
* @param xmlStr
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject xml2Json(String xmlStr) {
|
||||
JSONObject json = new JSONObject();
|
||||
try {
|
||||
xmlStr = xmlStr.replace("<?xml version=\\\"1.0\\\"?>\\n", "");
|
||||
Document doc = DocumentHelper.parseText(xmlStr);
|
||||
dom4j2Json(doc.getRootElement(), json);
|
||||
} catch (DocumentException e) {
|
||||
log.error("异常:{} ", e.getMessage());
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* xml转json
|
||||
*
|
||||
* @param element
|
||||
* @param json
|
||||
*/
|
||||
public static void dom4j2Json(Element element, JSONObject json) {
|
||||
// 如果是属性
|
||||
for (Object o : element.attributes()) {
|
||||
Attribute attr = (Attribute)o;
|
||||
if (!isEmpty(attr.getValue())) {
|
||||
json.put("@" + attr.getName(), attr.getValue());
|
||||
}
|
||||
}
|
||||
List<Element> chdEl = element.elements();
|
||||
if (chdEl.isEmpty() && !isEmpty(element.getText())) {
|
||||
// 如果没有子元素,只有一个值
|
||||
json.put(element.getName(), element.getText());
|
||||
}
|
||||
|
||||
for (Element e : chdEl) {
|
||||
// 有子元素
|
||||
if (!e.elements().isEmpty()) {
|
||||
// 子元素也有子元素
|
||||
JSONObject chdjson = new JSONObject();
|
||||
dom4j2Json(e, chdjson);
|
||||
Object o = json.get(e.getName());
|
||||
if (o != null) {
|
||||
JSONArray jsona = null;
|
||||
if (o instanceof JSONObject) {
|
||||
// 如果此元素已存在,则转为jsonArray
|
||||
JSONObject jsono = (JSONObject)o;
|
||||
json.remove(e.getName());
|
||||
jsona = new JSONArray();
|
||||
jsona.add(jsono);
|
||||
jsona.add(chdjson);
|
||||
}
|
||||
if (o instanceof JSONArray) {
|
||||
jsona = (JSONArray)o;
|
||||
jsona.add(chdjson);
|
||||
}
|
||||
json.put(e.getName(), jsona);
|
||||
} else {
|
||||
if (!chdjson.isEmpty()) {
|
||||
json.put(e.getName(), chdjson);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// 子元素没有子元素
|
||||
for (Object o : element.attributes()) {
|
||||
Attribute attr = (Attribute)o;
|
||||
if (!isEmpty(attr.getValue())) {
|
||||
json.put("@" + attr.getName(), attr.getValue());
|
||||
}
|
||||
}
|
||||
if (!e.getText().isEmpty()) {
|
||||
json.put(e.getName(), e.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isEmpty(String str) {
|
||||
if (str == null || str.trim().isEmpty() || "null".equals(str)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user