mirror of
https://gitee.com/hotlcc/wechat4j.git
synced 2025-06-08 03:24:09 +08:00
[update] 新增视频消息发送
This commit is contained in:
parent
11f3b99524
commit
a8a2b453ec
17
README.md
17
README.md
@ -55,6 +55,23 @@ JSONObject sendImage(String userName, String nickName, String remarkName, byte[]
|
||||
JSONObject sendImage(String userName, String nickName, String remarkName, File image);
|
||||
```
|
||||
|
||||
### 视频消息
|
||||
|
||||
```java
|
||||
// 通过userName发送视频消息
|
||||
JSONObject sendVideo(String userName, byte[] mediaData, String mediaName, ContentType contentType);
|
||||
JSONObject sendVideo(String userName, File image);
|
||||
// 通过昵称发送视频消息
|
||||
JSONObject sendVideoToNickName(String nickName, byte[] mediaData, String mediaName, ContentType contentType);
|
||||
JSONObject sendVideoToNickName(String nickName, File image);
|
||||
// 通过备注名发送视频消息
|
||||
JSONObject sendVideoToRemarkName(String remarkName, byte[] mediaData, String mediaName, ContentType contentType);
|
||||
JSONObject sendVideoToRemarkName(String remarkName, File image);
|
||||
// 发送视频消息(根据多种名称)
|
||||
JSONObject sendVideo(String userName, String nickName, String remarkName, byte[] mediaData, String mediaName, ContentType contentType);
|
||||
JSONObject sendVideo(String userName, String nickName, String remarkName, File image);
|
||||
```
|
||||
|
||||
> 更多消息类型支持尽请期待。
|
||||
|
||||
## 消息处理器
|
||||
|
@ -8,7 +8,6 @@ import com.hotlcc.wechat4j.handler.ReceivedMsgHandler;
|
||||
import com.hotlcc.wechat4j.model.*;
|
||||
import com.hotlcc.wechat4j.util.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpException;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpRequestInterceptor;
|
||||
import org.apache.http.client.CookieStore;
|
||||
@ -20,7 +19,6 @@ import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@ -180,7 +178,7 @@ public class Wechat {
|
||||
private HttpClient buildHttpClient(CookieStore cookieStore) {
|
||||
HttpRequestInterceptor interceptor = new HttpRequestInterceptor() {
|
||||
@Override
|
||||
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
|
||||
public void process(HttpRequest httpRequest, HttpContext httpContext) {
|
||||
httpRequest.addHeader("User-Agent", PropertiesUtil.getProperty("wechat4j.userAgent"));
|
||||
}
|
||||
};
|
||||
@ -194,8 +192,8 @@ public class Wechat {
|
||||
/**
|
||||
* 获取uuid(登录时)
|
||||
*
|
||||
* @param pw
|
||||
* @param time
|
||||
* @param pw 打印器
|
||||
* @param time 时间
|
||||
* @return
|
||||
*/
|
||||
private String getWxUuid(PrintWriter pw, int time) {
|
||||
@ -244,7 +242,7 @@ public class Wechat {
|
||||
/**
|
||||
* 获取并显示qrcode(登录时)
|
||||
*
|
||||
* @return
|
||||
* @return 是否成功
|
||||
*/
|
||||
private boolean getAndShowQRCode(PrintWriter pw, String uuid, int time) {
|
||||
pw.print("获取二维码...");
|
||||
@ -293,7 +291,7 @@ public class Wechat {
|
||||
|
||||
boolean flag = false;
|
||||
while (true) {
|
||||
JSONObject result = WebWeixinApiUtil.getRedirectUri(httpClient, LoginTipEnum.TIP_0, uuid);
|
||||
JSONObject result = WebWeixinApiUtil.getRedirectUri(httpClient, LoginTip.TIP_0, uuid);
|
||||
if (result == null) {
|
||||
pw.println("\t失败:出现异常");
|
||||
pw.flush();
|
||||
@ -664,10 +662,10 @@ public class Wechat {
|
||||
|
||||
//人为退出
|
||||
int retcode = result.getIntValue("retcode");
|
||||
if (retcode != RetcodeEnum.RECODE_0.getCode()) {
|
||||
if (retcode != Retcode.RECODE_0.getCode()) {
|
||||
log.info("微信退出或从其它设备登录");
|
||||
logout();
|
||||
processExitEvent(ExitTypeEnum.REMOTE_EXIT, null);
|
||||
processExitEvent(ExitType.REMOTE_EXIT, null);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -685,7 +683,7 @@ public class Wechat {
|
||||
if (i >= time) {
|
||||
log.info("重复{}次仍然失败,退出微信", i);
|
||||
logout();
|
||||
processExitEvent(ExitTypeEnum.ERROR_EXIT, e);
|
||||
processExitEvent(ExitType.ERROR_EXIT, e);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -699,7 +697,7 @@ public class Wechat {
|
||||
}
|
||||
}
|
||||
|
||||
processExitEvent(ExitTypeEnum.LOCAL_EXIT, null);
|
||||
processExitEvent(ExitType.LOCAL_EXIT, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -708,7 +706,7 @@ public class Wechat {
|
||||
* @param type 退出类型
|
||||
* @param t 异常
|
||||
*/
|
||||
private void processExitEvent(ExitTypeEnum type, Throwable t) {
|
||||
private void processExitEvent(ExitType type, Throwable t) {
|
||||
try {
|
||||
if (exitEventHandlers == null) {
|
||||
return;
|
||||
@ -724,7 +722,7 @@ public class Wechat {
|
||||
}
|
||||
}
|
||||
|
||||
private void processExitEvent(ExitTypeEnum type, Throwable t, ExitEventHandler handler) {
|
||||
private void processExitEvent(ExitType type, Throwable t, ExitEventHandler handler) {
|
||||
try {
|
||||
switch (type) {
|
||||
case ERROR_EXIT:
|
||||
@ -757,7 +755,7 @@ public class Wechat {
|
||||
*/
|
||||
private void processSelector(int selector) {
|
||||
try {
|
||||
SelectorEnum e = SelectorEnum.valueOf(selector);
|
||||
Selector e = Selector.valueOf(selector);
|
||||
if (e == null) {
|
||||
log.warn("Cannot process unknow selector {}", selector);
|
||||
return;
|
||||
@ -801,7 +799,7 @@ public class Wechat {
|
||||
}
|
||||
|
||||
int ret = baseResponse.getIntValue("Ret");
|
||||
if (ret != RetcodeEnum.RECODE_0.getCode()) {
|
||||
if (ret != Retcode.RECODE_0.getCode()) {
|
||||
log.warn("同步接口返回错误代码:{}", ret);
|
||||
return;
|
||||
}
|
||||
@ -1153,7 +1151,7 @@ public class Wechat {
|
||||
} else {
|
||||
message.setToUserName(userName);
|
||||
}
|
||||
message.setType(MsgTypeEnum.TEXT_MSG.getCode());
|
||||
message.setType(MsgType.TEXT_MSG.getCode());
|
||||
|
||||
return WebWeixinApiUtil.sendMsg(httpClient, urlVersion, passTicket, baseRequest, message);
|
||||
}
|
||||
@ -1257,7 +1255,7 @@ public class Wechat {
|
||||
|
||||
// 上传媒体文件
|
||||
String dataTicket = getCookieValue("webwx_data_ticket");
|
||||
JSONObject result = WebWeixinApiUtil.uploadMedia(httpClient, urlVersion, passTicket, baseRequest, loginUserName, toUserName, dataTicket, mediaData, mediaName, contentType);
|
||||
JSONObject result = WebWeixinApiUtil.uploadMedia(httpClient, urlVersion, passTicket, baseRequest, loginUserName, toUserName, dataTicket, mediaData, mediaName, contentType, MediaType.PICTURE);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
@ -1284,7 +1282,7 @@ public class Wechat {
|
||||
message.setLocalID(msgId);
|
||||
message.setMediaId(mediaId);
|
||||
message.setToUserName(toUserName);
|
||||
message.setType(MsgTypeEnum.IMAGE_MSG.getCode());
|
||||
message.setType(MsgType.IMAGE_MSG.getCode());
|
||||
result = WebWeixinApiUtil.sendImageMsg(httpClient, urlVersion, passTicket, baseRequest, message);
|
||||
|
||||
return result;
|
||||
@ -1433,4 +1431,196 @@ public class Wechat {
|
||||
byte[] mediaData = FileUtil.getBytes(image);
|
||||
return sendImage(userName, nickName, remarkName, mediaData, image.getName(), contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送视频消息
|
||||
*
|
||||
* @param userName 用户名(加密的)
|
||||
* @param mediaData 媒体文件数据
|
||||
* @param mediaName 媒体文件名
|
||||
* @param contentType 媒体文件类型
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideo(String userName, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
String loginUserName = getLoginUserName(false);
|
||||
String toUserName = StringUtil.isEmpty(userName) ? loginUserName : userName;
|
||||
BaseRequest baseRequest = new BaseRequest(wxsid, skey, wxuin);
|
||||
|
||||
// 上传媒体文件
|
||||
String dataTicket = getCookieValue("webwx_data_ticket");
|
||||
JSONObject result = WebWeixinApiUtil.uploadMedia(httpClient, urlVersion, passTicket, baseRequest, loginUserName, toUserName, dataTicket, mediaData, mediaName, contentType, MediaType.VIDEO);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
JSONObject br = result.getJSONObject("BaseResponse");
|
||||
if (br == null) {
|
||||
return result;
|
||||
}
|
||||
int ret = br.getIntValue("Ret");
|
||||
if (ret != 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
String mediaId = result.getString("MediaId");
|
||||
if (StringUtil.isEmpty(mediaId)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 发送视频消息
|
||||
String msgId = WechatUtil.createMsgId();
|
||||
MediaMessage message = new MediaMessage();
|
||||
message.setClientMsgId(msgId);
|
||||
message.setContent("");
|
||||
message.setFromUserName(loginUserName);
|
||||
message.setLocalID(msgId);
|
||||
message.setMediaId(mediaId);
|
||||
message.setToUserName(toUserName);
|
||||
message.setType(MsgType.VIDEO_CALL_MSG.getCode());
|
||||
result = WebWeixinApiUtil.sendVideoMsg(httpClient, urlVersion, baseRequest, message);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送视频消息
|
||||
*
|
||||
* @param userName 用户名(加密的)
|
||||
* @param video 视频文件
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideo(String userName, File video) {
|
||||
ContentType contentType = FileUtil.getContentBody(video);
|
||||
byte[] mediaData = FileUtil.getBytes(video);
|
||||
return sendVideo(userName, mediaData, video.getName(), contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送视频消息(根据昵称)
|
||||
*
|
||||
* @param nickName 昵称
|
||||
* @param mediaData 媒体文件数据
|
||||
* @param mediaName 媒体文件名
|
||||
* @param contentType 媒体文件类型
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideoToNickName(String nickName, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
if (StringUtil.isEmpty(nickName)) {
|
||||
return sendVideo(null, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
UserInfo userInfo = getContactByNickName(false, nickName);
|
||||
if (userInfo == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String userName = userInfo.getUserName();
|
||||
if (StringUtil.isEmpty(userName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sendVideo(userName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送视频消息(根据昵称)
|
||||
*
|
||||
* @param nickName 昵称
|
||||
* @param video 视频文件
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideoToNickName(String nickName, File video) {
|
||||
ContentType contentType = FileUtil.getContentBody(video);
|
||||
byte[] mediaData = FileUtil.getBytes(video);
|
||||
return sendVideoToNickName(nickName, mediaData, video.getName(), contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送视频消息(根据备注名)
|
||||
*
|
||||
* @param remarkName 备注名
|
||||
* @param mediaData 媒体文件数据
|
||||
* @param mediaName 媒体文件名
|
||||
* @param contentType 媒体文件类型
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideoToRemarkName(String remarkName, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
if (StringUtil.isEmpty(remarkName)) {
|
||||
return sendVideo(null, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
UserInfo userInfo = getContactByRemarkName(false, remarkName);
|
||||
if (userInfo == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String userName = userInfo.getUserName();
|
||||
if (StringUtil.isEmpty(userName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sendVideo(userName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送视频消息(根据备注名)
|
||||
*
|
||||
* @param remarkName 备注名
|
||||
* @param video 视频文件
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideoToRemarkName(String remarkName, File video) {
|
||||
ContentType contentType = FileUtil.getContentBody(video);
|
||||
byte[] mediaData = FileUtil.getBytes(video);
|
||||
return sendVideoToRemarkName(remarkName, mediaData, video.getName(), contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送视频消息(根据多种名称)
|
||||
*
|
||||
* @param userName 用户名(加密的)
|
||||
* @param nickName 昵称
|
||||
* @param remarkName 备注名
|
||||
* @param mediaData 媒体文件数据
|
||||
* @param mediaName 媒体文件名
|
||||
* @param contentType 媒体文件类型
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideo(String userName, String nickName, String remarkName, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
UserInfo userInfo;
|
||||
|
||||
if (StringUtil.isNotEmpty(userName)) {
|
||||
return sendVideo(userName, mediaData, mediaName, contentType);
|
||||
} else if (StringUtil.isNotEmpty(nickName)) {
|
||||
userInfo = getContactByNickName(false, nickName);
|
||||
} else if (StringUtil.isNotEmpty(remarkName)) {
|
||||
userInfo = getContactByRemarkName(false, remarkName);
|
||||
} else {
|
||||
String loginUserName = getLoginUserName(false);
|
||||
return sendVideo(loginUserName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
if (userInfo == null) {
|
||||
return null;
|
||||
}
|
||||
userName = userInfo.getUserName();
|
||||
if (StringUtil.isEmpty(userName)) {
|
||||
return null;
|
||||
}
|
||||
return sendVideo(userName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送视频消息(根据多种名称)
|
||||
*
|
||||
* @param userName 用户名(加密的)
|
||||
* @param nickName 昵称
|
||||
* @param remarkName 备注名
|
||||
* @param video 视频文件
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideo(String userName, String nickName, String remarkName, File video) {
|
||||
ContentType contentType = FileUtil.getContentBody(video);
|
||||
byte[] mediaData = FileUtil.getBytes(video);
|
||||
return sendVideo(userName, nickName, remarkName, mediaData, video.getName(), contentType);
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,17 @@
|
||||
package com.hotlcc.wechat4j.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
/**
|
||||
* 微信退出类型
|
||||
*
|
||||
* @author Allen
|
||||
*/
|
||||
public enum ExitTypeEnum {
|
||||
@AllArgsConstructor
|
||||
public enum ExitType {
|
||||
ERROR_EXIT("错误退出"),
|
||||
LOCAL_EXIT("本地退出"),
|
||||
REMOTE_EXIT("远程退出");
|
||||
|
||||
private String desc;
|
||||
|
||||
ExitTypeEnum(String desc) {
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
}
|
@ -1,27 +1,23 @@
|
||||
package com.hotlcc.wechat4j.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 等待确认登录的tip
|
||||
*
|
||||
* @author Allen
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public enum LoginTipEnum {
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum LoginTip {
|
||||
TIP_0(0, "扫码登录"),
|
||||
TIP_1(1, "确认登录");
|
||||
|
||||
private int code;
|
||||
private String desc;
|
||||
|
||||
LoginTipEnum(int code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return code + "";
|
22
src/main/java/com/hotlcc/wechat4j/enums/MediaType.java
Normal file
22
src/main/java/com/hotlcc/wechat4j/enums/MediaType.java
Normal file
@ -0,0 +1,22 @@
|
||||
package com.hotlcc.wechat4j.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author Allen
|
||||
* @version 1.0
|
||||
* @date 2019/4/17 9:51
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum MediaType {
|
||||
PICTURE(4, "pic"),
|
||||
VIDEO(4, "video");
|
||||
|
||||
public static String REQUEST_KEY = "mediatype";
|
||||
public static String REQUEST_JSON_KEY = "MediaType";
|
||||
|
||||
private Integer code;
|
||||
private String value;
|
||||
}
|
@ -1,12 +1,17 @@
|
||||
package com.hotlcc.wechat4j.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 消息类型enum
|
||||
*
|
||||
* @author Allen
|
||||
*/
|
||||
@SuppressWarnings({"unused"})
|
||||
public enum MsgTypeEnum {
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum MsgType {
|
||||
TEXT_MSG(1, "文本消息"),
|
||||
IMAGE_MSG(3, "图片消息"),
|
||||
VOICE_MSG(34, "语音消息"),
|
||||
@ -26,19 +31,6 @@ public enum MsgTypeEnum {
|
||||
SYSTEM_MSG(10000, "系统消息"),
|
||||
WITHDRAW_MSG(10002, "撤回消息");
|
||||
|
||||
MsgTypeEnum(int code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
private int code;
|
||||
private String desc;
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
35
src/main/java/com/hotlcc/wechat4j/enums/OperatingSystem.java
Normal file
35
src/main/java/com/hotlcc/wechat4j/enums/OperatingSystem.java
Normal file
@ -0,0 +1,35 @@
|
||||
package com.hotlcc.wechat4j.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 操作系统enum
|
||||
*
|
||||
* @author Allen
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum OperatingSystem {
|
||||
DARWIN("darwin"),
|
||||
WINDOWS("windows"),
|
||||
LINUX("linux"),
|
||||
MAC_OS("mac"),
|
||||
OTHER("other");
|
||||
|
||||
private String value;
|
||||
|
||||
public static OperatingSystem currentOperatingSystem() {
|
||||
String osName = System.getProperty("os.name").toLowerCase();
|
||||
if (osName.contains(OperatingSystem.DARWIN.getValue())) {
|
||||
return OperatingSystem.DARWIN;
|
||||
} else if (osName.contains(OperatingSystem.WINDOWS.getValue())) {
|
||||
return OperatingSystem.WINDOWS;
|
||||
} else if (osName.contains(OperatingSystem.LINUX.getValue())) {
|
||||
return OperatingSystem.LINUX;
|
||||
} else if (osName.contains(OperatingSystem.MAC_OS.getValue())) {
|
||||
return OperatingSystem.MAC_OS;
|
||||
}
|
||||
return OperatingSystem.OTHER;
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package com.hotlcc.wechat4j.enums;
|
||||
|
||||
/**
|
||||
* 操作系统enum
|
||||
*
|
||||
* @author Allen
|
||||
*/
|
||||
public enum OperatingSystemEnum {
|
||||
DARWIN("darwin"),
|
||||
WINDOWS("windows"),
|
||||
LINUX("linux"),
|
||||
MAC_OS("mac"),
|
||||
OTHER("other");
|
||||
|
||||
private String value;
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
OperatingSystemEnum(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static OperatingSystemEnum currentOperatingSystem() {
|
||||
String osName = System.getProperty("os.name").toLowerCase();
|
||||
if (osName.indexOf(OperatingSystemEnum.DARWIN.getValue()) >= 0) {
|
||||
return OperatingSystemEnum.DARWIN;
|
||||
} else if (osName.indexOf(OperatingSystemEnum.WINDOWS.getValue()) >= 0) {
|
||||
return OperatingSystemEnum.WINDOWS;
|
||||
} else if (osName.indexOf(OperatingSystemEnum.LINUX.getValue()) >= 0) {
|
||||
return OperatingSystemEnum.LINUX;
|
||||
} else if (osName.indexOf(OperatingSystemEnum.MAC_OS.getValue()) >= 0) {
|
||||
return OperatingSystemEnum.MAC_OS;
|
||||
}
|
||||
return OperatingSystemEnum.OTHER;
|
||||
}
|
||||
}
|
@ -1,11 +1,16 @@
|
||||
package com.hotlcc.wechat4j.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Ret代码
|
||||
*
|
||||
* @author Allen
|
||||
*/
|
||||
public enum RetcodeEnum {
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum Retcode {
|
||||
RECODE_0(0, "正常"),
|
||||
RECODE_1100(1100, "失败/登出微信"),
|
||||
RECODE_1101(1101, "从其它设备登录微信");
|
||||
@ -13,18 +18,9 @@ public enum RetcodeEnum {
|
||||
private int code;
|
||||
private String desc;
|
||||
|
||||
RetcodeEnum(int code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public static RetcodeEnum valueOf(int code) {
|
||||
RetcodeEnum[] es = values();
|
||||
for (RetcodeEnum e : es) {
|
||||
public static Retcode valueOf(int code) {
|
||||
Retcode[] es = values();
|
||||
for (Retcode e : es) {
|
||||
if (e.code == code) {
|
||||
return e;
|
||||
}
|
@ -1,11 +1,14 @@
|
||||
package com.hotlcc.wechat4j.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
/**
|
||||
* Selector代码
|
||||
*
|
||||
* @author Allen
|
||||
*/
|
||||
public enum SelectorEnum {
|
||||
@AllArgsConstructor
|
||||
public enum Selector {
|
||||
SELECTOR_0(0, "正常"),
|
||||
SELECTOR_2(2, "有新消息"),
|
||||
SELECTOR_4(4, "目前发现修改了联系人备注会出现"),
|
||||
@ -15,14 +18,9 @@ public enum SelectorEnum {
|
||||
private int code;
|
||||
private String desc;
|
||||
|
||||
SelectorEnum(int code, String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public static SelectorEnum valueOf(int code) {
|
||||
SelectorEnum[] es = values();
|
||||
for (SelectorEnum e : es) {
|
||||
public static Selector valueOf(int code) {
|
||||
Selector[] es = values();
|
||||
for (Selector e : es) {
|
||||
if (e.code == code) {
|
||||
return e;
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package com.hotlcc.wechat4j.handler;
|
||||
|
||||
import com.hotlcc.wechat4j.Wechat;
|
||||
import com.hotlcc.wechat4j.enums.ExitTypeEnum;
|
||||
import com.hotlcc.wechat4j.enums.ExitType;
|
||||
|
||||
/**
|
||||
* 退出事件处理器
|
||||
@ -16,7 +16,7 @@ public interface ExitEventHandler {
|
||||
* @param type 退出类型
|
||||
* @param t 异常
|
||||
*/
|
||||
void handleAllType(Wechat wechat, ExitTypeEnum type, Throwable t);
|
||||
void handleAllType(Wechat wechat, ExitType type, Throwable t);
|
||||
|
||||
/**
|
||||
* 针对错误导致的退出事件
|
||||
|
@ -6,7 +6,7 @@ import com.google.zxing.NotFoundException;
|
||||
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.common.HybridBinarizer;
|
||||
import com.hotlcc.wechat4j.enums.OperatingSystemEnum;
|
||||
import com.hotlcc.wechat4j.enums.OperatingSystem;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
@ -269,7 +269,7 @@ public final class QRCodeUtil {
|
||||
* @param data 二维码图片的字节数据
|
||||
*/
|
||||
public static void openQRCodeImage(byte[] data) {
|
||||
OperatingSystemEnum os = OperatingSystemEnum.currentOperatingSystem();
|
||||
OperatingSystem os = OperatingSystem.currentOperatingSystem();
|
||||
Runtime runtime;
|
||||
File tmp;
|
||||
switch (os) {
|
||||
|
@ -2,7 +2,8 @@ package com.hotlcc.wechat4j.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.hotlcc.wechat4j.enums.LoginTipEnum;
|
||||
import com.hotlcc.wechat4j.enums.LoginTip;
|
||||
import com.hotlcc.wechat4j.enums.MediaType;
|
||||
import com.hotlcc.wechat4j.model.BaseRequest;
|
||||
import com.hotlcc.wechat4j.model.MediaMessage;
|
||||
import com.hotlcc.wechat4j.model.WxMessage;
|
||||
@ -156,7 +157,7 @@ public final class WebWeixinApiUtil {
|
||||
* @return 返回数据
|
||||
*/
|
||||
public static JSONObject getRedirectUri(HttpClient httpClient,
|
||||
LoginTipEnum tip,
|
||||
LoginTip tip,
|
||||
String uuid) {
|
||||
try {
|
||||
long millis = System.currentTimeMillis();
|
||||
@ -672,6 +673,7 @@ public final class WebWeixinApiUtil {
|
||||
* @param mediaData 媒体文件二进制数据
|
||||
* @param mediaName 媒体文件名称
|
||||
* @param contentType 媒体文件类型
|
||||
* @param mediaType 媒体类型
|
||||
* @return 返回数据
|
||||
*/
|
||||
public static JSONObject uploadMedia(HttpClient httpClient,
|
||||
@ -683,7 +685,8 @@ public final class WebWeixinApiUtil {
|
||||
String dataTicket,
|
||||
byte[] mediaData,
|
||||
String mediaName,
|
||||
ContentType contentType) {
|
||||
ContentType contentType,
|
||||
MediaType mediaType) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.uploadmedia_url"))
|
||||
.add("urlVersion", urlVersion)
|
||||
@ -702,7 +705,7 @@ public final class WebWeixinApiUtil {
|
||||
uploadmediarequest.put("TotalLen", mediaLength);
|
||||
uploadmediarequest.put("StartPos", 0);
|
||||
uploadmediarequest.put("DataLen", mediaLength);
|
||||
uploadmediarequest.put("MediaType", 4);
|
||||
uploadmediarequest.put(MediaType.REQUEST_JSON_KEY, mediaType.getCode());
|
||||
uploadmediarequest.put("FromUserName", fromUserName);
|
||||
uploadmediarequest.put("ToUserName", toUserName);
|
||||
uploadmediarequest.put("FileMd5", DigestUtils.md5Hex(mediaData));
|
||||
@ -721,6 +724,7 @@ public final class WebWeixinApiUtil {
|
||||
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
|
||||
.addTextBody("chunks", String.valueOf(chunks))
|
||||
.addTextBody("chunk", String.valueOf(chunk))
|
||||
.addTextBody(MediaType.REQUEST_KEY, mediaType.getValue(), ContentType.TEXT_PLAIN)
|
||||
.addTextBody("uploadmediarequest", uploadmediarequest.toJSONString(), ContentType.TEXT_PLAIN)
|
||||
.addTextBody("webwx_data_ticket", dataTicket, ContentType.TEXT_PLAIN)
|
||||
.addTextBody("pass_ticket", passticket, ContentType.TEXT_PLAIN)
|
||||
@ -805,4 +809,48 @@ public final class WebWeixinApiUtil {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送视频消息
|
||||
*
|
||||
* @param httpClient http客户端
|
||||
* @param urlVersion url版本号
|
||||
* @param baseRequest BaseRequest
|
||||
* @param message 消息
|
||||
* @return 返回数据
|
||||
*/
|
||||
public static JSONObject sendVideoMsg(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
BaseRequest baseRequest,
|
||||
MediaMessage message) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.webwxsendvideomsg_url"))
|
||||
.add("urlVersion", urlVersion)
|
||||
.render();
|
||||
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
httpPost.setHeader("Content-type", ContentType.APPLICATION_JSON.toString());
|
||||
|
||||
JSONObject paramJson = new JSONObject();
|
||||
paramJson.put("BaseRequest", baseRequest);
|
||||
paramJson.put("Msg", message);
|
||||
paramJson.put("Scene", 0);
|
||||
HttpEntity paramEntity = new StringEntity(paramJson.toJSONString(), Consts.UTF_8);
|
||||
httpPost.setEntity(paramEntity);
|
||||
|
||||
HttpResponse response = httpClient.execute(httpPost);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (HttpStatus.SC_OK != statusCode) {
|
||||
throw new RuntimeException("响应失败(" + statusCode + ")");
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
String res = EntityUtils.toString(entity, Consts.UTF_8);
|
||||
|
||||
return JSONObject.parseObject(res);
|
||||
} catch (Exception e) {
|
||||
log.error("发送视频消息异常", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -32,3 +32,5 @@ webwx-url.webwxsendmsg_url=https://wx<urlVersion>.qq.com/cgi-bin/mmwebwx-bin/web
|
||||
webwx-url.uploadmedia_url=https://file.wx<urlVersion>.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json
|
||||
## 4.4、发送图片消息
|
||||
webwx-url.webwxsendmsgimg_url=https://wx<urlVersion>.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsgimg?fun=async&f=json&lang=zh_CN&pass_ticket=<pass_ticket>
|
||||
## 4.5、发送视频消息
|
||||
webwx-url.webwxsendvideomsg_url=https://wx<urlVersion>.qq.com/cgi-bin/mmwebwx-bin/webwxsendvideomsg?fun=async&f=json
|
@ -3,6 +3,7 @@ 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;
|
||||
@ -34,9 +35,13 @@ public class TestClass {
|
||||
|
||||
@Test
|
||||
public void testSendImage() {
|
||||
File file = new File("D:\\Downloads\\images\\6600e90b8b0ce2037a5291a7147ffd2b.jpeg");
|
||||
|
||||
JSONObject result = wechat.sendImage(null, file);
|
||||
File file = null;
|
||||
JSONObject result = null;
|
||||
file = new File("C:\\Users\\Administrator\\Pictures\\壁纸\\9e5f4981099bcf351e0ec18c3654aced.jpg");
|
||||
result = wechat.sendImage(null, file);
|
||||
file = new File("C:\\Users\\Administrator\\Videos\\手机QQ视频_20190416170016.mp4");
|
||||
result = wechat.sendVideo(null, file);
|
||||
System.out.println(result);
|
||||
while (true) CommonUtil.threadSleep(5000);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user