2
0
mirror of https://gitee.com/hotlcc/wechat4j.git synced 2026-01-13 07:11:08 +08:00

8 Commits

Author SHA1 Message Date
Allen
953d66e1aa 代码调整 2019-09-08 20:50:18 +08:00
Allen
28d49bbb84 代码调整 2019-09-08 04:04:15 +08:00
Allen
aa24047672 代码调整 2019-09-08 04:01:57 +08:00
Allen
048e793096 代码调整 2019-09-08 03:44:27 +08:00
Allen
430b18b32a 代码调整 2019-09-08 03:34:25 +08:00
Allen
cc98ea74b1 代码调整 2019-09-08 03:23:05 +08:00
Allen
56791e7965 !15 更新了readme文档
Merge pull request !15 from Allen/feature/20180914
2019-04-22 11:30:21 +08:00
Allen
fcd0316320 [update] 更新readme文档 2019-04-22 11:29:24 +08:00
23 changed files with 226 additions and 328 deletions

View File

@@ -29,7 +29,7 @@ wechat.autoLogin();
```java
// 通过userName发送文本消息
JSONObject sendText(String content, String userName);
JSONObject sendTextToUserName(String content, String userName);
// 通过昵称发送文本消息
JSONObject sendTextToNickName(String content, String nickName);
// 通过备注名发送文本消息
@@ -42,8 +42,8 @@ JSONObject sendText(String userName, String nickName, String remarkName, String
```java
// 通过userName发送图片消息
JSONObject sendImage(String userName, byte[] mediaData, String mediaName, ContentType contentType);
JSONObject sendImage(String userName, File image);
JSONObject sendImageToUserName(String userName, byte[] mediaData, String mediaName, ContentType contentType);
JSONObject sendImageToUserName(String userName, File image);
// 通过昵称发送图片消息
JSONObject sendImageToNickName(String nickName, byte[] mediaData, String mediaName, ContentType contentType);
JSONObject sendImageToNickName(String nickName, File image);
@@ -59,8 +59,8 @@ JSONObject sendImage(String userName, String nickName, String remarkName, File i
```java
// 通过userName发送视频消息
JSONObject sendVideo(String userName, byte[] mediaData, String mediaName, ContentType contentType);
JSONObject sendVideo(String userName, File video);
JSONObject sendVideoToUserName(String userName, byte[] mediaData, String mediaName, ContentType contentType);
JSONObject sendVideoToUserName(String userName, File video);
// 通过昵称发送视频消息
JSONObject sendVideoToNickName(String nickName, byte[] mediaData, String mediaName, ContentType contentType);
JSONObject sendVideoToNickName(String nickName, File video);

View File

@@ -85,6 +85,12 @@
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.4</version>
</dependency>
</dependencies>
<build>

View File

@@ -1,12 +1,27 @@
package com.hotlcc.wechat4j;
import cn.hutool.core.io.FileUtil;
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.*;
import com.hotlcc.wechat4j.enums.ExitType;
import com.hotlcc.wechat4j.enums.LoginTip;
import com.hotlcc.wechat4j.enums.MediaType;
import com.hotlcc.wechat4j.enums.MsgType;
import com.hotlcc.wechat4j.enums.Retcode;
import com.hotlcc.wechat4j.enums.Selector;
import com.hotlcc.wechat4j.handler.ExitEventHandler;
import com.hotlcc.wechat4j.handler.ReceivedMsgHandler;
import com.hotlcc.wechat4j.model.*;
import com.hotlcc.wechat4j.util.*;
import com.hotlcc.wechat4j.model.BaseRequest;
import com.hotlcc.wechat4j.model.MediaMessage;
import com.hotlcc.wechat4j.model.ReceivedMsg;
import com.hotlcc.wechat4j.model.UserInfo;
import com.hotlcc.wechat4j.model.WxMessage;
import com.hotlcc.wechat4j.util.PropertiesUtil;
import com.hotlcc.wechat4j.util.QRCodeUtil;
import com.hotlcc.wechat4j.util.WebWeixinApiUtil;
import com.hotlcc.wechat4j.util.WechatUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
@@ -222,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 + "");
@@ -265,7 +280,7 @@ public class Wechat {
pw.flush();
getWxUuid(pw, 0);
CommonUtil.threadSleep(2000);
ThreadUtil.sleep(2000);
continue;
}
@@ -383,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;
@@ -445,7 +460,7 @@ public class Wechat {
pw.println();
pw.flush();
CommonUtil.threadSleep(2000);
ThreadUtil.sleep(2000);
continue;
}
@@ -507,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;
@@ -677,7 +692,7 @@ public class Wechat {
if (i == 0) {
log.info("同步监听请求失败,正在重试...");
} else if (i > 0) {
log.info("第{}次重试失败" + i);
log.info("第{}次重试失败", i);
}
if (i >= time) {
@@ -693,7 +708,7 @@ public class Wechat {
//如果时间太短则阻塞2秒
long end = System.currentTimeMillis();
if (end - start < 2000) {
CommonUtil.threadSleep(2000);
ThreadUtil.sleep(2000);
}
}
@@ -726,7 +741,7 @@ public class Wechat {
try {
switch (type) {
case ERROR_EXIT:
handler.handleErrorExitEvent(wechat);
handler.handleErrorExitEvent(wechat, t);
break;
case REMOTE_EXIT:
handler.handleRemoteExitEvent(wechat);
@@ -1035,7 +1050,7 @@ public class Wechat {
* @return 返回数据
*/
public UserInfo getContactByUserName(boolean update, String userName) {
if (StringUtil.isEmpty(userName)) {
if (StrUtil.isEmpty(userName)) {
return null;
}
@@ -1069,7 +1084,7 @@ public class Wechat {
* @return 返回数据
*/
public UserInfo getContactByNickName(boolean update, String nickName) {
if (StringUtil.isEmpty(nickName)) {
if (StrUtil.isEmpty(nickName)) {
return null;
}
@@ -1103,7 +1118,7 @@ public class Wechat {
* @return 返回数据
*/
public UserInfo getContactByRemarkName(boolean update, String remarkName) {
if (StringUtil.isEmpty(remarkName)) {
if (StrUtil.isEmpty(remarkName)) {
return null;
}
@@ -1139,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);
@@ -1161,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());
@@ -1185,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);
@@ -1203,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);
}
@@ -1213,7 +1228,7 @@ public class Wechat {
}
String userName = userInfo.getUserName();
if (StringUtil.isEmpty(userName)) {
if (StrUtil.isEmpty(userName)) {
return null;
}
@@ -1228,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);
}
@@ -1238,7 +1253,7 @@ public class Wechat {
}
String userName = userInfo.getUserName();
if (StringUtil.isEmpty(userName)) {
if (StrUtil.isEmpty(userName)) {
return null;
}
@@ -1257,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);
@@ -1272,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);
@@ -1289,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);
// 上传媒体文件
@@ -1308,7 +1323,7 @@ public class Wechat {
}
String mediaId = result.getString("MediaId");
if (StringUtil.isEmpty(mediaId)) {
if (StrUtil.isEmpty(mediaId)) {
return result;
}
@@ -1335,8 +1350,8 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendImageToUserName(String userName, File image) {
ContentType contentType = FileUtil.getContentType(image);
byte[] mediaData = FileUtil.getBytes(image);
ContentType contentType = WechatUtil.getContentType(image);
byte[] mediaData = FileUtil.readBytes(image);
return sendImageToUserName(userName, mediaData, image.getName(), contentType);
}
@@ -1350,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);
}
@@ -1360,7 +1375,7 @@ public class Wechat {
}
String userName = userInfo.getUserName();
if (StringUtil.isEmpty(userName)) {
if (StrUtil.isEmpty(userName)) {
return null;
}
@@ -1375,8 +1390,8 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendImageToNickName(String nickName, File image) {
ContentType contentType = FileUtil.getContentType(image);
byte[] mediaData = FileUtil.getBytes(image);
ContentType contentType = WechatUtil.getContentType(image);
byte[] mediaData = FileUtil.readBytes(image);
return sendImageToNickName(nickName, mediaData, image.getName(), contentType);
}
@@ -1390,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);
}
@@ -1400,7 +1415,7 @@ public class Wechat {
}
String userName = userInfo.getUserName();
if (StringUtil.isEmpty(userName)) {
if (StrUtil.isEmpty(userName)) {
return null;
}
@@ -1415,8 +1430,8 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendImageToRemarkName(String remarkName, File image) {
ContentType contentType = FileUtil.getContentType(image);
byte[] mediaData = FileUtil.getBytes(image);
ContentType contentType = WechatUtil.getContentType(image);
byte[] mediaData = FileUtil.readBytes(image);
return sendImageToRemarkName(remarkName, mediaData, image.getName(), contentType);
}
@@ -1434,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);
@@ -1449,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);
@@ -1465,8 +1480,8 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendImage(String userName, String nickName, String remarkName, File image) {
ContentType contentType = FileUtil.getContentType(image);
byte[] mediaData = FileUtil.getBytes(image);
ContentType contentType = WechatUtil.getContentType(image);
byte[] mediaData = FileUtil.readBytes(image);
return sendImage(userName, nickName, remarkName, mediaData, image.getName(), contentType);
}
@@ -1481,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);
// 上传媒体文件
@@ -1500,7 +1515,7 @@ public class Wechat {
}
String mediaId = result.getString("MediaId");
if (StringUtil.isEmpty(mediaId)) {
if (StrUtil.isEmpty(mediaId)) {
return result;
}
@@ -1527,8 +1542,8 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendVideoToUserName(String userName, File video) {
ContentType contentType = FileUtil.getContentType(video);
byte[] mediaData = FileUtil.getBytes(video);
ContentType contentType = WechatUtil.getContentType(video);
byte[] mediaData = FileUtil.readBytes(video);
return sendVideoToUserName(userName, mediaData, video.getName(), contentType);
}
@@ -1542,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);
}
@@ -1552,7 +1567,7 @@ public class Wechat {
}
String userName = userInfo.getUserName();
if (StringUtil.isEmpty(userName)) {
if (StrUtil.isEmpty(userName)) {
return null;
}
@@ -1567,8 +1582,8 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendVideoToNickName(String nickName, File video) {
ContentType contentType = FileUtil.getContentType(video);
byte[] mediaData = FileUtil.getBytes(video);
ContentType contentType = WechatUtil.getContentType(video);
byte[] mediaData = FileUtil.readBytes(video);
return sendVideoToNickName(nickName, mediaData, video.getName(), contentType);
}
@@ -1582,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);
}
@@ -1592,7 +1607,7 @@ public class Wechat {
}
String userName = userInfo.getUserName();
if (StringUtil.isEmpty(userName)) {
if (StrUtil.isEmpty(userName)) {
return null;
}
@@ -1607,8 +1622,8 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendVideoToRemarkName(String remarkName, File video) {
ContentType contentType = FileUtil.getContentType(video);
byte[] mediaData = FileUtil.getBytes(video);
ContentType contentType = WechatUtil.getContentType(video);
byte[] mediaData = FileUtil.readBytes(video);
return sendVideoToRemarkName(remarkName, mediaData, video.getName(), contentType);
}
@@ -1626,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);
@@ -1641,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);
@@ -1657,8 +1672,8 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendVideo(String userName, String nickName, String remarkName, File video) {
ContentType contentType = FileUtil.getContentType(video);
byte[] mediaData = FileUtil.getBytes(video);
ContentType contentType = WechatUtil.getContentType(video);
byte[] mediaData = FileUtil.readBytes(video);
return sendVideo(userName, nickName, remarkName, mediaData, video.getName(), contentType);
}
}

View File

@@ -9,9 +9,10 @@ import lombok.AllArgsConstructor;
*/
@AllArgsConstructor
public enum ExitType {
ERROR_EXIT("错误退出"),
LOCAL_EXIT("本地退出"),
REMOTE_EXIT("远程退出");
private String desc;
/** 错误退出 */
ERROR_EXIT,
/** 本地退出 */
LOCAL_EXIT,
/** 远程退出 */
REMOTE_EXIT
}

View File

@@ -8,18 +8,13 @@ import lombok.Getter;
*
* @author Allen
*/
@SuppressWarnings("unused")
@Getter
@AllArgsConstructor
public enum LoginTip {
TIP_0(0, "扫码登录"),
TIP_1(1, "确认登录");
/** 扫码登录 */
TIP_0(0),
/** 确认登录 */
TIP_1(1);
private int code;
private String desc;
@Override
public String toString() {
return code + "";
}
}

View File

@@ -11,11 +11,13 @@ import lombok.Getter;
@Getter
@AllArgsConstructor
public enum MediaType {
/** pic */
PICTURE(4, "pic"),
/** video */
VIDEO(4, "video");
public static String REQUEST_KEY = "mediatype";
public static String REQUEST_JSON_KEY = "MediaType";
public static final String REQUEST_KEY = "mediatype";
public static final String REQUEST_JSON_KEY = "MediaType";
private Integer code;
private String value;

View File

@@ -12,25 +12,42 @@ import lombok.Getter;
@AllArgsConstructor
@Getter
public enum MsgType {
TEXT_MSG(1, "文本消息"),
IMAGE_MSG(3, "图片消息"),
VOICE_MSG(34, "语音消息"),
VERIFY_MSG(37, "验证消息"),
POSSIBLE_FRIEND_MSG(40, "可能的朋友的消息"),
SHARE_CARD_MSG(42, "共享名片"),
VIDEO_CALL_MSG(43, "视频通话消息"),
ANIMATED_STICKER_MSG(47, "动画表情"),
LOCATION_MSG(48, "位置消息"),
SHARE_LINK_MSG(49, "分享链接"),
VOIP_MSG(50, "VoIP消息"),
INIT_MSG(51, "初始化消息"),
VOIP_NOTIFY_MSG(52, "VoIP通知"),
VOIP_INVITE_MSG(53, "VoIP邀请"),
VIDEO_MSG(62, "小视频"),
SYS_NOTICE_MSG(9999, "系统通知"),
SYSTEM_MSG(10000, "系统消息"),
WITHDRAW_MSG(10002, "撤回消息");
/** 文本消息 */
TEXT_MSG(1),
/** 图片消息 */
IMAGE_MSG(3),
/** 语音消息 */
VOICE_MSG(34),
/** 验证消息 */
VERIFY_MSG(37),
/** 可能的朋友的消息 */
POSSIBLE_FRIEND_MSG(40),
/** 共享名片 */
SHARE_CARD_MSG(42),
/** 视频通话消息 */
VIDEO_CALL_MSG(43),
/** 动画表情 */
ANIMATED_STICKER_MSG(47),
/** 位置消息 */
LOCATION_MSG(48),
/** 分享链接 */
SHARE_LINK_MSG(49),
/** VoIP消息 */
VOIP_MSG(50),
/** 初始化消息 */
INIT_MSG(51),
/** VoIP通知 */
VOIP_NOTIFY_MSG(52),
/** VoIP邀请 */
VOIP_INVITE_MSG(53),
/** 小视频 */
VIDEO_MSG(62),
/** 系统通知 */
SYS_NOTICE_MSG(9999),
/** 系统消息 */
SYSTEM_MSG(10000),
/** 撤回消息 */
WITHDRAW_MSG(10002);
private int code;
private String desc;
}

View File

@@ -11,10 +11,15 @@ import lombok.Getter;
@Getter
@AllArgsConstructor
public enum OperatingSystem {
/** darwin */
DARWIN("darwin"),
/** windows */
WINDOWS("windows"),
/** linux */
LINUX("linux"),
/** mac os */
MAC_OS("mac"),
/** other */
OTHER("other");
private String value;

View File

@@ -11,12 +11,14 @@ import lombok.Getter;
@AllArgsConstructor
@Getter
public enum Retcode {
RECODE_0(0, "正常"),
RECODE_1100(1100, "失败/登出微信"),
RECODE_1101(1101, "从其它设备登录微信");
/** 正常 */
RECODE_0(0),
/** 失败/登出微信 */
RECODE_1100(1100),
/** 从其它设备登录微信 */
RECODE_1101(1101);
private int code;
private String desc;
public static Retcode valueOf(int code) {
Retcode[] es = values();

View File

@@ -9,14 +9,18 @@ import lombok.AllArgsConstructor;
*/
@AllArgsConstructor
public enum Selector {
SELECTOR_0(0, "正常"),
SELECTOR_2(2, "有新消息"),
SELECTOR_4(4, "目前发现修改了联系人备注会出现"),
SELECTOR_6(6, "目前不知道代表什么"),
SELECTOR_7(7, "手机操作了微信");
/** 正常 */
SELECTOR_0(0),
/** 有新消息 */
SELECTOR_2(2),
/** 目前发现修改了联系人备注会出现 */
SELECTOR_4(4),
/** 目前不知道代表什么 */
SELECTOR_6(6),
/** 手机操作了微信 */
SELECTOR_7(7);
private int code;
private String desc;
public static Selector valueOf(int code) {
Selector[] es = values();

View File

@@ -23,7 +23,7 @@ public interface ExitEventHandler {
*
* @param wechat 微信客户端
*/
void handleErrorExitEvent(Wechat wechat);
void handleErrorExitEvent(Wechat wechat, Throwable t);
/**
* 针对远程人为导致的退出事件

View File

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
@@ -18,11 +19,9 @@ import java.util.List;
*/
@Getter
@Setter
@NoArgsConstructor
public final class AppInfo implements Serializable {
private static final long serialVersionUID = 1L;
private AppInfo() {
}
private static final long serialVersionUID = 6409793898938883312L;
@JSONField(name = "Type")
private Integer type;

View File

@@ -2,7 +2,9 @@ package com.hotlcc.wechat4j.model;
import com.alibaba.fastjson.annotation.JSONField;
import com.hotlcc.wechat4j.util.WechatUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
@@ -14,18 +16,10 @@ import java.io.Serializable;
*/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class BaseRequest implements Serializable {
private static final long serialVersionUID = 1L;
public BaseRequest() {
}
public BaseRequest(String deviceID, String sid, String skey, String uin) {
this.deviceID = deviceID;
this.sid = sid;
this.skey = skey;
this.uin = uin;
}
private static final long serialVersionUID = 9141083671818346541L;
public BaseRequest(String Sid, String Skey, String Uin) {
this(WechatUtil.createDeviceID(), Sid, Skey, Uin);

View File

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
@@ -13,12 +14,10 @@ import java.util.List;
@Getter
@Setter
@NoArgsConstructor
public final class ReceivedMsg implements Serializable {
private static final long serialVersionUID = 1L;
private ReceivedMsg() {
}
@JSONField(name = "SubMsgType")
private Integer subMsgType;
@JSONField(name = "VoiceLength")

View File

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
@@ -18,12 +19,10 @@ import java.util.List;
*/
@Getter
@Setter
@NoArgsConstructor
public final class RecommendInfo implements Serializable {
private static final long serialVersionUID = 1L;
private RecommendInfo() {
}
@JSONField(name = "Ticket")
private String ticket;
@JSONField(name = "UserName")

View File

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
@@ -18,11 +19,9 @@ import java.util.List;
*/
@Getter
@Setter
@NoArgsConstructor
public final class UserInfo implements Serializable {
private static final long serialVersionUID = 1L;
private UserInfo() {
}
private static final long serialVersionUID = 3117101379254847458L;
@JSONField(name = "Uin")
private Long uin;

View File

@@ -14,7 +14,7 @@ import java.io.Serializable;
@Getter
@Setter
public class WxMessage implements Serializable {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 3090487762066388772L;
@JSONField(name = "ClientMsgId")
private String clientMsgId;

View File

@@ -1,41 +0,0 @@
package com.hotlcc.wechat4j.util;
import lombok.extern.slf4j.Slf4j;
/**
* 通用工具类
*
* @author Allen
*/
@Slf4j
public final class CommonUtil {
private CommonUtil() {
}
/**
* 睡眠线程
*
* @param millis 时间
* @param nanos nanos
*/
public static void threadSleep(long millis, int nanos) {
try {
Thread.sleep(millis, nanos);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/**
* 睡眠线程
*
* @param millis 时间
*/
public static void threadSleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,82 +0,0 @@
package com.hotlcc.wechat4j.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import javax.activation.MimetypesFileTypeMap;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* 文件工具类
*
* @author Allen
*/
@Slf4j
public final class FileUtil {
private FileUtil() {
}
/**
* 从文件中获取二进制数据
*
* @param file 文件
* @return 二进制数据
*/
public static byte[] getBytes(File file) {
if (file == null) {
throw new IllegalArgumentException("参数file不能为null");
}
FileInputStream fis = null;
ByteArrayOutputStream baos = null;
try {
fis = new FileInputStream(file);
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
baos.flush();
baos.close();
fis.close();
return baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取文件的ContentType
*
* @param file 文件
* @return ContentType
*/
public static ContentType getContentType(File file) {
String mimeType = new MimetypesFileTypeMap().getContentType(file);
ContentType contentType = ContentType.parse(mimeType);
return contentType;
}
}

View File

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

View File

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

View File

@@ -3,6 +3,10 @@ package com.hotlcc.wechat4j.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.http.entity.ContentType;
import javax.activation.MimetypesFileTypeMap;
import java.io.File;
/**
* 微信工具类
@@ -56,4 +60,16 @@ public final class WechatUtil {
}
return synckey.toString();
}
/**
* 获取文件的ContentType
*
* @param file 文件
* @return ContentType
*/
public static ContentType getContentType(File file) {
String mimeType = new MimetypesFileTypeMap().getContentType(file);
ContentType contentType = ContentType.parse(mimeType);
return contentType;
}
}

View File

@@ -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.CommonUtil;
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());
}
});
@@ -38,16 +38,17 @@ public class TestClass {
public void testSendImage() {
File file = null;
JSONObject result = null;
JSONObject result;
file = new File("C:\\Users\\Administrator\\Pictures\\壁纸\\9e5f4981099bcf351e0ec18c3654aced.jpg");
result = wechat.sendImageToUserName(null, file);
file = new File("C:\\Users\\Administrator\\Videos\\手机QQ视频_20190416170016.mp4");
result = wechat.sendVideoToUserName(null, file);
System.out.println(result);
while (true) CommonUtil.threadSleep(5000);
while (true) {
ThreadUtil.sleep(5000);
}
}
@Test
public void testGetImageData() {
byte[] data = wechat.getContactHeadImgByUserName(null);
FileOutputStream fos = null;
@@ -69,4 +70,9 @@ public class TestClass {
}
}
}
@Test
public void sendMsgToSelf() {
wechat.sendTextToUserName(null, "测试一下");
}
}