2
0
mirror of https://gitee.com/hotlcc/wechat4j.git synced 2025-07-27 19:19:32 +08:00

Compare commits

..

No commits in common. "master" and "v0.2.4" have entirely different histories.

10 changed files with 68 additions and 178 deletions

23
LICENSE
View File

@ -1,23 +0,0 @@
版权所有c<年份><版权持有人>
反996许可证版本1.0
在符合下列条件的情况下,特此免费向任何得到本授权作品的副本(包括源代码、文件和/或相关内容,以
下统称为“授权作品”)的个人和法人实体授权:被授权个人或法人实体有权以任何目的处置授权作品,包括
但不限于使用、复制,修改,衍生利用、散布,发布和再许可:
1. 个人或法人实体必须在许可作品的每个再散布或衍生副本上包含以上版权声明和本许可证,不得自行修
改。
2. 个人或法人实体必须严格遵守与个人实际所在地或个人出生地或归化地、或法人实体注册地或经营地(
以较严格者为准)的司法管辖区所有适用的与劳动和就业相关法律、法规、规则和标准。如果该司法管辖区
没有此类法律、法规、规章和标准或其法律、法规、规章和标准不可执行,则个人或法人实体必须遵守国际
劳工标准的核心公约。
3. 个人或法人不得以任何方式诱导、暗示或强迫其全职或兼职员工或其独立承包人以口头或书面形式同意
直接或间接限制、削弱或放弃其所拥有的,受相关与劳动和就业有关的法律、法规、规则和标准保护的权利
或补救措施,无论该等书面或口头协议是否被该司法管辖区的法律所承认,该等个人或法人实体也不得以任
何方法限制其雇员或独立承包人向版权持有人或监督许可证合规情况的有关当局报告或投诉上述违反许可证
的行为的权利。
该授权作品是"按原样"提供,不做任何明示或暗示的保证,包括但不限于对适销性、特定用途适用性和非侵
权性的保证。在任何情况下,无论是在合同诉讼、侵权诉讼或其他诉讼中,版权持有人均不承担因本软件或
本软件的使用或其他交易而产生、引起或与之相关的任何索赔、损害或其他责任。

View File

@ -29,7 +29,7 @@ wechat.autoLogin();
```java
// 通过userName发送文本消息
JSONObject sendTextToUserName(String content, String userName);
JSONObject sendText(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 sendImageToUserName(String userName, byte[] mediaData, String mediaName, ContentType contentType);
JSONObject sendImageToUserName(String userName, File image);
JSONObject sendImage(String userName, byte[] mediaData, String mediaName, ContentType contentType);
JSONObject sendImage(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 sendVideoToUserName(String userName, byte[] mediaData, String mediaName, ContentType contentType);
JSONObject sendVideoToUserName(String userName, File video);
JSONObject sendVideo(String userName, byte[] mediaData, String mediaName, ContentType contentType);
JSONObject sendVideo(String userName, File video);
// 通过昵称发送视频消息
JSONObject sendVideoToNickName(String nickName, byte[] mediaData, String mediaName, ContentType contentType);
JSONObject sendVideoToNickName(String nickName, File video);

View File

@ -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视频为video其它文件为doc必须|
|mediatype|pic|文件类型图片为pic其它文件为doc必须;|
|uploadmediarequest||JSON字符串必须|
|webwx_data_ticket||cookie中的数据必须|
|pass_ticket||登录时获取的;必须;|

View File

@ -6,7 +6,7 @@
<groupId>com.hotlcc</groupId>
<artifactId>wechat4j</artifactId>
<version>0.2.5</version>
<version>0.2.4</version>
<name>wechat4j</name>
<description>Wechat client for Java.</description>

View File

@ -1129,45 +1129,6 @@ 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());
}
}
/**
* 发送文本消息
*
@ -1175,7 +1136,7 @@ public class Wechat {
* @param content 文本消息内容
* @return 返回数据
*/
public JSONObject sendTextToUserName(String userName, String content) {
public JSONObject sendText(String userName, String content) {
BaseRequest baseRequest = new BaseRequest(wxsid, skey, wxuin);
String msgId = WechatUtil.createMsgId();
@ -1204,7 +1165,7 @@ public class Wechat {
*/
public JSONObject sendTextToNickName(String nickName, String content) {
if (StringUtil.isEmpty(nickName)) {
return sendTextToUserName(null, content);
return sendText(null, content);
}
UserInfo userInfo = getContactByNickName(false, nickName);
@ -1217,7 +1178,7 @@ public class Wechat {
return null;
}
return sendTextToUserName(userName, content);
return sendText(userName, content);
}
/**
@ -1229,7 +1190,7 @@ public class Wechat {
*/
public JSONObject sendTextToRemarkName(String remarkName, String content) {
if (StringUtil.isEmpty(remarkName)) {
return sendTextToUserName(null, content);
return sendText(null, content);
}
UserInfo userInfo = getContactByRemarkName(false, remarkName);
@ -1242,7 +1203,7 @@ public class Wechat {
return null;
}
return sendTextToUserName(userName, content);
return sendText(userName, content);
}
/**
@ -1258,14 +1219,14 @@ public class Wechat {
UserInfo userInfo;
if (StringUtil.isNotEmpty(userName)) {
return sendTextToUserName(userName, content);
return sendText(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 sendTextToUserName(loginUserName, content);
return sendText(loginUserName, content);
}
if (userInfo == null) {
@ -1275,7 +1236,7 @@ public class Wechat {
if (StringUtil.isEmpty(userName)) {
return null;
}
return sendTextToUserName(userName, content);
return sendText(userName, content);
}
/**
@ -1287,7 +1248,7 @@ public class Wechat {
* @param contentType 媒体文件类型
* @return 返回数据
*/
public JSONObject sendImageToUserName(String userName, byte[] mediaData, String mediaName, ContentType contentType) {
public JSONObject sendImage(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);
@ -1334,10 +1295,10 @@ public class Wechat {
* @param image 图片文件
* @return 返回数据
*/
public JSONObject sendImageToUserName(String userName, File image) {
ContentType contentType = FileUtil.getContentType(image);
public JSONObject sendImage(String userName, File image) {
ContentType contentType = FileUtil.getContentBody(image);
byte[] mediaData = FileUtil.getBytes(image);
return sendImageToUserName(userName, mediaData, image.getName(), contentType);
return sendImage(userName, mediaData, image.getName(), contentType);
}
/**
@ -1351,7 +1312,7 @@ public class Wechat {
*/
public JSONObject sendImageToNickName(String nickName, byte[] mediaData, String mediaName, ContentType contentType) {
if (StringUtil.isEmpty(nickName)) {
return sendImageToUserName(null, mediaData, mediaName, contentType);
return sendImage(null, mediaData, mediaName, contentType);
}
UserInfo userInfo = getContactByNickName(false, nickName);
@ -1364,7 +1325,7 @@ public class Wechat {
return null;
}
return sendImageToUserName(userName, mediaData, mediaName, contentType);
return sendImage(userName, mediaData, mediaName, contentType);
}
/**
@ -1375,7 +1336,7 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendImageToNickName(String nickName, File image) {
ContentType contentType = FileUtil.getContentType(image);
ContentType contentType = FileUtil.getContentBody(image);
byte[] mediaData = FileUtil.getBytes(image);
return sendImageToNickName(nickName, mediaData, image.getName(), contentType);
}
@ -1391,7 +1352,7 @@ public class Wechat {
*/
public JSONObject sendImageToRemarkName(String remarkName, byte[] mediaData, String mediaName, ContentType contentType) {
if (StringUtil.isEmpty(remarkName)) {
return sendImageToUserName(null, mediaData, mediaName, contentType);
return sendImage(null, mediaData, mediaName, contentType);
}
UserInfo userInfo = getContactByRemarkName(false, remarkName);
@ -1404,7 +1365,7 @@ public class Wechat {
return null;
}
return sendImageToUserName(userName, mediaData, mediaName, contentType);
return sendImage(userName, mediaData, mediaName, contentType);
}
/**
@ -1415,7 +1376,7 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendImageToRemarkName(String remarkName, File image) {
ContentType contentType = FileUtil.getContentType(image);
ContentType contentType = FileUtil.getContentBody(image);
byte[] mediaData = FileUtil.getBytes(image);
return sendImageToRemarkName(remarkName, mediaData, image.getName(), contentType);
}
@ -1431,18 +1392,19 @@ 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 sendImageToUserName(userName, mediaData, mediaName, contentType);
return sendImage(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 sendImageToUserName(loginUserName, mediaData, mediaName, contentType);
return sendImage(loginUserName, mediaData, mediaName, contentType);
}
if (userInfo == null) {
@ -1452,7 +1414,7 @@ public class Wechat {
if (StringUtil.isEmpty(userName)) {
return null;
}
return sendImageToUserName(userName, mediaData, mediaName, contentType);
return sendImage(userName, mediaData, mediaName, contentType);
}
/**
@ -1465,7 +1427,7 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendImage(String userName, String nickName, String remarkName, File image) {
ContentType contentType = FileUtil.getContentType(image);
ContentType contentType = FileUtil.getContentBody(image);
byte[] mediaData = FileUtil.getBytes(image);
return sendImage(userName, nickName, remarkName, mediaData, image.getName(), contentType);
}
@ -1479,7 +1441,7 @@ public class Wechat {
* @param contentType 媒体文件类型
* @return 返回数据
*/
public JSONObject sendVideoToUserName(String userName, byte[] mediaData, String mediaName, ContentType contentType) {
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);
@ -1526,10 +1488,10 @@ public class Wechat {
* @param video 视频文件
* @return 返回数据
*/
public JSONObject sendVideoToUserName(String userName, File video) {
ContentType contentType = FileUtil.getContentType(video);
public JSONObject sendVideo(String userName, File video) {
ContentType contentType = FileUtil.getContentBody(video);
byte[] mediaData = FileUtil.getBytes(video);
return sendVideoToUserName(userName, mediaData, video.getName(), contentType);
return sendVideo(userName, mediaData, video.getName(), contentType);
}
/**
@ -1543,7 +1505,7 @@ public class Wechat {
*/
public JSONObject sendVideoToNickName(String nickName, byte[] mediaData, String mediaName, ContentType contentType) {
if (StringUtil.isEmpty(nickName)) {
return sendVideoToUserName(null, mediaData, mediaName, contentType);
return sendVideo(null, mediaData, mediaName, contentType);
}
UserInfo userInfo = getContactByNickName(false, nickName);
@ -1556,7 +1518,7 @@ public class Wechat {
return null;
}
return sendVideoToUserName(userName, mediaData, mediaName, contentType);
return sendVideo(userName, mediaData, mediaName, contentType);
}
/**
@ -1567,7 +1529,7 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendVideoToNickName(String nickName, File video) {
ContentType contentType = FileUtil.getContentType(video);
ContentType contentType = FileUtil.getContentBody(video);
byte[] mediaData = FileUtil.getBytes(video);
return sendVideoToNickName(nickName, mediaData, video.getName(), contentType);
}
@ -1583,7 +1545,7 @@ public class Wechat {
*/
public JSONObject sendVideoToRemarkName(String remarkName, byte[] mediaData, String mediaName, ContentType contentType) {
if (StringUtil.isEmpty(remarkName)) {
return sendVideoToUserName(null, mediaData, mediaName, contentType);
return sendVideo(null, mediaData, mediaName, contentType);
}
UserInfo userInfo = getContactByRemarkName(false, remarkName);
@ -1596,7 +1558,7 @@ public class Wechat {
return null;
}
return sendVideoToUserName(userName, mediaData, mediaName, contentType);
return sendVideo(userName, mediaData, mediaName, contentType);
}
/**
@ -1607,7 +1569,7 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendVideoToRemarkName(String remarkName, File video) {
ContentType contentType = FileUtil.getContentType(video);
ContentType contentType = FileUtil.getContentBody(video);
byte[] mediaData = FileUtil.getBytes(video);
return sendVideoToRemarkName(remarkName, mediaData, video.getName(), contentType);
}
@ -1627,14 +1589,14 @@ public class Wechat {
UserInfo userInfo;
if (StringUtil.isNotEmpty(userName)) {
return sendVideoToUserName(userName, mediaData, mediaName, contentType);
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 sendVideoToUserName(loginUserName, mediaData, mediaName, contentType);
return sendVideo(loginUserName, mediaData, mediaName, contentType);
}
if (userInfo == null) {
@ -1644,7 +1606,7 @@ public class Wechat {
if (StringUtil.isEmpty(userName)) {
return null;
}
return sendVideoToUserName(userName, mediaData, mediaName, contentType);
return sendVideo(userName, mediaData, mediaName, contentType);
}
/**
@ -1657,7 +1619,7 @@ public class Wechat {
* @return 返回数据
*/
public JSONObject sendVideo(String userName, String nickName, String remarkName, File video) {
ContentType contentType = FileUtil.getContentType(video);
ContentType contentType = FileUtil.getContentBody(video);
byte[] mediaData = FileUtil.getBytes(video);
return sendVideo(userName, nickName, remarkName, mediaData, video.getName(), contentType);
}

View File

@ -74,7 +74,7 @@ public final class FileUtil {
* @param file 文件
* @return ContentType
*/
public static ContentType getContentType(File file) {
public static ContentType getContentBody(File file) {
String mimeType = new MimetypesFileTypeMap().getContentType(file);
ContentType contentType = ContentType.parse(mimeType);
return contentType;

View File

@ -565,42 +565,6 @@ 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;
}
}
/**
* 从服务端同步新数据
*

View File

@ -56,4 +56,21 @@ 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";
}
}
}

View File

@ -1,6 +1,3 @@
# 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&_=<_>
@ -26,8 +23,6 @@ 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>

View File

@ -9,9 +9,6 @@ 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;
@ -32,41 +29,19 @@ public class TestClass {
}
public void testSendText() {
JSONObject result = wechat.sendTextToUserName(null, "这是消息内容");
JSONObject result = wechat.sendText(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.sendImageToUserName(null, file);
result = wechat.sendImage(null, file);
file = new File("C:\\Users\\Administrator\\Videos\\手机QQ视频_20190416170016.mp4");
result = wechat.sendVideoToUserName(null, file);
result = wechat.sendVideo(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();
}
}
}
}
}