2
0
mirror of https://gitee.com/hotlcc/wechat4j.git synced 2025-06-07 02:54:07 +08:00

[update] 1.调整了发送消息相关方法的名称;2.新增获取联系人头像数据的方法;

This commit is contained in:
Allen 2019-04-22 11:22:58 +08:00 committed by lichangchun
parent 166db64b01
commit 7de2bae4cc
6 changed files with 139 additions and 52 deletions

View File

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

View File

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

View File

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

View File

@ -56,21 +56,4 @@ public final class WechatUtil {
} }
return synckey.toString(); 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,3 +1,6 @@
# 0、全局
## 0.1、Web微信host
webwx-url.webwxhost_url=https://wx<urlVersion>.qq.com
# 1、登录 # 1、登录
## 1.1、获取微信uuid ## 1.1、获取微信uuid
webwx-url.uuid_url=https://login.wx.qq.com/jslogin?appid=<appid>&fun=new&lang=zh_CN&_=<_> 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> 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、批量获取指定联系人列表 ## 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> 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、收发消息
## 4.1、从服务端拉取新消息 ## 4.1、从服务端拉取新消息
webwx-url.webwxsync_url=https://wx<urlVersion>.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=<sid>&skey=<skey>&pass_ticket=<pass_ticket> 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,6 +9,9 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestClass { public class TestClass {
private Wechat wechat; private Wechat wechat;
@ -29,19 +32,41 @@ public class TestClass {
} }
public void testSendText() { public void testSendText() {
JSONObject result = wechat.sendText(null, "这是消息内容"); JSONObject result = wechat.sendTextToUserName(null, "这是消息内容");
System.out.println(result); System.out.println(result);
} }
@Test
public void testSendImage() { public void testSendImage() {
File file = null; File file = null;
JSONObject result = null; JSONObject result = null;
file = new File("C:\\Users\\Administrator\\Pictures\\壁纸\\9e5f4981099bcf351e0ec18c3654aced.jpg"); 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"); file = new File("C:\\Users\\Administrator\\Videos\\手机QQ视频_20190416170016.mp4");
result = wechat.sendVideo(null, file); result = wechat.sendVideoToUserName(null, file);
System.out.println(result); System.out.println(result);
while (true) CommonUtil.threadSleep(5000); 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();
}
}
}
}
} }