diff --git a/src/main/java/com/hotlcc/wechat4j/Wechat.java b/src/main/java/com/hotlcc/wechat4j/Wechat.java index d75a870..9c30af3 100644 --- a/src/main/java/com/hotlcc/wechat4j/Wechat.java +++ b/src/main/java/com/hotlcc/wechat4j/Wechat.java @@ -10,6 +10,7 @@ import com.hotlcc.wechat4j.enums.SelectorEnum; import com.hotlcc.wechat4j.handler.ExitEventHandler; import com.hotlcc.wechat4j.handler.ReceivedMsgHandler; import com.hotlcc.wechat4j.model.ReceivedMsg; +import com.hotlcc.wechat4j.model.UserInfo; import com.hotlcc.wechat4j.util.CommonUtil; import com.hotlcc.wechat4j.util.PropertiesUtil; import com.hotlcc.wechat4j.util.QRCodeUtil; @@ -57,11 +58,11 @@ public class Wechat { private volatile String skey; private volatile String wxuin; //用户数据 - private volatile JSONObject loginUser; + private volatile UserInfo loginUser; private final Lock loginUserLock = new ReentrantLock(); private volatile JSONObject SyncKey; private final Lock SyncKeyLock = new ReentrantLock(); - private volatile JSONArray ContactList; + private volatile List ContactList; private final Lock ContactListLock = new ReentrantLock(); //在线状态 private volatile boolean isOnline = false; @@ -388,7 +389,7 @@ public class Wechat { return false; } - loginUser = result.getJSONObject("User"); + loginUser = UserInfo.valueOf(result.getJSONObject("User")); SyncKey = result.getJSONObject("SyncKey"); return true; @@ -774,7 +775,7 @@ public class Wechat { private void processNewMsg(JSONObject AddMsg) { try { - ReceivedMsg msg = new ReceivedMsg(AddMsg); + ReceivedMsg msg = ReceivedMsg.valueOf(AddMsg); processNewMsg(msg); } catch (Exception e) { logger.error("Execute processNewMsg error.", e); @@ -812,7 +813,7 @@ public class Wechat { * * @return */ - public JSONObject getLoginUser(boolean update) { + public UserInfo getLoginUser(boolean update) { if (loginUser == null || update) { JSONObject result = webWeixinApi.webWeixinInit(httpClient, passTicket, wxsid, skey, wxuin); if (result == null) { @@ -832,7 +833,7 @@ public class Wechat { try { loginUserLock.lock(); if (loginUser == null || update) { - loginUser = result.getJSONObject("User"); + loginUser = UserInfo.valueOf(result.getJSONObject("User")); } } finally { loginUserLock.unlock(); @@ -849,11 +850,11 @@ public class Wechat { * @return */ public String getLoginUserName(boolean update) { - JSONObject loginUser = getLoginUser(update); + UserInfo loginUser = getLoginUser(update); if (loginUser == null) { return null; } - return loginUser.getString("UserName"); + return loginUser.getUserName(); } /** @@ -862,11 +863,11 @@ public class Wechat { * @return */ public String getLoginUserNickName(boolean update) { - JSONObject loginUser = getLoginUser(update); + UserInfo loginUser = getLoginUser(update); if (loginUser == null) { return null; } - return loginUser.getString("NickName"); + return loginUser.getNickName(); } /** @@ -926,7 +927,7 @@ public class Wechat { * @param update * @return */ - public JSONArray getContactList(boolean update) { + public List getContactList(boolean update) { if (ContactList == null || update) { JSONObject result = webWeixinApi.getContact(httpClient, passTicket, skey); if (result == null) { @@ -946,7 +947,7 @@ public class Wechat { try { ContactListLock.lock(); if (ContactList == null || update) { - ContactList = result.getJSONArray("MemberList"); + ContactList = UserInfo.valueOf(result.getJSONArray("MemberList")); } } finally { ContactListLock.unlock(); @@ -964,24 +965,23 @@ public class Wechat { * @param UserName * @return */ - public JSONObject getContactByUserName(boolean update, String UserName) { + public UserInfo getContactByUserName(boolean update, String UserName) { if (StringUtil.isEmpty(UserName)) { return null; } - JSONArray list = getContactList(update); + List list = getContactList(update); if (list == null) { return null; } - for (int i = 0, len = list.size(); i < len; i++) { - JSONObject c = list.getJSONObject(i); - if (c == null) { + for (UserInfo userInfo : list) { + if (userInfo == null) { continue; } - if (UserName.equals(c.getString("UserName"))) { - return c; + if (UserName.equals(userInfo.getUserName())) { + return userInfo; } } @@ -995,24 +995,23 @@ public class Wechat { * @param NickName * @return */ - public JSONObject getContactByNickName(boolean update, String NickName) { + public UserInfo getContactByNickName(boolean update, String NickName) { if (StringUtil.isEmpty(NickName)) { return null; } - JSONArray list = getContactList(update); + List list = getContactList(update); if (list == null) { return null; } - for (int i = 0, len = list.size(); i < len; i++) { - JSONObject c = list.getJSONObject(i); - if (c == null) { + for (UserInfo userInfo : list) { + if (userInfo == null) { continue; } - if (NickName.equals(c.getString("NickName"))) { - return c; + if (NickName.equals(userInfo.getNickName())) { + return userInfo; } } @@ -1026,24 +1025,23 @@ public class Wechat { * @param RemarkName * @return */ - public JSONObject getContactByRemarkName(boolean update, String RemarkName) { + public UserInfo getContactByRemarkName(boolean update, String RemarkName) { if (StringUtil.isEmpty(RemarkName)) { return null; } - JSONArray list = getContactList(update); + List list = getContactList(update); if (list == null) { return null; } - for (int i = 0, len = list.size(); i < len; i++) { - JSONObject c = list.getJSONObject(i); - if (c == null) { + for (UserInfo userInfo : list) { + if (userInfo == null) { continue; } - if (RemarkName.equals(c.getString("RemarkName"))) { - return c; + if (RemarkName.equals(userInfo.getRemarkName())) { + return userInfo; } } diff --git a/src/main/java/com/hotlcc/wechat4j/model/AppInfo.java b/src/main/java/com/hotlcc/wechat4j/model/AppInfo.java index 0f3d325..46905fa 100644 --- a/src/main/java/com/hotlcc/wechat4j/model/AppInfo.java +++ b/src/main/java/com/hotlcc/wechat4j/model/AppInfo.java @@ -1,19 +1,18 @@ package com.hotlcc.wechat4j.model; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import java.util.ArrayList; +import java.util.List; + public final class AppInfo { + private AppInfo() { + } + private Integer Type; private String AppID; - public AppInfo(JSONObject info) { - if (info == null) { - return; - } - this.Type = info.getInteger("Type"); - this.AppID = info.getString("AppID"); - } - public Integer getType() { return Type; } @@ -21,4 +20,27 @@ public final class AppInfo { public String getAppID() { return AppID; } + + public static AppInfo valueOf(JSONObject info) { + if (info == null) { + return null; + } + AppInfo appInfo = new AppInfo(); + appInfo.Type = info.getInteger("Type"); + appInfo.AppID = info.getString("AppID"); + return appInfo; + } + + public static List valueOf(JSONArray infos) { + if (infos == null) { + return null; + } + + List appInfos = new ArrayList<>(); + for (int i = 0, len = infos.size(); i < len; i++) { + JSONObject info = infos.getJSONObject(i); + appInfos.add(AppInfo.valueOf(info)); + } + return appInfos; + } } diff --git a/src/main/java/com/hotlcc/wechat4j/model/ReceivedMsg.java b/src/main/java/com/hotlcc/wechat4j/model/ReceivedMsg.java index 21c921c..2035bf5 100644 --- a/src/main/java/com/hotlcc/wechat4j/model/ReceivedMsg.java +++ b/src/main/java/com/hotlcc/wechat4j/model/ReceivedMsg.java @@ -1,39 +1,13 @@ package com.hotlcc.wechat4j.model; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import java.util.ArrayList; +import java.util.List; + public final class ReceivedMsg { - public ReceivedMsg(JSONObject msg) { - if (msg == null) { - return; - } - this.SubMsgType = msg.getInteger("SubMsgType"); - this.VoiceLength = msg.getLong("VoiceLength"); - this.FileName = msg.getString("FileName"); - this.ImgHeight = msg.getLong("ImgHeight"); - this.ToUserName = msg.getString("ToUserName"); - this.HasProductId = msg.getLong("HasProductId"); - this.ImgStatus = msg.getInteger("ImgStatus"); - this.Url = msg.getString("Url"); - this.ImgWidth = msg.getInteger("ImgWidth"); - this.ForwardFlag = msg.getInteger("ForwardFlag"); - this.Status = msg.getInteger("Status"); - this.Ticket = msg.getString("Ticket"); - this.RecommendInfo = new RecommendInfo(msg.getJSONObject("RecommendInfo")); - this.CreateTime = msg.getLong("CreateTime"); - this.NewMsgId = msg.getLong("NewMsgId"); - this.MsgType = msg.getInteger("MsgType"); - this.EncryFileName = msg.getString("EncryFileName"); - this.MsgId = msg.getString("MsgId"); - this.StatusNotifyCode = msg.getInteger("StatusNotifyCode"); - this.AppInfo = new AppInfo(msg.getJSONObject("AppInfo")); - this.PlayLength = msg.getLong("PlayLength"); - this.MediaId = msg.getString("MediaId"); - this.Content = msg.getString("Content"); - this.StatusNotifyUserName = msg.getString("StatusNotifyUserName"); - this.FromUserName = msg.getString("FromUserName"); - this.OriContent = msg.getString("OriContent"); - this.FileSize = msg.getString("FileSize"); + private ReceivedMsg() { } private Integer SubMsgType; @@ -141,7 +115,7 @@ public final class ReceivedMsg { return StatusNotifyCode; } - public com.hotlcc.wechat4j.model.AppInfo getAppInfo() { + public AppInfo getAppInfo() { return AppInfo; } @@ -176,4 +150,55 @@ public final class ReceivedMsg { public String getFileSize() { return FileSize; } + + public static ReceivedMsg valueOf(JSONObject msg) { + if (msg == null) { + return null; + } + + ReceivedMsg receivedMsg = new ReceivedMsg(); + + receivedMsg.SubMsgType = msg.getInteger("SubMsgType"); + receivedMsg.VoiceLength = msg.getLong("VoiceLength"); + receivedMsg.FileName = msg.getString("FileName"); + receivedMsg.ImgHeight = msg.getLong("ImgHeight"); + receivedMsg.ToUserName = msg.getString("ToUserName"); + receivedMsg.HasProductId = msg.getLong("HasProductId"); + receivedMsg.ImgStatus = msg.getInteger("ImgStatus"); + receivedMsg.Url = msg.getString("Url"); + receivedMsg.ImgWidth = msg.getInteger("ImgWidth"); + receivedMsg.ForwardFlag = msg.getInteger("ForwardFlag"); + receivedMsg.Status = msg.getInteger("Status"); + receivedMsg.Ticket = msg.getString("Ticket"); + receivedMsg.RecommendInfo = com.hotlcc.wechat4j.model.RecommendInfo.valueOf(msg.getJSONObject("RecommendInfo")); + receivedMsg.CreateTime = msg.getLong("CreateTime"); + receivedMsg.NewMsgId = msg.getLong("NewMsgId"); + receivedMsg.MsgType = msg.getInteger("MsgType"); + receivedMsg.EncryFileName = msg.getString("EncryFileName"); + receivedMsg.MsgId = msg.getString("MsgId"); + receivedMsg.StatusNotifyCode = msg.getInteger("StatusNotifyCode"); + receivedMsg.AppInfo = com.hotlcc.wechat4j.model.AppInfo.valueOf(msg.getJSONObject("AppInfo")); + receivedMsg.PlayLength = msg.getLong("PlayLength"); + receivedMsg.MediaId = msg.getString("MediaId"); + receivedMsg.Content = msg.getString("Content"); + receivedMsg.StatusNotifyUserName = msg.getString("StatusNotifyUserName"); + receivedMsg.FromUserName = msg.getString("FromUserName"); + receivedMsg.OriContent = msg.getString("OriContent"); + receivedMsg.FileSize = msg.getString("FileSize"); + + return receivedMsg; + } + + public static List valueOf(JSONArray msgs) { + if (msgs == null) { + return null; + } + + List receivedMsgList = new ArrayList<>(); + for (int i = 0, len = msgs.size(); i < len; i++) { + JSONObject info = msgs.getJSONObject(i); + receivedMsgList.add(ReceivedMsg.valueOf(info)); + } + return receivedMsgList; + } } diff --git a/src/main/java/com/hotlcc/wechat4j/model/RecommendInfo.java b/src/main/java/com/hotlcc/wechat4j/model/RecommendInfo.java index dfc7884..862f351 100644 --- a/src/main/java/com/hotlcc/wechat4j/model/RecommendInfo.java +++ b/src/main/java/com/hotlcc/wechat4j/model/RecommendInfo.java @@ -1,26 +1,13 @@ package com.hotlcc.wechat4j.model; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import java.util.ArrayList; +import java.util.List; + public final class RecommendInfo { - public RecommendInfo(JSONObject info) { - if (info == null) { - return; - } - this.Ticket = info.getString("Ticket"); - this.UserName = info.getString("UserName"); - this.Sex = info.getInteger("Sex"); - this.AttrStatus = info.getInteger("AttrStatus"); - this.City = info.getString("City"); - this.NickName = info.getString("NickName"); - this.Scene = info.getInteger("Scene"); - this.Province = info.getString("Province"); - this.Content = info.getString("Content"); - this.Alias = info.getString("Alias"); - this.Signature = info.getString("Signature"); - this.OpCode = info.getInteger("OpCode"); - this.QQNum = info.getLong("QQNum"); - this.VerifyFlag = info.getInteger("VerifyFlag"); + private RecommendInfo() { } private String Ticket; @@ -93,4 +80,42 @@ public final class RecommendInfo { public Integer getVerifyFlag() { return VerifyFlag; } + + public static RecommendInfo valueOf(JSONObject info) { + if (info == null) { + return null; + } + + RecommendInfo recommendInfo = new RecommendInfo(); + + recommendInfo.Ticket = info.getString("Ticket"); + recommendInfo.UserName = info.getString("UserName"); + recommendInfo.Sex = info.getInteger("Sex"); + recommendInfo.AttrStatus = info.getInteger("AttrStatus"); + recommendInfo.City = info.getString("City"); + recommendInfo.NickName = info.getString("NickName"); + recommendInfo.Scene = info.getInteger("Scene"); + recommendInfo.Province = info.getString("Province"); + recommendInfo.Content = info.getString("Content"); + recommendInfo.Alias = info.getString("Alias"); + recommendInfo.Signature = info.getString("Signature"); + recommendInfo.OpCode = info.getInteger("OpCode"); + recommendInfo.QQNum = info.getLong("QQNum"); + recommendInfo.VerifyFlag = info.getInteger("VerifyFlag"); + + return recommendInfo; + } + + public static List valueOf(JSONArray infos) { + if (infos == null) { + return null; + } + + List recommendInfos = new ArrayList<>(); + for (int i = 0, len = infos.size(); i < len; i++) { + JSONObject info = infos.getJSONObject(i); + recommendInfos.add(RecommendInfo.valueOf(info)); + } + return recommendInfos; + } } diff --git a/src/main/java/com/hotlcc/wechat4j/model/UserInfo.java b/src/main/java/com/hotlcc/wechat4j/model/UserInfo.java new file mode 100644 index 0000000..19d5957 --- /dev/null +++ b/src/main/java/com/hotlcc/wechat4j/model/UserInfo.java @@ -0,0 +1,226 @@ +package com.hotlcc.wechat4j.model; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +/** + * 微信用户信息 + */ +public final class UserInfo { + private UserInfo() { + } + + private Long Uin; + private String NickName; + private String HeadImgUrl; + private Integer ContactFlag; + private Integer MemberCount; + private List MemberList; + private String RemarkName; + private Integer HideInputBarFlag; + private Integer Sex; + private String Signature; + private Integer VerifyFlag; + private Long OwnerUin; + private String PYInitial; + private String PYQuanPin; + private String RemarkPYInitial; + private String RemarkPYQuanPin; + private Integer StarFriend; + private Integer AppAccountFlag; + private Integer Statues; + private Integer AttrStatus; + private String Province; + private String City; + private String Alias; + private Integer SnsFlag; + private Integer UniFriend; + private String DisplayName; + private Long ChatRoomId; + private String KeyWord; + private String EncryChatRoomId; + private Integer IsOwner; + private String UserName; + + public Long getUin() { + return Uin; + } + + public String getNickName() { + return NickName; + } + + public String getHeadImgUrl() { + return HeadImgUrl; + } + + public Integer getContactFlag() { + return ContactFlag; + } + + public Integer getMemberCount() { + return MemberCount; + } + + public List getMemberList() { + return MemberList; + } + + public String getRemarkName() { + return RemarkName; + } + + public Integer getHideInputBarFlag() { + return HideInputBarFlag; + } + + public Integer getSex() { + return Sex; + } + + public String getSignature() { + return Signature; + } + + public Integer getVerifyFlag() { + return VerifyFlag; + } + + public Long getOwnerUin() { + return OwnerUin; + } + + public String getPYInitial() { + return PYInitial; + } + + public String getPYQuanPin() { + return PYQuanPin; + } + + public String getRemarkPYInitial() { + return RemarkPYInitial; + } + + public String getRemarkPYQuanPin() { + return RemarkPYQuanPin; + } + + public Integer getStarFriend() { + return StarFriend; + } + + public Integer getAppAccountFlag() { + return AppAccountFlag; + } + + public Integer getStatues() { + return Statues; + } + + public Integer getAttrStatus() { + return AttrStatus; + } + + public String getProvince() { + return Province; + } + + public String getCity() { + return City; + } + + public String getAlias() { + return Alias; + } + + public Integer getSnsFlag() { + return SnsFlag; + } + + public Integer getUniFriend() { + return UniFriend; + } + + public String getDisplayName() { + return DisplayName; + } + + public Long getChatRoomId() { + return ChatRoomId; + } + + public String getKeyWord() { + return KeyWord; + } + + public String getEncryChatRoomId() { + return EncryChatRoomId; + } + + public Integer getIsOwner() { + return IsOwner; + } + + public String getUserName() { + return UserName; + } + + public static UserInfo valueOf(JSONObject info) { + if (info == null) { + return null; + } + + UserInfo userInfo = new UserInfo(); + + userInfo.Uin = info.getLong("Uin"); + userInfo.NickName = info.getString("NickName"); + userInfo.HeadImgUrl = info.getString("HeadImgUrl"); + userInfo.ContactFlag = info.getInteger("ContactFlag"); + userInfo.MemberCount = info.getInteger("MemberCount"); + userInfo.MemberList = valueOf(info.getJSONArray("MemberList")); + userInfo.RemarkName = info.getString("RemarkName"); + userInfo.HideInputBarFlag = info.getInteger("HideInputBarFlag"); + userInfo.Sex = info.getInteger("Sex"); + userInfo.Signature = info.getString("Signature"); + userInfo.VerifyFlag = info.getInteger("VerifyFlag"); + userInfo.OwnerUin = info.getLong("OwnerUin"); + userInfo.PYInitial = info.getString("PYInitial"); + userInfo.PYQuanPin = info.getString("PYQuanPin"); + userInfo.RemarkPYInitial = info.getString("RemarkPYInitial"); + userInfo.RemarkPYQuanPin = info.getString("RemarkPYQuanPin"); + userInfo.StarFriend = info.getInteger("StarFriend"); + userInfo.AppAccountFlag = info.getInteger("AppAccountFlag"); + userInfo.Statues = info.getInteger("Statues"); + userInfo.AttrStatus = info.getInteger("AttrStatus"); + userInfo.Province = info.getString("Province"); + userInfo.City = info.getString("City"); + userInfo.Alias = info.getString("Alias"); + userInfo.SnsFlag = info.getInteger("SnsFlag"); + userInfo.UniFriend = info.getInteger("UniFriend"); + userInfo.DisplayName = info.getString("DisplayName"); + userInfo.ChatRoomId = info.getLong("ChatRoomId"); + userInfo.KeyWord = info.getString("KeyWord"); + userInfo.EncryChatRoomId = info.getString("EncryChatRoomId"); + userInfo.IsOwner = info.getInteger("IsOwner"); + userInfo.UserName = info.getString("UserName"); + + return userInfo; + } + + public static List valueOf(JSONArray infos) { + if (infos == null) { + return null; + } + + List userList = new ArrayList<>(); + for (int i = 0, len = infos.size(); i < len; i++) { + JSONObject info = infos.getJSONObject(i); + userList.add(UserInfo.valueOf(info)); + } + return userList; + } +} diff --git a/src/test/java/TestClass2.java b/src/test/java/TestClass2.java index 8efb235..8d9b116 100644 --- a/src/test/java/TestClass2.java +++ b/src/test/java/TestClass2.java @@ -1,5 +1,8 @@ import com.hotlcc.wechat4j.Wechat; import com.hotlcc.wechat4j.api.WebWeixinApi; +import com.hotlcc.wechat4j.handler.ReceivedMsgHandler; +import com.hotlcc.wechat4j.model.ReceivedMsg; +import com.hotlcc.wechat4j.model.UserInfo; import com.hotlcc.wechat4j.util.CommonUtil; public class TestClass2 { @@ -7,6 +10,15 @@ public class TestClass2 { WebWeixinApi api = new WebWeixinApi(); Wechat wechat = new Wechat(); wechat.setWebWeixinApi(api); + wechat.addReceivedMsgHandler(new ReceivedMsgHandler() { + @Override + public void handleAllType(Wechat wechat, ReceivedMsg msg) { + System.out.println("===收到消息:" + msg.getContent()); + UserInfo contact = wechat.getContactByUserName(false, msg.getFromUserName()); + if ("李国栋".equals(contact.getRemarkName())) { + } + } + }); wechat.autoLogin(); CommonUtil.threadSleep(1000 * 60 * 10); wechat.logout();