mirror of
https://gitee.com/hotlcc/wechat4j.git
synced 2025-07-27 02:59:35 +08:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
443b35fac0 | ||
|
56791e7965 | ||
|
fcd0316320 | ||
|
3749cdbc21 | ||
|
7de2bae4cc | ||
|
166db64b01 | ||
|
81ab8dd81c |
23
LICENSE
Normal file
23
LICENSE
Normal file
@ -0,0 +1,23 @@
|
||||
版权所有(c)<年份><版权持有人>
|
||||
|
||||
反996许可证版本1.0
|
||||
|
||||
在符合下列条件的情况下,特此免费向任何得到本授权作品的副本(包括源代码、文件和/或相关内容,以
|
||||
下统称为“授权作品”)的个人和法人实体授权:被授权个人或法人实体有权以任何目的处置授权作品,包括
|
||||
但不限于使用、复制,修改,衍生利用、散布,发布和再许可:
|
||||
|
||||
1. 个人或法人实体必须在许可作品的每个再散布或衍生副本上包含以上版权声明和本许可证,不得自行修
|
||||
改。
|
||||
2. 个人或法人实体必须严格遵守与个人实际所在地或个人出生地或归化地、或法人实体注册地或经营地(
|
||||
以较严格者为准)的司法管辖区所有适用的与劳动和就业相关法律、法规、规则和标准。如果该司法管辖区
|
||||
没有此类法律、法规、规章和标准或其法律、法规、规章和标准不可执行,则个人或法人实体必须遵守国际
|
||||
劳工标准的核心公约。
|
||||
3. 个人或法人不得以任何方式诱导、暗示或强迫其全职或兼职员工或其独立承包人以口头或书面形式同意
|
||||
直接或间接限制、削弱或放弃其所拥有的,受相关与劳动和就业有关的法律、法规、规则和标准保护的权利
|
||||
或补救措施,无论该等书面或口头协议是否被该司法管辖区的法律所承认,该等个人或法人实体也不得以任
|
||||
何方法限制其雇员或独立承包人向版权持有人或监督许可证合规情况的有关当局报告或投诉上述违反许可证
|
||||
的行为的权利。
|
||||
|
||||
该授权作品是"按原样"提供,不做任何明示或暗示的保证,包括但不限于对适销性、特定用途适用性和非侵
|
||||
权性的保证。在任何情况下,无论是在合同诉讼、侵权诉讼或其他诉讼中,版权持有人均不承担因本软件或
|
||||
本软件的使用或其他交易而产生、引起或与之相关的任何索赔、损害或其他责任。
|
10
README.md
10
README.md
@ -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);
|
||||
|
@ -924,7 +924,7 @@ POST参数分析:
|
||||
- 10002:撤回消息
|
||||
- SyncKey会暂存起来作为下一次请求的参数。
|
||||
|
||||
### 4.2、发送消息
|
||||
### 4.2、发送文本消息
|
||||
|
||||
打开一个聊天界面,向好友发送一条消息,会发现产生如下 POST 请求:
|
||||
|
||||
@ -1010,7 +1010,7 @@ FORM表单参数分析:
|
||||
|~~size~~|99785|文件大小;非必须;|
|
||||
|chunks|3|文件分片数量;文件较大需要分片时必须;|
|
||||
|chunk|0|文件分片序号;从0开始;文件较大需要分片时必须;|
|
||||
|mediatype|pic|文件类型;图片为pic,其它文件为doc;非必须;|
|
||||
|mediatype|pic|文件类型;图片为pic,视频为video,其它文件为doc;必须;|
|
||||
|uploadmediarequest||JSON字符串;必须;|
|
||||
|webwx_data_ticket||cookie中的数据;必须;|
|
||||
|pass_ticket||登录时获取的;必须;|
|
||||
|
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.hotlcc</groupId>
|
||||
<artifactId>wechat4j</artifactId>
|
||||
<version>0.2.4</version>
|
||||
<version>0.2.5</version>
|
||||
|
||||
<name>wechat4j</name>
|
||||
<description>Wechat client for Java.</description>
|
||||
|
@ -1129,6 +1129,45 @@ public class Wechat {
|
||||
return getContactByRemarkName(false, remarkName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取联系人信息(根据多种名称)
|
||||
*
|
||||
* @param update 是否强制更新
|
||||
* @param userName 用户名(加密的)
|
||||
* @param nickName 昵称
|
||||
* @param remarkName 备注
|
||||
* @return 返回数据
|
||||
*/
|
||||
public UserInfo getContact(boolean update, String userName, String nickName, String remarkName) {
|
||||
if (StringUtil.isNotEmpty(userName)) {
|
||||
return getContactByUserName(update, userName);
|
||||
} else if (StringUtil.isNotEmpty(nickName)) {
|
||||
return getContactByNickName(update, nickName);
|
||||
} else if (StringUtil.isNotEmpty(remarkName)) {
|
||||
return getContactByRemarkName(update, remarkName);
|
||||
} else {
|
||||
return getLoginUser(update);
|
||||
}
|
||||
}
|
||||
|
||||
public UserInfo getContact(String userName, String nickName, String remarkName) {
|
||||
return getContact(false, userName, nickName, remarkName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据UserName获取联系人头像图片
|
||||
*
|
||||
* @param userName 用户名(加密的)
|
||||
* @return 返回数据
|
||||
*/
|
||||
public byte[] getContactHeadImgByUserName(String userName) {
|
||||
if (!StringUtil.isEmpty(userName)) {
|
||||
return WebWeixinApiUtil.getContactHeadImg(httpClient, urlVersion, userName);
|
||||
} else {
|
||||
return WebWeixinApiUtil.getContactHeadImg(httpClient, urlVersion, getLoginUserName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送文本消息
|
||||
*
|
||||
@ -1136,7 +1175,7 @@ public class Wechat {
|
||||
* @param content 文本消息内容
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendText(String userName, String content) {
|
||||
public JSONObject sendTextToUserName(String userName, String content) {
|
||||
BaseRequest baseRequest = new BaseRequest(wxsid, skey, wxuin);
|
||||
|
||||
String msgId = WechatUtil.createMsgId();
|
||||
@ -1165,7 +1204,7 @@ public class Wechat {
|
||||
*/
|
||||
public JSONObject sendTextToNickName(String nickName, String content) {
|
||||
if (StringUtil.isEmpty(nickName)) {
|
||||
return sendText(null, content);
|
||||
return sendTextToUserName(null, content);
|
||||
}
|
||||
|
||||
UserInfo userInfo = getContactByNickName(false, nickName);
|
||||
@ -1178,7 +1217,7 @@ public class Wechat {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sendText(userName, content);
|
||||
return sendTextToUserName(userName, content);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1190,7 +1229,7 @@ public class Wechat {
|
||||
*/
|
||||
public JSONObject sendTextToRemarkName(String remarkName, String content) {
|
||||
if (StringUtil.isEmpty(remarkName)) {
|
||||
return sendText(null, content);
|
||||
return sendTextToUserName(null, content);
|
||||
}
|
||||
|
||||
UserInfo userInfo = getContactByRemarkName(false, remarkName);
|
||||
@ -1203,7 +1242,7 @@ public class Wechat {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sendText(userName, content);
|
||||
return sendTextToUserName(userName, content);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1219,14 +1258,14 @@ public class Wechat {
|
||||
UserInfo userInfo;
|
||||
|
||||
if (StringUtil.isNotEmpty(userName)) {
|
||||
return sendText(userName, content);
|
||||
return sendTextToUserName(userName, content);
|
||||
} else if (StringUtil.isNotEmpty(nickName)) {
|
||||
userInfo = getContactByNickName(false, nickName);
|
||||
} else if (StringUtil.isNotEmpty(remarkName)) {
|
||||
userInfo = getContactByRemarkName(false, remarkName);
|
||||
} else {
|
||||
String loginUserName = getLoginUserName(false);
|
||||
return sendText(loginUserName, content);
|
||||
return sendTextToUserName(loginUserName, content);
|
||||
}
|
||||
|
||||
if (userInfo == null) {
|
||||
@ -1236,7 +1275,7 @@ public class Wechat {
|
||||
if (StringUtil.isEmpty(userName)) {
|
||||
return null;
|
||||
}
|
||||
return sendText(userName, content);
|
||||
return sendTextToUserName(userName, content);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1248,7 +1287,7 @@ public class Wechat {
|
||||
* @param contentType 媒体文件类型
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendImage(String userName, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
public JSONObject sendImageToUserName(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);
|
||||
@ -1295,10 +1334,10 @@ public class Wechat {
|
||||
* @param image 图片文件
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendImage(String userName, File image) {
|
||||
ContentType contentType = FileUtil.getContentBody(image);
|
||||
public JSONObject sendImageToUserName(String userName, File image) {
|
||||
ContentType contentType = FileUtil.getContentType(image);
|
||||
byte[] mediaData = FileUtil.getBytes(image);
|
||||
return sendImage(userName, mediaData, image.getName(), contentType);
|
||||
return sendImageToUserName(userName, mediaData, image.getName(), contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1312,7 +1351,7 @@ public class Wechat {
|
||||
*/
|
||||
public JSONObject sendImageToNickName(String nickName, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
if (StringUtil.isEmpty(nickName)) {
|
||||
return sendImage(null, mediaData, mediaName, contentType);
|
||||
return sendImageToUserName(null, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
UserInfo userInfo = getContactByNickName(false, nickName);
|
||||
@ -1325,7 +1364,7 @@ public class Wechat {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sendImage(userName, mediaData, mediaName, contentType);
|
||||
return sendImageToUserName(userName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1336,7 +1375,7 @@ public class Wechat {
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendImageToNickName(String nickName, File image) {
|
||||
ContentType contentType = FileUtil.getContentBody(image);
|
||||
ContentType contentType = FileUtil.getContentType(image);
|
||||
byte[] mediaData = FileUtil.getBytes(image);
|
||||
return sendImageToNickName(nickName, mediaData, image.getName(), contentType);
|
||||
}
|
||||
@ -1352,7 +1391,7 @@ public class Wechat {
|
||||
*/
|
||||
public JSONObject sendImageToRemarkName(String remarkName, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
if (StringUtil.isEmpty(remarkName)) {
|
||||
return sendImage(null, mediaData, mediaName, contentType);
|
||||
return sendImageToUserName(null, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
UserInfo userInfo = getContactByRemarkName(false, remarkName);
|
||||
@ -1365,7 +1404,7 @@ public class Wechat {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sendImage(userName, mediaData, mediaName, contentType);
|
||||
return sendImageToUserName(userName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1376,7 +1415,7 @@ public class Wechat {
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendImageToRemarkName(String remarkName, File image) {
|
||||
ContentType contentType = FileUtil.getContentBody(image);
|
||||
ContentType contentType = FileUtil.getContentType(image);
|
||||
byte[] mediaData = FileUtil.getBytes(image);
|
||||
return sendImageToRemarkName(remarkName, mediaData, image.getName(), contentType);
|
||||
}
|
||||
@ -1392,19 +1431,18 @@ public class Wechat {
|
||||
* @param contentType 媒体文件类型
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendImage(String userName, String nickName, String remarkName
|
||||
, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
public JSONObject sendImage(String userName, String nickName, String remarkName, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
UserInfo userInfo;
|
||||
|
||||
if (StringUtil.isNotEmpty(userName)) {
|
||||
return sendImage(userName, mediaData, mediaName, contentType);
|
||||
return sendImageToUserName(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 sendImage(loginUserName, mediaData, mediaName, contentType);
|
||||
return sendImageToUserName(loginUserName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
if (userInfo == null) {
|
||||
@ -1414,7 +1452,7 @@ public class Wechat {
|
||||
if (StringUtil.isEmpty(userName)) {
|
||||
return null;
|
||||
}
|
||||
return sendImage(userName, mediaData, mediaName, contentType);
|
||||
return sendImageToUserName(userName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1427,7 +1465,7 @@ public class Wechat {
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendImage(String userName, String nickName, String remarkName, File image) {
|
||||
ContentType contentType = FileUtil.getContentBody(image);
|
||||
ContentType contentType = FileUtil.getContentType(image);
|
||||
byte[] mediaData = FileUtil.getBytes(image);
|
||||
return sendImage(userName, nickName, remarkName, mediaData, image.getName(), contentType);
|
||||
}
|
||||
@ -1441,7 +1479,7 @@ public class Wechat {
|
||||
* @param contentType 媒体文件类型
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideo(String userName, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
public JSONObject sendVideoToUserName(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);
|
||||
@ -1488,10 +1526,10 @@ public class Wechat {
|
||||
* @param video 视频文件
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideo(String userName, File video) {
|
||||
ContentType contentType = FileUtil.getContentBody(video);
|
||||
public JSONObject sendVideoToUserName(String userName, File video) {
|
||||
ContentType contentType = FileUtil.getContentType(video);
|
||||
byte[] mediaData = FileUtil.getBytes(video);
|
||||
return sendVideo(userName, mediaData, video.getName(), contentType);
|
||||
return sendVideoToUserName(userName, mediaData, video.getName(), contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1505,7 +1543,7 @@ public class Wechat {
|
||||
*/
|
||||
public JSONObject sendVideoToNickName(String nickName, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
if (StringUtil.isEmpty(nickName)) {
|
||||
return sendVideo(null, mediaData, mediaName, contentType);
|
||||
return sendVideoToUserName(null, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
UserInfo userInfo = getContactByNickName(false, nickName);
|
||||
@ -1518,7 +1556,7 @@ public class Wechat {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sendVideo(userName, mediaData, mediaName, contentType);
|
||||
return sendVideoToUserName(userName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1529,7 +1567,7 @@ public class Wechat {
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideoToNickName(String nickName, File video) {
|
||||
ContentType contentType = FileUtil.getContentBody(video);
|
||||
ContentType contentType = FileUtil.getContentType(video);
|
||||
byte[] mediaData = FileUtil.getBytes(video);
|
||||
return sendVideoToNickName(nickName, mediaData, video.getName(), contentType);
|
||||
}
|
||||
@ -1545,7 +1583,7 @@ public class Wechat {
|
||||
*/
|
||||
public JSONObject sendVideoToRemarkName(String remarkName, byte[] mediaData, String mediaName, ContentType contentType) {
|
||||
if (StringUtil.isEmpty(remarkName)) {
|
||||
return sendVideo(null, mediaData, mediaName, contentType);
|
||||
return sendVideoToUserName(null, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
UserInfo userInfo = getContactByRemarkName(false, remarkName);
|
||||
@ -1558,7 +1596,7 @@ public class Wechat {
|
||||
return null;
|
||||
}
|
||||
|
||||
return sendVideo(userName, mediaData, mediaName, contentType);
|
||||
return sendVideoToUserName(userName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1569,7 +1607,7 @@ public class Wechat {
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideoToRemarkName(String remarkName, File video) {
|
||||
ContentType contentType = FileUtil.getContentBody(video);
|
||||
ContentType contentType = FileUtil.getContentType(video);
|
||||
byte[] mediaData = FileUtil.getBytes(video);
|
||||
return sendVideoToRemarkName(remarkName, mediaData, video.getName(), contentType);
|
||||
}
|
||||
@ -1589,14 +1627,14 @@ public class Wechat {
|
||||
UserInfo userInfo;
|
||||
|
||||
if (StringUtil.isNotEmpty(userName)) {
|
||||
return sendVideo(userName, mediaData, mediaName, contentType);
|
||||
return sendVideoToUserName(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);
|
||||
return sendVideoToUserName(loginUserName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
if (userInfo == null) {
|
||||
@ -1606,7 +1644,7 @@ public class Wechat {
|
||||
if (StringUtil.isEmpty(userName)) {
|
||||
return null;
|
||||
}
|
||||
return sendVideo(userName, mediaData, mediaName, contentType);
|
||||
return sendVideoToUserName(userName, mediaData, mediaName, contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1619,7 +1657,7 @@ public class Wechat {
|
||||
* @return 返回数据
|
||||
*/
|
||||
public JSONObject sendVideo(String userName, String nickName, String remarkName, File video) {
|
||||
ContentType contentType = FileUtil.getContentBody(video);
|
||||
ContentType contentType = FileUtil.getContentType(video);
|
||||
byte[] mediaData = FileUtil.getBytes(video);
|
||||
return sendVideo(userName, nickName, remarkName, mediaData, video.getName(), contentType);
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ public final class FileUtil {
|
||||
* @param file 文件
|
||||
* @return ContentType
|
||||
*/
|
||||
public static ContentType getContentBody(File file) {
|
||||
public static ContentType getContentType(File file) {
|
||||
String mimeType = new MimetypesFileTypeMap().getContentType(file);
|
||||
ContentType contentType = ContentType.parse(mimeType);
|
||||
return contentType;
|
||||
|
@ -565,6 +565,42 @@ public final class WebWeixinApiUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取联系人头像
|
||||
*
|
||||
* @param httpClient http客户端
|
||||
* @param urlVersion url版本号
|
||||
* @param username 用户名
|
||||
* @return 头像图片数据
|
||||
*/
|
||||
public static byte[] getContactHeadImg(HttpClient httpClient,
|
||||
String urlVersion,
|
||||
String username) {
|
||||
try {
|
||||
String url = new ST(PropertiesUtil.getProperty("webwx-url.webwxgetheadimg_url"))
|
||||
.add("urlVersion", urlVersion)
|
||||
.add("seq", System.currentTimeMillis())
|
||||
.add("username", username)
|
||||
.render();
|
||||
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
|
||||
HttpResponse response = httpClient.execute(httpGet);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (HttpStatus.SC_OK != statusCode) {
|
||||
throw new RuntimeException("响应失败(" + statusCode + ")");
|
||||
}
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
byte[] headImgData = EntityUtils.toByteArray(entity);
|
||||
|
||||
return headImgData;
|
||||
} catch (Exception e) {
|
||||
log.error("获取联系人头像图片异常", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从服务端同步新数据
|
||||
*
|
||||
|
@ -56,21 +56,4 @@ public final class WechatUtil {
|
||||
}
|
||||
return synckey.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ContentType得到微信上传所需的mediatype
|
||||
*
|
||||
* @param contentType contentType
|
||||
* @return 微信上传所需的mediatype
|
||||
*/
|
||||
public static String getMediatype(String contentType) {
|
||||
if (contentType == null) {
|
||||
return "doc";
|
||||
}
|
||||
if (contentType.indexOf("image") >= 0) {
|
||||
return "pic";
|
||||
} else {
|
||||
return "doc";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,6 @@
|
||||
# 0、全局
|
||||
## 0.1、Web微信host
|
||||
webwx-url.webwxhost_url=https://wx<urlVersion>.qq.com
|
||||
# 1、登录
|
||||
## 1.1、获取微信uuid
|
||||
webwx-url.uuid_url=https://login.wx.qq.com/jslogin?appid=<appid>&fun=new&lang=zh_CN&_=<_>
|
||||
@ -23,6 +26,8 @@ webwx-url.synccheck_url=https://webpush.wx<urlVersion>.qq.com/cgi-bin/mmwebwx-bi
|
||||
webwx-url.getcontact_url=https://wx<urlVersion>.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?lang=zh_CN&pass_ticket=<pass_ticket>&r=<r>&seq=0&skey=<skey>
|
||||
## 3.2、批量获取指定联系人列表
|
||||
webwx-url.batchgetcontact_url=https://wx<urlVersion>.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact?type=ex&r=<r>&lang=zh_CN&pass_ticket=<pass_ticket>
|
||||
## 3.3、获取联系人头像
|
||||
webwx-url.webwxgetheadimg_url=https://wx<urlVersion>.qq.com/cgi-bin/mmwebwx-bin/webwxgetheadimg?seq=<seq>&username=<username>
|
||||
# 4、收发消息
|
||||
## 4.1、从服务端拉取新消息
|
||||
webwx-url.webwxsync_url=https://wx<urlVersion>.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=<sid>&skey=<skey>&pass_ticket=<pass_ticket>
|
||||
|
@ -9,6 +9,9 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class TestClass {
|
||||
private Wechat wechat;
|
||||
@ -29,19 +32,41 @@ public class TestClass {
|
||||
}
|
||||
|
||||
public void testSendText() {
|
||||
JSONObject result = wechat.sendText(null, "这是消息内容");
|
||||
JSONObject result = wechat.sendTextToUserName(null, "这是消息内容");
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendImage() {
|
||||
File file = null;
|
||||
JSONObject result = null;
|
||||
file = new File("C:\\Users\\Administrator\\Pictures\\壁纸\\9e5f4981099bcf351e0ec18c3654aced.jpg");
|
||||
result = wechat.sendImage(null, file);
|
||||
result = wechat.sendImageToUserName(null, file);
|
||||
file = new File("C:\\Users\\Administrator\\Videos\\手机QQ视频_20190416170016.mp4");
|
||||
result = wechat.sendVideo(null, file);
|
||||
result = wechat.sendVideoToUserName(null, file);
|
||||
System.out.println(result);
|
||||
while (true) CommonUtil.threadSleep(5000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetImageData() {
|
||||
byte[] data = wechat.getContactHeadImgByUserName(null);
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream("D:\\a.jpg");
|
||||
fos.write(data);
|
||||
fos.flush();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user