diff --git a/src/main/java/com/hotlcc/wechat4j/Wechat.java b/src/main/java/com/hotlcc/wechat4j/Wechat.java index f17c462..a047e72 100644 --- a/src/main/java/com/hotlcc/wechat4j/Wechat.java +++ b/src/main/java/com/hotlcc/wechat4j/Wechat.java @@ -1,6 +1,7 @@ package com.hotlcc.wechat4j; import cn.hutool.core.thread.ThreadUtil; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hotlcc.wechat4j.enums.ExitType; @@ -19,7 +20,6 @@ import com.hotlcc.wechat4j.model.WxMessage; import com.hotlcc.wechat4j.util.FileUtil; import com.hotlcc.wechat4j.util.PropertiesUtil; import com.hotlcc.wechat4j.util.QRCodeUtil; -import com.hotlcc.wechat4j.util.StringUtil; import com.hotlcc.wechat4j.util.WebWeixinApiUtil; import com.hotlcc.wechat4j.util.WechatUtil; import lombok.extern.slf4j.Slf4j; @@ -237,7 +237,7 @@ public class Wechat { return null; } - if (StringUtil.isEmpty(uuid)) { + if (StrUtil.isEmpty(uuid)) { pw.print("\t失败"); if (i == 0 && time > 0) { pw.print(",将重复尝试" + time + "次"); @@ -398,7 +398,7 @@ public class Wechat { } String uuid = result.getString("uuid"); - if (StringUtil.isEmpty(uuid)) { + if (StrUtil.isEmpty(uuid)) { pw.println("\t失败:空值"); pw.flush(); return null; @@ -522,13 +522,13 @@ public class Wechat { // 2、登录 // 2.1、获取uuid String uuid = null; - if (tryPushLogin && StringUtil.isNotEmpty(wxuin)) { + if (tryPushLogin && StrUtil.isNotEmpty(wxuin)) { uuid = getWxUuid(pw, wxuin); } - if (StringUtil.isEmpty(uuid)) { + if (StrUtil.isEmpty(uuid)) { uuid = getWxUuid(pw, time); } - if (StringUtil.isEmpty(uuid)) { + if (StrUtil.isEmpty(uuid)) { pw.println("无法获取uuid,登录不成功"); pw.flush(); return false; @@ -1050,7 +1050,7 @@ public class Wechat { * @return 返回数据 */ public UserInfo getContactByUserName(boolean update, String userName) { - if (StringUtil.isEmpty(userName)) { + if (StrUtil.isEmpty(userName)) { return null; } @@ -1084,7 +1084,7 @@ public class Wechat { * @return 返回数据 */ public UserInfo getContactByNickName(boolean update, String nickName) { - if (StringUtil.isEmpty(nickName)) { + if (StrUtil.isEmpty(nickName)) { return null; } @@ -1118,7 +1118,7 @@ public class Wechat { * @return 返回数据 */ public UserInfo getContactByRemarkName(boolean update, String remarkName) { - if (StringUtil.isEmpty(remarkName)) { + if (StrUtil.isEmpty(remarkName)) { return null; } @@ -1154,11 +1154,11 @@ public class Wechat { * @return 返回数据 */ public UserInfo getContact(boolean update, String userName, String nickName, String remarkName) { - if (StringUtil.isNotEmpty(userName)) { + if (StrUtil.isNotEmpty(userName)) { return getContactByUserName(update, userName); - } else if (StringUtil.isNotEmpty(nickName)) { + } else if (StrUtil.isNotEmpty(nickName)) { return getContactByNickName(update, nickName); - } else if (StringUtil.isNotEmpty(remarkName)) { + } else if (StrUtil.isNotEmpty(remarkName)) { return getContactByRemarkName(update, remarkName); } else { return getLoginUser(update); @@ -1176,7 +1176,7 @@ public class Wechat { * @return 返回数据 */ public byte[] getContactHeadImgByUserName(String userName) { - if (!StringUtil.isEmpty(userName)) { + if (!StrUtil.isEmpty(userName)) { return WebWeixinApiUtil.getContactHeadImg(httpClient, urlVersion, userName); } else { return WebWeixinApiUtil.getContactHeadImg(httpClient, urlVersion, getLoginUserName()); @@ -1200,7 +1200,7 @@ public class Wechat { message.setContent(content); message.setFromUserName(loginUserName); message.setLocalID(msgId); - if (StringUtil.isEmpty(userName)) { + if (StrUtil.isEmpty(userName)) { message.setToUserName(loginUserName); } else { message.setToUserName(userName); @@ -1218,7 +1218,7 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendTextToNickName(String nickName, String content) { - if (StringUtil.isEmpty(nickName)) { + if (StrUtil.isEmpty(nickName)) { return sendTextToUserName(null, content); } @@ -1228,7 +1228,7 @@ public class Wechat { } String userName = userInfo.getUserName(); - if (StringUtil.isEmpty(userName)) { + if (StrUtil.isEmpty(userName)) { return null; } @@ -1243,7 +1243,7 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendTextToRemarkName(String remarkName, String content) { - if (StringUtil.isEmpty(remarkName)) { + if (StrUtil.isEmpty(remarkName)) { return sendTextToUserName(null, content); } @@ -1253,7 +1253,7 @@ public class Wechat { } String userName = userInfo.getUserName(); - if (StringUtil.isEmpty(userName)) { + if (StrUtil.isEmpty(userName)) { return null; } @@ -1272,11 +1272,11 @@ public class Wechat { public JSONObject sendText(String userName, String nickName, String remarkName, String content) { UserInfo userInfo; - if (StringUtil.isNotEmpty(userName)) { + if (StrUtil.isNotEmpty(userName)) { return sendTextToUserName(userName, content); - } else if (StringUtil.isNotEmpty(nickName)) { + } else if (StrUtil.isNotEmpty(nickName)) { userInfo = getContactByNickName(false, nickName); - } else if (StringUtil.isNotEmpty(remarkName)) { + } else if (StrUtil.isNotEmpty(remarkName)) { userInfo = getContactByRemarkName(false, remarkName); } else { String loginUserName = getLoginUserName(false); @@ -1287,7 +1287,7 @@ public class Wechat { return null; } userName = userInfo.getUserName(); - if (StringUtil.isEmpty(userName)) { + if (StrUtil.isEmpty(userName)) { return null; } return sendTextToUserName(userName, content); @@ -1304,7 +1304,7 @@ public class Wechat { */ public JSONObject sendImageToUserName(String userName, byte[] mediaData, String mediaName, ContentType contentType) { String loginUserName = getLoginUserName(false); - String toUserName = StringUtil.isEmpty(userName) ? loginUserName : userName; + String toUserName = StrUtil.isEmpty(userName) ? loginUserName : userName; BaseRequest baseRequest = new BaseRequest(wxsid, skey, wxuin); // 上传媒体文件 @@ -1323,7 +1323,7 @@ public class Wechat { } String mediaId = result.getString("MediaId"); - if (StringUtil.isEmpty(mediaId)) { + if (StrUtil.isEmpty(mediaId)) { return result; } @@ -1365,7 +1365,7 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendImageToNickName(String nickName, byte[] mediaData, String mediaName, ContentType contentType) { - if (StringUtil.isEmpty(nickName)) { + if (StrUtil.isEmpty(nickName)) { return sendImageToUserName(null, mediaData, mediaName, contentType); } @@ -1375,7 +1375,7 @@ public class Wechat { } String userName = userInfo.getUserName(); - if (StringUtil.isEmpty(userName)) { + if (StrUtil.isEmpty(userName)) { return null; } @@ -1405,7 +1405,7 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendImageToRemarkName(String remarkName, byte[] mediaData, String mediaName, ContentType contentType) { - if (StringUtil.isEmpty(remarkName)) { + if (StrUtil.isEmpty(remarkName)) { return sendImageToUserName(null, mediaData, mediaName, contentType); } @@ -1415,7 +1415,7 @@ public class Wechat { } String userName = userInfo.getUserName(); - if (StringUtil.isEmpty(userName)) { + if (StrUtil.isEmpty(userName)) { return null; } @@ -1449,11 +1449,11 @@ public class Wechat { public JSONObject sendImage(String userName, String nickName, String remarkName, byte[] mediaData, String mediaName, ContentType contentType) { UserInfo userInfo; - if (StringUtil.isNotEmpty(userName)) { + if (StrUtil.isNotEmpty(userName)) { return sendImageToUserName(userName, mediaData, mediaName, contentType); - } else if (StringUtil.isNotEmpty(nickName)) { + } else if (StrUtil.isNotEmpty(nickName)) { userInfo = getContactByNickName(false, nickName); - } else if (StringUtil.isNotEmpty(remarkName)) { + } else if (StrUtil.isNotEmpty(remarkName)) { userInfo = getContactByRemarkName(false, remarkName); } else { String loginUserName = getLoginUserName(false); @@ -1464,7 +1464,7 @@ public class Wechat { return null; } userName = userInfo.getUserName(); - if (StringUtil.isEmpty(userName)) { + if (StrUtil.isEmpty(userName)) { return null; } return sendImageToUserName(userName, mediaData, mediaName, contentType); @@ -1496,7 +1496,7 @@ public class Wechat { */ public JSONObject sendVideoToUserName(String userName, byte[] mediaData, String mediaName, ContentType contentType) { String loginUserName = getLoginUserName(false); - String toUserName = StringUtil.isEmpty(userName) ? loginUserName : userName; + String toUserName = StrUtil.isEmpty(userName) ? loginUserName : userName; BaseRequest baseRequest = new BaseRequest(wxsid, skey, wxuin); // 上传媒体文件 @@ -1515,7 +1515,7 @@ public class Wechat { } String mediaId = result.getString("MediaId"); - if (StringUtil.isEmpty(mediaId)) { + if (StrUtil.isEmpty(mediaId)) { return result; } @@ -1557,7 +1557,7 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendVideoToNickName(String nickName, byte[] mediaData, String mediaName, ContentType contentType) { - if (StringUtil.isEmpty(nickName)) { + if (StrUtil.isEmpty(nickName)) { return sendVideoToUserName(null, mediaData, mediaName, contentType); } @@ -1567,7 +1567,7 @@ public class Wechat { } String userName = userInfo.getUserName(); - if (StringUtil.isEmpty(userName)) { + if (StrUtil.isEmpty(userName)) { return null; } @@ -1597,7 +1597,7 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendVideoToRemarkName(String remarkName, byte[] mediaData, String mediaName, ContentType contentType) { - if (StringUtil.isEmpty(remarkName)) { + if (StrUtil.isEmpty(remarkName)) { return sendVideoToUserName(null, mediaData, mediaName, contentType); } @@ -1607,7 +1607,7 @@ public class Wechat { } String userName = userInfo.getUserName(); - if (StringUtil.isEmpty(userName)) { + if (StrUtil.isEmpty(userName)) { return null; } @@ -1641,11 +1641,11 @@ public class Wechat { public JSONObject sendVideo(String userName, String nickName, String remarkName, byte[] mediaData, String mediaName, ContentType contentType) { UserInfo userInfo; - if (StringUtil.isNotEmpty(userName)) { + if (StrUtil.isNotEmpty(userName)) { return sendVideoToUserName(userName, mediaData, mediaName, contentType); - } else if (StringUtil.isNotEmpty(nickName)) { + } else if (StrUtil.isNotEmpty(nickName)) { userInfo = getContactByNickName(false, nickName); - } else if (StringUtil.isNotEmpty(remarkName)) { + } else if (StrUtil.isNotEmpty(remarkName)) { userInfo = getContactByRemarkName(false, remarkName); } else { String loginUserName = getLoginUserName(false); @@ -1656,7 +1656,7 @@ public class Wechat { return null; } userName = userInfo.getUserName(); - if (StringUtil.isEmpty(userName)) { + if (StrUtil.isEmpty(userName)) { return null; } return sendVideoToUserName(userName, mediaData, mediaName, contentType); diff --git a/src/main/java/com/hotlcc/wechat4j/util/StringUtil.java b/src/main/java/com/hotlcc/wechat4j/util/StringUtil.java deleted file mode 100644 index 3c34f00..0000000 --- a/src/main/java/com/hotlcc/wechat4j/util/StringUtil.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.hotlcc.wechat4j.util; - -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.util.UUID; - -/** - * 字符串工具类 - * - * @author Allen - */ -public final class StringUtil { - private StringUtil() { - } - - public static boolean isEmpty(String str) { - return str == null || "".equals(str); - } - - public static boolean isNotEmpty(String str) { - return !isEmpty(str); - } - - public static String encodeURL(String str, String enc) { - try { - return URLEncoder.encode(str, enc); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - - public static String decodeURL(String str, String enc) { - try { - return URLDecoder.decode(str, enc); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - - public static String getUuid() { - return UUID.randomUUID().toString().replace("-", "").toLowerCase(); - } -} diff --git a/src/main/java/com/hotlcc/wechat4j/util/WebWeixinApiUtil.java b/src/main/java/com/hotlcc/wechat4j/util/WebWeixinApiUtil.java index 65c9f0b..68c84b3 100644 --- a/src/main/java/com/hotlcc/wechat4j/util/WebWeixinApiUtil.java +++ b/src/main/java/com/hotlcc/wechat4j/util/WebWeixinApiUtil.java @@ -1,5 +1,7 @@ package com.hotlcc.wechat4j.util; +import cn.hutool.core.util.StrUtil; +import cn.hutool.core.util.URLUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hotlcc.wechat4j.enums.LoginTip; @@ -9,7 +11,11 @@ import com.hotlcc.wechat4j.model.MediaMessage; import com.hotlcc.wechat4j.model.WxMessage; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; -import org.apache.http.*; +import org.apache.http.Consts; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; @@ -101,7 +107,7 @@ public final class WebWeixinApiUtil { String uuid = matcher.group(2); result.put("uuid", uuid); - if (StringUtil.isEmpty(uuid)) { + if (StrUtil.isEmpty(uuid)) { throw new RuntimeException("获取的uuid为空"); } @@ -274,7 +280,7 @@ public final class WebWeixinApiUtil { String url = new ST(PropertiesUtil.getProperty("webwx-url.logout_url")) .add("urlVersion", urlVersion) .add("type", i) - .add("skey", StringUtil.encodeURL(baseRequest.getSkey(), Consts.UTF_8.name())) + .add("skey", URLUtil.encode(baseRequest.getSkey())) .render(); HttpPost httpPost = new HttpPost(url); @@ -342,7 +348,7 @@ public final class WebWeixinApiUtil { try { String url = new ST(PropertiesUtil.getProperty("webwx-url.webwxinit_url")) .add("urlVersion", urlVersion) - .add("pass_ticket", StringUtil.encodeURL(passticket, Consts.UTF_8.name())) + .add("pass_ticket", URLUtil.encode(passticket)) .add("r", System.currentTimeMillis() / 1252L) .render(); @@ -388,7 +394,7 @@ public final class WebWeixinApiUtil { try { String url = new ST(PropertiesUtil.getProperty("webwx-url.statusnotify_url")) .add("urlVersion", urlVersion) - .add("pass_ticket", StringUtil.encodeURL(passticket, Consts.UTF_8.name())) + .add("pass_ticket", URLUtil.encode(passticket)) .render(); HttpPost httpPost = new HttpPost(url); @@ -437,11 +443,11 @@ public final class WebWeixinApiUtil { String url = new ST(PropertiesUtil.getProperty("webwx-url.synccheck_url")) .add("urlVersion", urlVersion) .add("r", millis) - .add("skey", StringUtil.encodeURL(baseRequest.getSkey(), Consts.UTF_8.name())) + .add("skey", URLUtil.encode(baseRequest.getSkey())) .add("sid", baseRequest.getSid()) .add("uin", baseRequest.getUin()) .add("deviceid", WechatUtil.createDeviceID()) - .add("synckey", StringUtil.encodeURL(WechatUtil.syncKeyListToString(syncKeyList), Consts.UTF_8.name())) + .add("synckey", URLUtil.encode(WechatUtil.syncKeyListToString(syncKeyList))) .add("_", millis) .render(); @@ -494,9 +500,9 @@ public final class WebWeixinApiUtil { try { String url = new ST(PropertiesUtil.getProperty("webwx-url.getcontact_url")) .add("urlVersion", urlVersion) - .add("pass_ticket", StringUtil.encodeURL(passticket, Consts.UTF_8.name())) + .add("pass_ticket", URLUtil.encode(passticket)) .add("r", System.currentTimeMillis()) - .add("skey", StringUtil.encodeURL(skey, Consts.UTF_8.name())) + .add("skey", URLUtil.encode(skey)) .render(); HttpGet httpGet = new HttpGet(url); @@ -535,7 +541,7 @@ public final class WebWeixinApiUtil { try { String url = new ST(PropertiesUtil.getProperty("webwx-url.batchgetcontact_url")) .add("urlVersion", urlVersion) - .add("pass_ticket", StringUtil.encodeURL(passticket, Consts.UTF_8.name())) + .add("pass_ticket", URLUtil.encode(passticket)) .add("r", System.currentTimeMillis()) .render(); @@ -621,7 +627,7 @@ public final class WebWeixinApiUtil { .add("urlVersion", urlVersion) .add("skey", baseRequest.getSkey()) .add("sid", baseRequest.getSid()) - .add("pass_ticket", StringUtil.encodeURL(passticket, Consts.UTF_8.name())) + .add("pass_ticket", URLUtil.encode(passticket)) .render(); HttpPost httpPost = new HttpPost(url); @@ -667,7 +673,7 @@ public final class WebWeixinApiUtil { try { String url = new ST(PropertiesUtil.getProperty("webwx-url.webwxsendmsg_url")) .add("urlVersion", urlVersion) - .add("pass_ticket", StringUtil.encodeURL(passticket, Consts.UTF_8.name())) + .add("pass_ticket", URLUtil.encode(passticket)) .render(); HttpPost httpPost = new HttpPost(url); @@ -817,7 +823,7 @@ public final class WebWeixinApiUtil { try { String url = new ST(PropertiesUtil.getProperty("webwx-url.webwxsendmsgimg_url")) .add("urlVersion", urlVersion) - .add("pass_ticket", StringUtil.encodeURL(passticket, Consts.UTF_8.name())) + .add("pass_ticket", URLUtil.encode(passticket)) .render(); HttpPost httpPost = new HttpPost(url); diff --git a/src/test/java/TestClass.java b/src/test/java/TestClass.java index 92cbdec..c0601f9 100644 --- a/src/test/java/TestClass.java +++ b/src/test/java/TestClass.java @@ -1,10 +1,10 @@ import cn.hutool.core.thread.ThreadUtil; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; import com.hotlcc.wechat4j.Wechat; import com.hotlcc.wechat4j.handler.ReceivedMsgHandler; import com.hotlcc.wechat4j.model.ReceivedMsg; import com.hotlcc.wechat4j.model.UserInfo; -import com.hotlcc.wechat4j.util.StringUtil; import org.junit.Before; import org.junit.Test; @@ -23,7 +23,7 @@ public class TestClass { @Override public void handleAllType(Wechat wechat, ReceivedMsg msg) { UserInfo contact = wechat.getContactByUserName(false, msg.getFromUserName()); - String name = StringUtil.isEmpty(contact.getRemarkName()) ? contact.getNickName() : contact.getRemarkName(); + String name = StrUtil.isEmpty(contact.getRemarkName()) ? contact.getNickName() : contact.getRemarkName(); System.out.println(name + ": " + msg.getContent()); } });