2
0
mirror of https://gitee.com/hotlcc/wechat4j.git synced 2025-07-19 15:19:35 +08:00

提交代码

This commit is contained in:
hotlcc 2018-07-25 12:41:25 +08:00
parent 83e9411bcd
commit acc3fc9966
6 changed files with 399 additions and 91 deletions

View File

@ -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<UserInfo> 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<UserInfo> 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<UserInfo> 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<UserInfo> 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<UserInfo> 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;
}
}

View File

@ -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<AppInfo> valueOf(JSONArray infos) {
if (infos == null) {
return null;
}
List<AppInfo> 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;
}
}

View File

@ -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<ReceivedMsg> valueOf(JSONArray msgs) {
if (msgs == null) {
return null;
}
List<ReceivedMsg> 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;
}
}

View File

@ -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<RecommendInfo> valueOf(JSONArray infos) {
if (infos == null) {
return null;
}
List<RecommendInfo> 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;
}
}

View File

@ -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<UserInfo> 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<UserInfo> 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<UserInfo> valueOf(JSONArray infos) {
if (infos == null) {
return null;
}
List<UserInfo> 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;
}
}

View File

@ -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();