From 62810d099339c38a64497ebdd12efcddca3b327a Mon Sep 17 00:00:00 2001 From: hotlcc Date: Wed, 25 Jul 2018 22:56:56 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/hotlcc/wechat4j/Wechat.java | 20 +++++++++++-------- src/test/java/TestClass2.java | 5 +++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/hotlcc/wechat4j/Wechat.java b/src/main/java/com/hotlcc/wechat4j/Wechat.java index 4f2ba46..c37f2c4 100644 --- a/src/main/java/com/hotlcc/wechat4j/Wechat.java +++ b/src/main/java/com/hotlcc/wechat4j/Wechat.java @@ -569,17 +569,21 @@ public class Wechat { * 退出登录 */ public void logout(boolean clearAllLoginInfo) { - try { - isOnlineLock.lock(); + if (isOnline) { + try { + isOnlineLock.lock(); - webWeixinApi.logout(httpClient, urlVersion, new BaseRequest(wxsid, skey, wxuin)); - isOnline = false; + if (isOnline) { + webWeixinApi.logout(httpClient, urlVersion, new BaseRequest(wxsid, skey, wxuin)); + isOnline = false; - if (clearAllLoginInfo) { - clearAllLoginInfo(); + if (clearAllLoginInfo) { + clearAllLoginInfo(); + } + } + } finally { + isOnlineLock.unlock(); } - } finally { - isOnlineLock.unlock(); } } diff --git a/src/test/java/TestClass2.java b/src/test/java/TestClass2.java index b6c6a4d..dbae62a 100644 --- a/src/test/java/TestClass2.java +++ b/src/test/java/TestClass2.java @@ -4,6 +4,7 @@ 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.sun.scenario.effect.GaussianShadow; public class TestClass2 { public static void main(String[] args) { @@ -22,5 +23,9 @@ public class TestClass2 { } }); wechat.autoLogin(); + UserInfo userInfo = wechat.getContactByNickName(false, "Allen"); + System.out.println(JSONObject.toJSONString(userInfo)); + wechat.sendText("aaa", userInfo.getUserName()); + wechat.logout(); } } From d71d34b01b8f72e7182ff6cbfd3a822aae0a847b Mon Sep 17 00:00:00 2001 From: hotlcc Date: Wed, 15 Aug 2018 15:42:38 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/hotlcc/wechat4j/Wechat.java | 202 +++++++++--------- 1 file changed, 99 insertions(+), 103 deletions(-) diff --git a/src/main/java/com/hotlcc/wechat4j/Wechat.java b/src/main/java/com/hotlcc/wechat4j/Wechat.java index c37f2c4..4e79a30 100644 --- a/src/main/java/com/hotlcc/wechat4j/Wechat.java +++ b/src/main/java/com/hotlcc/wechat4j/Wechat.java @@ -28,8 +28,7 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -183,24 +182,24 @@ public class Wechat { /** * 获取uuid(登录时) * - * @param ps + * @param pw * @param time * @return */ - private String getWxUuid(PrintStream ps, int time) { - ps.print("尝试正常方式获取uuid..."); - ps.flush(); + private String getWxUuid(PrintWriter pw, int time) { + pw.print("尝试正常方式获取uuid..."); + pw.flush(); for (int i = 0; i <= time; i++) { if (i > 0) { - ps.print("\t第" + i + "次尝试..."); - ps.flush(); + pw.print("\t第" + i + "次尝试..."); + pw.flush(); } JSONObject result = webWeixinApi.getWxUuid(httpClient); if (result == null) { - ps.println("\t失败:出现异常"); - ps.flush(); + pw.println("\t失败:出现异常"); + pw.flush(); return null; } @@ -208,23 +207,23 @@ public class Wechat { String uuid = result.getString("uuid"); if (!"200".equals(code)) { String msg = result.getString("msg"); - ps.println("\t失败:" + msg); - ps.flush(); + pw.println("\t失败:" + msg); + pw.flush(); return null; } if (StringUtil.isEmpty(uuid)) { - ps.print("\t失败"); + pw.print("\t失败"); if (i == 0 && time > 0) { - ps.print(",将重复尝试" + time + "次"); + pw.print(",将重复尝试" + time + "次"); } - ps.println(); - ps.flush(); + pw.println(); + pw.flush(); continue; } - ps.println("\t成功,值为:" + uuid); - ps.flush(); + pw.println("\t成功,值为:" + uuid); + pw.flush(); return uuid; } return null; @@ -235,35 +234,35 @@ public class Wechat { * * @return */ - private boolean getAndShowQRCode(PrintStream ps, String uuid, int time) { - ps.print("获取二维码..."); - ps.flush(); + private boolean getAndShowQRCode(PrintWriter pw, String uuid, int time) { + pw.print("获取二维码..."); + pw.flush(); for (int i = 0; i <= time; i++) { if (i > 0) { - ps.print("\t第" + i + "次尝试..."); - ps.flush(); + pw.print("\t第" + i + "次尝试..."); + pw.flush(); } byte[] data = webWeixinApi.getQR(httpClient, uuid); if (data == null || data.length <= 0) { - ps.print("\t失败"); + pw.print("\t失败"); if (i == 0 && time > 0) { - ps.print(",将重新获取uuid并重复尝试" + time + "次"); + pw.print(",将重新获取uuid并重复尝试" + time + "次"); } - ps.println(); - ps.flush(); - getWxUuid(ps, 0); + pw.println(); + pw.flush(); + getWxUuid(pw, 0); CommonUtil.threadSleep(2000); continue; } - ps.println("\t成功,请扫描二维码:"); - ps.flush(); - ps.println(QRCodeUtil.toCharMatrix(data)); - ps.flush(); + pw.println("\t成功,请扫描二维码:"); + pw.flush(); + pw.println(QRCodeUtil.toCharMatrix(data)); + pw.flush(); QRCodeUtil.openQRCodeImage(data); return true; } @@ -276,39 +275,39 @@ public class Wechat { * * @return */ - private JSONObject waitForConfirm(PrintStream ps, String uuid) { - ps.print("等待手机端扫码..."); - ps.flush(); + private JSONObject waitForConfirm(PrintWriter pw, String uuid) { + pw.print("等待手机端扫码..."); + pw.flush(); boolean flag = false; while (true) { JSONObject result = webWeixinApi.getRedirectUri(httpClient, LoginTipEnum.TIP_0, uuid); if (result == null) { - ps.println("\t失败:出现异常"); - ps.flush(); + pw.println("\t失败:出现异常"); + pw.flush(); return null; } String code = result.getString("code"); if ("408".equals(code)) { - ps.print("."); - ps.flush(); + pw.print("."); + pw.flush(); continue; } else if ("400".equals(code)) { - ps.println("\t失败,二维码失效"); - ps.flush(); + pw.println("\t失败,二维码失效"); + pw.flush(); return null; } else if ("201".equals(code)) { if (!flag) { - ps.println(); - ps.print("请确认登录..."); - ps.flush(); + pw.println(); + pw.print("请确认登录..."); + pw.flush(); flag = true; } continue; } else if ("200".equals(code)) { - ps.println("\t成功,认证完成"); - ps.flush(); + pw.println("\t成功,认证完成"); + pw.flush(); return result; } else { return null; @@ -319,21 +318,21 @@ public class Wechat { /** * 获取登录认证码(登录时) */ - private boolean getLoginCode(PrintStream ps, String redirectUri) { - ps.print("获取登录认证码..."); - ps.flush(); + private boolean getLoginCode(PrintWriter pw, String redirectUri) { + pw.print("获取登录认证码..."); + pw.flush(); JSONObject result = webWeixinApi.getLoginCode(httpClient, redirectUri); if (result == null) { - ps.println("\t失败:出现异常"); - ps.flush(); + pw.println("\t失败:出现异常"); + pw.flush(); return false; } String ret = result.getString("ret"); if (!"0".equals(ret)) { - ps.println("\t失败:错误的返回码(" + ret + ")"); - ps.flush(); + pw.println("\t失败:错误的返回码(" + ret + ")"); + pw.flush(); return false; } @@ -342,8 +341,8 @@ public class Wechat { skey = result.getString("skey"); wxuin = result.getString("wxuin"); - ps.println("\t成功"); - ps.flush(); + pw.println("\t成功"); + pw.flush(); return true; } @@ -351,37 +350,37 @@ public class Wechat { /** * push方式获取uuid(登录时) * - * @param ps + * @param pw * @param wxuin * @return */ - private String getWxUuid(PrintStream ps, String wxuin) { - ps.print("尝试push方式获取uuid..."); - ps.flush(); + private String getWxUuid(PrintWriter pw, String wxuin) { + pw.print("尝试push方式获取uuid..."); + pw.flush(); JSONObject result = webWeixinApi.pushLogin(httpClient, urlVersion, wxuin); if (result == null) { - ps.println("\t失败:出现异常"); - ps.flush(); + pw.println("\t失败:出现异常"); + pw.flush(); return null; } String ret = result.getString("ret"); if (!"0".equals(ret)) { - ps.println("\t失败:错误的返回码(" + ret + ")"); - ps.flush(); + pw.println("\t失败:错误的返回码(" + ret + ")"); + pw.flush(); return null; } String uuid = result.getString("uuid"); if (StringUtil.isEmpty(uuid)) { - ps.println("\t失败:空值"); - ps.flush(); + pw.println("\t失败:空值"); + pw.flush(); return null; } - ps.println("\t成功,值为:" + uuid); - ps.flush(); + pw.println("\t成功,值为:" + uuid); + pw.flush(); return uuid; } @@ -418,30 +417,30 @@ public class Wechat { * * @return */ - private boolean wxInitWithRetry(PrintStream ps, int time) { - ps.print("正在初始化数据..."); - ps.flush(); + private boolean wxInitWithRetry(PrintWriter pw, int time) { + pw.print("正在初始化数据..."); + pw.flush(); for (int i = 0; i <= time; i++) { if (i > 0) { - ps.print("\t第" + i + "次尝试..."); - ps.flush(); + pw.print("\t第" + i + "次尝试..."); + pw.flush(); } if (!wxInit()) { - ps.print("\t失败"); + pw.print("\t失败"); if (i == 0 && time > 0) { - ps.print(",将重复尝试" + time + "次"); + pw.print(",将重复尝试" + time + "次"); } - ps.println(); - ps.flush(); + pw.println(); + pw.flush(); CommonUtil.threadSleep(2000); continue; } - ps.println("\t成功"); - ps.flush(); + pw.println("\t成功"); + pw.flush(); return true; } @@ -480,18 +479,15 @@ public class Wechat { /** * 自动登录 */ - public boolean autoLogin(OutputStream os, boolean tryPushLogin) { + public boolean autoLogin(PrintWriter pw, boolean tryPushLogin) { // 0、获取消息打印流 - PrintStream ps = null; - if (os != null) { - ps = new PrintStream(os); - } else { - ps = System.out; + if (pw == null) { + pw = new PrintWriter(System.out); } // 1、判断是否已经登录 if (isOnline) { - ps.println("当前已是登录状态,无需登录"); + pw.println("当前已是登录状态,无需登录"); return true; } @@ -502,45 +498,45 @@ public class Wechat { // 2.1、获取uuid String uuid = null; if (tryPushLogin && StringUtil.isNotEmpty(wxuin)) { - uuid = getWxUuid(ps, wxuin); + uuid = getWxUuid(pw, wxuin); } if (StringUtil.isEmpty(uuid)) { - uuid = getWxUuid(ps, time); + uuid = getWxUuid(pw, time); } if (StringUtil.isEmpty(uuid)) { - ps.println("无法获取uuid,登录不成功"); - ps.flush(); + pw.println("无法获取uuid,登录不成功"); + pw.flush(); return false; } // 2.2、获取并显示二维码 - if (!getAndShowQRCode(ps, uuid, time)) { - ps.println("无法获取二维码,登录不成功"); - ps.flush(); + if (!getAndShowQRCode(pw, uuid, time)) { + pw.println("无法获取二维码,登录不成功"); + pw.flush(); return false; } // 2.3、等待确认 - result = waitForConfirm(ps, uuid); + result = waitForConfirm(pw, uuid); if (result == null) { - ps.println("手机端认证失败,登录不成功"); - ps.flush(); + pw.println("手机端认证失败,登录不成功"); + pw.flush(); return false; } urlVersion = result.getString("urlVersion"); // 2.4、获取登录认证码 - if (!getLoginCode(ps, result.getString("redirectUri"))) { - ps.println("无法获取登录认证码,登录不成功"); - ps.flush(); + if (!getLoginCode(pw, result.getString("redirectUri"))) { + pw.println("无法获取登录认证码,登录不成功"); + pw.flush(); return false; } // 3、初始化数据 - if (!wxInitWithRetry(ps, time)) { - ps.println("初始化数据失败,请重新登录"); - ps.flush(); + if (!wxInitWithRetry(pw, time)) { + pw.println("初始化数据失败,请重新登录"); + pw.flush(); return false; } - ps.println("微信登录成功,欢迎你:" + getLoginUserNickName(false)); - ps.flush(); + pw.println("微信登录成功,欢迎你:" + getLoginUserNickName(false)); + pw.flush(); try { isOnlineLock.lock(); From 4508c70682971188698a9e0a8e56db3c5c066a18 Mon Sep 17 00:00:00 2001 From: hotlcc Date: Wed, 15 Aug 2018 16:06:20 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/hotlcc/wechat4j/api/WebWeixinApi.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/hotlcc/wechat4j/api/WebWeixinApi.java b/src/main/java/com/hotlcc/wechat4j/api/WebWeixinApi.java index 52dccee..fe280cb 100644 --- a/src/main/java/com/hotlcc/wechat4j/api/WebWeixinApi.java +++ b/src/main/java/com/hotlcc/wechat4j/api/WebWeixinApi.java @@ -81,7 +81,7 @@ public class WebWeixinApi { JSONObject result = new JSONObject(); result.put("code", code); if (!"200".equals(code)) { - result.put("msg", "返回code错误,请确认appid是否有效"); + result.put("msg", "错误代码(" + code + "),请确认appid是否有效"); return result; } From 3e0e974a45ef21af042f4386efef0569c638cee6 Mon Sep 17 00:00:00 2001 From: hotlcc Date: Wed, 15 Aug 2018 16:18:34 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/hotlcc/wechat4j/util/PropertiesUtil.java | 4 ++-- .../resources/{config => META-INF/wechat4j}/app.properties | 0 .../{config => META-INF/wechat4j}/webwx-url.properties | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename src/main/resources/{config => META-INF/wechat4j}/app.properties (100%) rename src/main/resources/{config => META-INF/wechat4j}/webwx-url.properties (100%) diff --git a/src/main/java/com/hotlcc/wechat4j/util/PropertiesUtil.java b/src/main/java/com/hotlcc/wechat4j/util/PropertiesUtil.java index c1aaa4c..b0a2830 100644 --- a/src/main/java/com/hotlcc/wechat4j/util/PropertiesUtil.java +++ b/src/main/java/com/hotlcc/wechat4j/util/PropertiesUtil.java @@ -17,8 +17,8 @@ public final class PropertiesUtil { static { loadProperties(new String[]{ - "config/app.properties", - "config/webwx-url.properties" + "META-INF/wechat4j/app.properties", + "META-INF/wechat4j/webwx-url.properties" }); } diff --git a/src/main/resources/config/app.properties b/src/main/resources/META-INF/wechat4j/app.properties similarity index 100% rename from src/main/resources/config/app.properties rename to src/main/resources/META-INF/wechat4j/app.properties diff --git a/src/main/resources/config/webwx-url.properties b/src/main/resources/META-INF/wechat4j/webwx-url.properties similarity index 100% rename from src/main/resources/config/webwx-url.properties rename to src/main/resources/META-INF/wechat4j/webwx-url.properties From 8d4c2cace05c7e9bcdac59109259171c7372cb2b Mon Sep 17 00:00:00 2001 From: hotlcc Date: Wed, 15 Aug 2018 16:25:55 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81:?= =?UTF-8?q?=E9=87=87=E7=94=A8lombok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 +- .../com/hotlcc/wechat4j/model/AppInfo.java | 10 +- .../hotlcc/wechat4j/model/BaseRequest.java | 36 +---- .../hotlcc/wechat4j/model/ReceivedMsg.java | 114 +--------------- .../hotlcc/wechat4j/model/RecommendInfo.java | 58 +------- .../com/hotlcc/wechat4j/model/UserInfo.java | 126 +----------------- .../com/hotlcc/wechat4j/model/WxMessage.java | 52 +------- 7 files changed, 22 insertions(+), 381 deletions(-) diff --git a/pom.xml b/pom.xml index 0f67578..d6bd0fc 100644 --- a/pom.xml +++ b/pom.xml @@ -79,7 +79,12 @@ javase 3.3.3 - + + + org.projectlombok + lombok + 1.16.20 + diff --git a/src/main/java/com/hotlcc/wechat4j/model/AppInfo.java b/src/main/java/com/hotlcc/wechat4j/model/AppInfo.java index 4a1649b..780f828 100644 --- a/src/main/java/com/hotlcc/wechat4j/model/AppInfo.java +++ b/src/main/java/com/hotlcc/wechat4j/model/AppInfo.java @@ -3,10 +3,12 @@ package com.hotlcc.wechat4j.model; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; +import lombok.Getter; import java.util.ArrayList; import java.util.List; +@Getter public final class AppInfo { private AppInfo() { } @@ -16,14 +18,6 @@ public final class AppInfo { @JSONField(name = "AppID") private String appID; - public Integer getType() { - return type; - } - - public String getAppID() { - return appID; - } - public static AppInfo valueOf(JSONObject info) { if (info == null) { return null; diff --git a/src/main/java/com/hotlcc/wechat4j/model/BaseRequest.java b/src/main/java/com/hotlcc/wechat4j/model/BaseRequest.java index 9a609b0..9f77c74 100644 --- a/src/main/java/com/hotlcc/wechat4j/model/BaseRequest.java +++ b/src/main/java/com/hotlcc/wechat4j/model/BaseRequest.java @@ -2,10 +2,14 @@ package com.hotlcc.wechat4j.model; import com.alibaba.fastjson.annotation.JSONField; import com.hotlcc.wechat4j.util.WechatUtil; +import lombok.Getter; +import lombok.Setter; /** * 基本请求模型 */ +@Getter +@Setter public class BaseRequest { public BaseRequest() { } @@ -29,36 +33,4 @@ public class BaseRequest { private String skey; @JSONField(name = "Uin") private String uin; - - public String getDeviceID() { - return deviceID; - } - - public void setDeviceID(String deviceID) { - deviceID = deviceID; - } - - public String getSid() { - return sid; - } - - public void setSid(String sid) { - sid = sid; - } - - public String getSkey() { - return skey; - } - - public void setSkey(String skey) { - skey = skey; - } - - public String getUin() { - return uin; - } - - public void setUin(String uin) { - uin = uin; - } } diff --git a/src/main/java/com/hotlcc/wechat4j/model/ReceivedMsg.java b/src/main/java/com/hotlcc/wechat4j/model/ReceivedMsg.java index 3a83100..d5d56be 100644 --- a/src/main/java/com/hotlcc/wechat4j/model/ReceivedMsg.java +++ b/src/main/java/com/hotlcc/wechat4j/model/ReceivedMsg.java @@ -3,10 +3,12 @@ package com.hotlcc.wechat4j.model; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; +import lombok.Getter; import java.util.ArrayList; import java.util.List; +@Getter public final class ReceivedMsg { private ReceivedMsg() { } @@ -68,118 +70,6 @@ public final class ReceivedMsg { @JSONField(name = "FileSize") private String fileSize; - public Integer getSubMsgType() { - return subMsgType; - } - - public Long getVoiceLength() { - return voiceLength; - } - - public String getFileName() { - return fileName; - } - - public Long getImgHeight() { - return imgHeight; - } - - public String getToUserName() { - return toUserName; - } - - public Long getHasProductId() { - return hasProductId; - } - - public Integer getImgStatus() { - return imgStatus; - } - - public String getUrl() { - return url; - } - - public Integer getImgWidth() { - return imgWidth; - } - - public Integer getForwardFlag() { - return forwardFlag; - } - - public Integer getStatus() { - return status; - } - - public String getTicket() { - return ticket; - } - - public com.hotlcc.wechat4j.model.RecommendInfo getRecommendInfo() { - return recommendInfo; - } - - public Long getCreateTime() { - return createTime; - } - - public Long getNewMsgId() { - return newMsgId; - } - - public Integer getMsgType() { - return msgType; - } - - public String getEncryFileName() { - return encryFileName; - } - - public String getMsgId() { - return msgId; - } - - public Integer getStatusNotifyCode() { - return statusNotifyCode; - } - - public AppInfo getAppInfo() { - return appInfo; - } - - public Integer getAppMsgType() { - return appMsgType; - } - - public Long getPlayLength() { - return playLength; - } - - public String getMediaId() { - return mediaId; - } - - public String getContent() { - return content; - } - - public String getStatusNotifyUserName() { - return statusNotifyUserName; - } - - public String getFromUserName() { - return fromUserName; - } - - public String getOriContent() { - return oriContent; - } - - public String getFileSize() { - return fileSize; - } - public static ReceivedMsg valueOf(JSONObject msg) { if (msg == null) { return null; diff --git a/src/main/java/com/hotlcc/wechat4j/model/RecommendInfo.java b/src/main/java/com/hotlcc/wechat4j/model/RecommendInfo.java index 1cf5296..bbc177d 100644 --- a/src/main/java/com/hotlcc/wechat4j/model/RecommendInfo.java +++ b/src/main/java/com/hotlcc/wechat4j/model/RecommendInfo.java @@ -3,10 +3,12 @@ package com.hotlcc.wechat4j.model; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; +import lombok.Getter; import java.util.ArrayList; import java.util.List; +@Getter public final class RecommendInfo { private RecommendInfo() { } @@ -40,62 +42,6 @@ public final class RecommendInfo { @JSONField(name = "VerifyFlag") private Integer verifyFlag; - public String getTicket() { - return ticket; - } - - public String getUserName() { - return userName; - } - - public Integer getSex() { - return sex; - } - - public Integer getAttrStatus() { - return attrStatus; - } - - public String getCity() { - return city; - } - - public String getNickName() { - return nickName; - } - - public Integer getScene() { - return scene; - } - - public String getProvince() { - return province; - } - - public String getContent() { - return content; - } - - public String getAlias() { - return alias; - } - - public String getSignature() { - return signature; - } - - public Integer getOpCode() { - return opCode; - } - - public Long getQQNum() { - return qqNum; - } - - public Integer getVerifyFlag() { - return verifyFlag; - } - public static RecommendInfo valueOf(JSONObject info) { if (info == null) { return null; diff --git a/src/main/java/com/hotlcc/wechat4j/model/UserInfo.java b/src/main/java/com/hotlcc/wechat4j/model/UserInfo.java index 8f716a5..6eba56a 100644 --- a/src/main/java/com/hotlcc/wechat4j/model/UserInfo.java +++ b/src/main/java/com/hotlcc/wechat4j/model/UserInfo.java @@ -3,6 +3,7 @@ package com.hotlcc.wechat4j.model; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; +import lombok.Getter; import java.util.ArrayList; import java.util.List; @@ -10,6 +11,7 @@ import java.util.List; /** * 微信用户信息 */ +@Getter public final class UserInfo { private UserInfo() { } @@ -77,130 +79,6 @@ public final class UserInfo { @JSONField(name = "UserName") 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; diff --git a/src/main/java/com/hotlcc/wechat4j/model/WxMessage.java b/src/main/java/com/hotlcc/wechat4j/model/WxMessage.java index 8f275a6..05db641 100644 --- a/src/main/java/com/hotlcc/wechat4j/model/WxMessage.java +++ b/src/main/java/com/hotlcc/wechat4j/model/WxMessage.java @@ -1,10 +1,14 @@ package com.hotlcc.wechat4j.model; import com.alibaba.fastjson.annotation.JSONField; +import lombok.Getter; +import lombok.Setter; /** * 要发送的消息 */ +@Getter +@Setter public class WxMessage { @JSONField(name = "ClientMsgId") private String clientMsgId; @@ -18,52 +22,4 @@ public class WxMessage { private String toUserName; @JSONField(name = "Type") private Integer type; - - public String getClientMsgId() { - return clientMsgId; - } - - public void setClientMsgId(String clientMsgId) { - this.clientMsgId = clientMsgId; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public String getFromUserName() { - return fromUserName; - } - - public void setFromUserName(String fromUserName) { - this.fromUserName = fromUserName; - } - - public String getLocalID() { - return localID; - } - - public void setLocalID(String localID) { - this.localID = localID; - } - - public String getToUserName() { - return toUserName; - } - - public void setToUserName(String toUserName) { - this.toUserName = toUserName; - } - - public Integer getType() { - return type; - } - - public void setType(Integer type) { - this.type = type; - } } From c5010d4a22c36c0129c507727a92c78364596c5f Mon Sep 17 00:00:00 2001 From: hotlcc Date: Fri, 17 Aug 2018 15:39:21 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/hotlcc/wechat4j/Wechat.java | 4 +++- src/main/java/com/hotlcc/wechat4j/enums/SelectorEnum.java | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/hotlcc/wechat4j/Wechat.java b/src/main/java/com/hotlcc/wechat4j/Wechat.java index 4e79a30..cb69ac5 100644 --- a/src/main/java/com/hotlcc/wechat4j/Wechat.java +++ b/src/main/java/com/hotlcc/wechat4j/Wechat.java @@ -743,7 +743,7 @@ public class Wechat { try { SelectorEnum e = SelectorEnum.valueOf(selector); if (e == null) { - logger.warn("Cannot process selector for error selector {}", selector); + logger.warn("Cannot process unknow selector {}", selector); return; } @@ -755,6 +755,8 @@ public class Wechat { break; case SELECTOR_4: break; + case SELECTOR_6: + break; case SELECTOR_7: break; default: diff --git a/src/main/java/com/hotlcc/wechat4j/enums/SelectorEnum.java b/src/main/java/com/hotlcc/wechat4j/enums/SelectorEnum.java index d994def..fe75108 100644 --- a/src/main/java/com/hotlcc/wechat4j/enums/SelectorEnum.java +++ b/src/main/java/com/hotlcc/wechat4j/enums/SelectorEnum.java @@ -4,6 +4,7 @@ public enum SelectorEnum { SELECTOR_0(0, "正常"), SELECTOR_2(2, "有新消息"), SELECTOR_4(4, "目前发现修改了联系人备注会出现"), + SELECTOR_6(6, "目前不知道代表什么"), SELECTOR_7(7, "手机操作了微信"); private int code; From 002bdf3d9caf597f383afded33b3e51e47a5c75e Mon Sep 17 00:00:00 2001 From: hotlcc Date: Fri, 17 Aug 2018 15:55:00 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/hotlcc/wechat4j/Wechat.java | 50 +++++++------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/hotlcc/wechat4j/Wechat.java b/src/main/java/com/hotlcc/wechat4j/Wechat.java index cb69ac5..0d2f62e 100644 --- a/src/main/java/com/hotlcc/wechat4j/Wechat.java +++ b/src/main/java/com/hotlcc/wechat4j/Wechat.java @@ -815,36 +815,24 @@ public class Wechat { */ private void processNewMsg(JSONArray AddMsgList) { try { - if (AddMsgList != null && !AddMsgList.isEmpty()) { - int len = AddMsgList.size(); - logger.debug("收到{}条新消息", len); - for (int i = 0; i < len; i++) { - JSONObject AddMsg = AddMsgList.getJSONObject(i); - processNewMsg(AddMsg); - } - } - } catch (Exception e) { - logger.error("Execute processNewMsg error.", e); - } - } - - private void processNewMsg(JSONObject AddMsg) { - try { - ReceivedMsg msg = ReceivedMsg.valueOf(AddMsg); - processNewMsg(msg); - } catch (Exception e) { - logger.error("Execute processNewMsg error.", e); - } - } - - private void processNewMsg(ReceivedMsg msg) { - try { - if (receivedMsgHandlers == null) { + if (AddMsgList == null || AddMsgList.isEmpty()) { return; } - for (ReceivedMsgHandler handler : receivedMsgHandlers) { - if (handler != null) { - processNewMsg(msg, handler); + + int len = AddMsgList.size(); + logger.debug("收到{}条新消息", len); + + if (receivedMsgHandlers == null || receivedMsgHandlers.isEmpty()) { + logger.warn("收到{}条新消息,但没有配置消息处理器", len); + return; + } + + List receivedMsgList = ReceivedMsg.valueOf(AddMsgList); + for (ReceivedMsg receivedMsg : receivedMsgList) { + for (ReceivedMsgHandler handler : receivedMsgHandlers) { + if (handler != null) { + processNewMsg(receivedMsg, handler); + } } } } catch (Exception e) { @@ -854,11 +842,9 @@ public class Wechat { private void processNewMsg(ReceivedMsg msg, ReceivedMsgHandler handler) { try { - if (handler != null) { - handler.handleAllType(wechat, msg); - } + handler.handleAllType(wechat, msg); } catch (Exception e) { - + logger.error("Execute processNewMsg error.", e); } } } From 69109564d151e4a06fb1e68504c77a4b4b5e144c Mon Sep 17 00:00:00 2001 From: hotlcc Date: Fri, 17 Aug 2018 16:05:22 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/hotlcc/wechat4j/Wechat.java | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/hotlcc/wechat4j/Wechat.java b/src/main/java/com/hotlcc/wechat4j/Wechat.java index 0d2f62e..91859c0 100644 --- a/src/main/java/com/hotlcc/wechat4j/Wechat.java +++ b/src/main/java/com/hotlcc/wechat4j/Wechat.java @@ -885,6 +885,10 @@ public class Wechat { return loginUser; } + public UserInfo getLoginUser() { + return getLoginUser(false); + } + /** * 获取登录用户名 * @@ -898,6 +902,10 @@ public class Wechat { return loginUser.getUserName(); } + public String getLoginUserName() { + return getLoginUserName(false); + } + /** * 获取登录用户的昵称 * @@ -911,6 +919,10 @@ public class Wechat { return loginUser.getNickName(); } + public String getLoginUserNickName() { + return getLoginUserNickName(false); + } + /** * 获取SyncKey * @@ -999,15 +1011,19 @@ public class Wechat { return contactList; } + public List getContactList() { + return getContactList(false); + } + /** * 根据UserName获取联系人信息 * * @param update - * @param UserName + * @param userName * @return */ - public UserInfo getContactByUserName(boolean update, String UserName) { - if (StringUtil.isEmpty(UserName)) { + public UserInfo getContactByUserName(boolean update, String userName) { + if (StringUtil.isEmpty(userName)) { return null; } @@ -1021,7 +1037,7 @@ public class Wechat { continue; } - if (UserName.equals(userInfo.getUserName())) { + if (userName.equals(userInfo.getUserName())) { return userInfo; } } @@ -1029,15 +1045,19 @@ public class Wechat { return null; } + public UserInfo getContactByUserName(String userName) { + return getContactByUserName(false, userName); + } + /** * 根据NickName获取联系人信息 * * @param update - * @param NickName + * @param nickName * @return */ - public UserInfo getContactByNickName(boolean update, String NickName) { - if (StringUtil.isEmpty(NickName)) { + public UserInfo getContactByNickName(boolean update, String nickName) { + if (StringUtil.isEmpty(nickName)) { return null; } @@ -1051,7 +1071,7 @@ public class Wechat { continue; } - if (NickName.equals(userInfo.getNickName())) { + if (nickName.equals(userInfo.getNickName())) { return userInfo; } } @@ -1059,15 +1079,19 @@ public class Wechat { return null; } + public UserInfo getContactByNickName(String nickName) { + return getContactByNickName(false, nickName); + } + /** * 根据RemarkName获取联系人信息 * * @param update - * @param RemarkName + * @param remarkName * @return */ - public UserInfo getContactByRemarkName(boolean update, String RemarkName) { - if (StringUtil.isEmpty(RemarkName)) { + public UserInfo getContactByRemarkName(boolean update, String remarkName) { + if (StringUtil.isEmpty(remarkName)) { return null; } @@ -1081,7 +1105,7 @@ public class Wechat { continue; } - if (RemarkName.equals(userInfo.getRemarkName())) { + if (remarkName.equals(userInfo.getRemarkName())) { return userInfo; } } @@ -1089,6 +1113,10 @@ public class Wechat { return null; } + public UserInfo getContactByRemarkName(String remarkName) { + return getContactByRemarkName(false, remarkName); + } + /** * 发送文本消息 * From 2eb38f9ff758a2eb7f2457c19a37f554df592869 Mon Sep 17 00:00:00 2001 From: hotlcc Date: Fri, 17 Aug 2018 16:10:45 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/hotlcc/wechat4j/Wechat.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/hotlcc/wechat4j/Wechat.java b/src/main/java/com/hotlcc/wechat4j/Wechat.java index 91859c0..b88a36a 100644 --- a/src/main/java/com/hotlcc/wechat4j/Wechat.java +++ b/src/main/java/com/hotlcc/wechat4j/Wechat.java @@ -640,7 +640,6 @@ public class Wechat { long start = System.currentTimeMillis(); try { - //API调用异常导致退出 JSONObject result = webWeixinApi.syncCheck(httpClient, urlVersion, new BaseRequest(wxsid, skey, wxuin), getSyncKeyList(false)); logger.debug("微信同步监听心跳返回数据:{}", result); if (result == null) { From 571d9b4287488462d5cc8c67efe84d4eea4f5bc7 Mon Sep 17 00:00:00 2001 From: hotlcc Date: Fri, 17 Aug 2018 16:21:04 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/hotlcc/wechat4j/api/WebWeixinApi.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/hotlcc/wechat4j/api/WebWeixinApi.java b/src/main/java/com/hotlcc/wechat4j/api/WebWeixinApi.java index fe280cb..e6ecc81 100644 --- a/src/main/java/com/hotlcc/wechat4j/api/WebWeixinApi.java +++ b/src/main/java/com/hotlcc/wechat4j/api/WebWeixinApi.java @@ -405,8 +405,8 @@ public class WebWeixinApi { .render(); HttpGet httpGet = new HttpGet(url); - RequestConfig config = RequestConfig.custom(). - setRedirectsEnabled(false) + RequestConfig config = RequestConfig.custom() + .setRedirectsEnabled(false) .build(); httpGet.setConfig(config);