Skip to content

Commit

Permalink
获取好友头像
Browse files Browse the repository at this point in the history
  • Loading branch information
yaphone committed Jun 26, 2017
1 parent 447d193 commit 25c18e4
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 21 deletions.
38 changes: 23 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

> itchat是一个开源的微信个人号接口,使用Python调用微信从未如此简单。使用短短的几十行代码,你就可以完成一个能够处理所有信息的微信机器人。当然,itchat的使用远不止一个机器人,更多的功能等着你来发现,如今微信已经成为了个人社交的很大一部分,希望这个项目能够帮助你扩展你的个人的微信号、方便自己的生活。(引自itchat项目)
你可以轻松将[itchat4j](https://github.com/yaphone/itchat4j)其集成在你个人的Java应用中,无论是SpringMVC、桌面程序还是嵌入式程序,只要使用的JDK是1.6以上的版本,都可以轻松接入。玩法很多,请打开你的脑洞,比如这些:
你可以轻松将[itchat4j](https://github.com/yaphone/itchat4j)其集成在你个人的Java应用中,无论是SpringMVC、桌面程序还是嵌入式程序,只要使用的JDK是1.7以上的版本,都可以轻松接入。玩法很多,请打开你的脑洞,比如这些:

- Just for fun,把个人微信号扩展为"公众号",在朋友面前装个X吧。
- 集成在你的个人应用(SpringMVC、Servlet、GUI)中,为应用提供更强的服务能力。
Expand Down Expand Up @@ -46,103 +46,111 @@

目前在`package cn.zhouyafeng.itchat4j.api`包中有两个静态类,即`MessageTools``WechatTools`,目前对外暴露的方法有:

#### 1.获取好友列表 WechatTools.getContactList()
#### 1.获取好友昵称列表 WechatTools.getContactNickNameList()

此方法会返回好友昵称列表,其函数声明为:

```
public static List<String> getContactList()
public static List<String> getContactNickNameList()
```

#### 2.获取群列表 WechatTools.getGroupIdList()
#### 2.获取好友完整信息列表 WechatTools.getContactList()

此方法会返回好友的完整信息,如昵称、备注、地区、头像链接等,其函数声明为:

```java
public static List<JSONObject> getContactList()
```

#### 3.获取群列表 WechatTools.getGroupIdList()

群列表与好友列表不同,在登陆后群列表其实是空的,只有主动发送消息或者收到一条群消息时,才能获取到这个群的信息,群列表会记录这个群的id,其格式为`@@d052d34b9c9228830363013ee53deb461404f80ea353dbdd8fc9391cbf5f1c46`。调用此方法会返回已知的群列表。其声明函数为:

```
public static List<String> getGroupIdList()
```

#### 3.根据群ID获取群成员WechatTools.getMemberListByGroupId()
#### 4.根据群ID获取群成员WechatTools.getMemberListByGroupId()

此方法根据群ID(格式为`@@d052d34b9c9228830363013ee53deb461404f80ea353dbdd8fc9391cbf5f1c46`)获取群成员列表。其函数声明为:

```java
public static JSONArray getMemberListByGroupId(String groupId)
```

#### 4.退出微信 WechatTools.logout()
#### 5.退出微信 WechatTools.logout()

退出itchat4j,不再处理消息,其函数声明为:

```
public static void logout()
```

#### 5.获取微信在线状态WechatTools.getWechatStatus()
#### 6.获取微信在线状态WechatTools.getWechatStatus()

查询微信在线状态,在线返回`true`,离线返回`false`,其函数声明为

```java
public static boolean getWechatStatus()
```

#### 6.获取群昵称列表WechatTools.getGroupNickNameList()
#### 7.获取群昵称列表WechatTools.getGroupNickNameList()

获取群昵称列表,函数声明为:

```java
public static List<String> getGroupNickNameList()
```

#### 7.根据用户昵称修改用户备注MessageTools.remarkNameByNickName(String nickName, String remName)
#### 8.根据用户昵称修改用户备注MessageTools.remarkNameByNickName(String nickName, String remName)

根据用户昵称修改用户备注名称,其函数声明为:

```
public static void remarkNameByNickName(String nickName, String remName)
```

#### 8. 根据好友昵称发送文本消息,MessageTools.sendMsgByNickName(String text, String nickName)
#### 9. 根据好友昵称发送文本消息,MessageTools.sendMsgByNickName(String text, String nickName)

此方法根据用户昵称发送文本消息,注意,用户需在你的好友列表里,否则发送失败,如果你的好友列表里有存在昵称一样的多个用户,则只会给第一个匹配的好友发送消息。方法接受两个参数,`text`为要发送的文本消息,`nickName`为要发送消息的好友昵称,成功发送时返回true,失败返回false。其函数声明为:

```
public static boolean sendMsgByNickName(String text, String nickName)
```

#### 9.根据ID发送文本消息, MessageTools.sendMsgById(String text, String id)
#### 10.根据ID发送文本消息, MessageTools.sendMsgById(String text, String id)

根据ID发送文本消息,发送者ID可以从`msg`里通过`msg.getString("FromUserName")`获取,格式为`@@d052d34b9c9228830363013ee53deb461404f80ea353dbdd8fc9391cbf5f1c46`(群消息)或`@a257b99314d8313862cd44ab02fe0f81`(非群消息),调用此方法可向指定id发送消息。其函数声明为:

```
public static void sendMsgById(String text, String id)
```

#### 10.根据好友昵称发送图片消息,MessageTools.sendPicMsgByNickName(String nickName, String filePath)
#### 11.根据好友昵称发送图片消息,MessageTools.sendPicMsgByNickName(String nickName, String filePath)

此方法根据好友昵称发送图片消息,`filePath`为图片文件路径,如`D:/itchat4j/pic/test.jpg`,成功返回true,失败返回false。其函数声明为:

```
public static boolean sendPicMsgByNickName(String nickName, String filePath)
```

#### 11.根据ID发送图片消息,MessageTools.sendPicMsgByUserId(String userId, String filePath)
#### 12.根据ID发送图片消息,MessageTools.sendPicMsgByUserId(String userId, String filePath)

此方法根据好友ID发送图片消息,filePath`为图片文件路径,如`D:/itchat4j/pic/test.jpg`,成功返回true,失败返回false。其函数声明为:

```
public static boolean sendPicMsgByUserId(String userId, String filePath)
```

#### 12.根据好友昵称发送文件消息,MessageTools.sendFileMsgByNickName(String nickName, String filePath)
#### 13.根据好友昵称发送文件消息,MessageTools.sendFileMsgByNickName(String nickName, String filePath)

此方法根据好友昵称发送文件消息,文件可以为多种类型,如txt、PDF、小视频、语音、excel、docx等,发送时请保证文件后缀名正确。成功返回true,失败返回false。其函数声明为:

```
public static boolean sendPicFileByNickName(String nickName, String filePath)
```

#### 13.根据ID发送文件消息,MessageTools.sendFileMsgByNickName(String nickName, String filePath)
#### 14.根据ID发送文件消息,MessageTools.sendFileMsgByNickName(String nickName, String filePath)

此方法根据好友昵称发送文件消息,成功返回true,失败返回false。其函数声明为:

Expand Down
18 changes: 14 additions & 4 deletions src/main/java/cn/zhouyafeng/itchat4j/api/WechatTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,22 @@ public static String getUserNameByNickName(String nickName) {
* @date 2017年5月4日 下午11:37:20
* @return
*/
public static List<String> getContactList() {
List<String> contactList = new ArrayList<String>();
public static List<String> getContactNickNameList() {
List<String> contactNickNameList = new ArrayList<String>();
for (JSONObject o : core.getContactList()) {
contactList.add(o.getString("NickName"));
contactNickNameList.add(o.getString("NickName"));
}
return contactList;
return contactNickNameList;
}

/**
* 返回好友完整信息列表
*
* @date 2017年6月26日 下午9:45:39
* @return
*/
public static List<JSONObject> getContactList() {
return core.getContactList();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cn/zhouyafeng/itchat4j/core/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public static Core getInstance() {
boolean alive = false;
private int memberCount = 0;

private String indexUrl;

private String userName;
private String nickName;
private List<JSONObject> msgList = new ArrayList<JSONObject>();
Expand Down Expand Up @@ -262,4 +264,12 @@ public void setGroupMemeberMap(Map<String, JSONArray> groupMemeberMap) {
this.groupMemeberMap = groupMemeberMap;
}

public String getIndexUrl() {
return indexUrl;
}

public void setIndexUrl(String indexUrl) {
this.indexUrl = indexUrl;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ private void processLoginInfo(String loginContent) {
fileUrl = "https://" + entry.getValue().get(0) + "/cgi-bin/mmwebwx-bin";
syncUrl = "https://" + entry.getValue().get(1) + "/cgi-bin/mmwebwx-bin";
if (core.getLoginInfo().get("url").toString().contains(indexUrl)) {
core.setIndexUrl(indexUrl);
core.getLoginInfo().put("fileUrl", fileUrl);
core.getLoginInfo().put("syncUrl", syncUrl);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cn.zhouyafeng.itchat4j.face.IMsgHandlerFace;

/**
*
*
* @author https://github.com/yaphone
* @date 创建时间:2017年4月28日 上午12:44:10
* @version 1.0
Expand Down
102 changes: 102 additions & 0 deletions src/test/java/cn/zhouyafeng/itchat4j/demo/demo3/PicYourFriends.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cn.zhouyafeng.itchat4j.demo.demo3;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import cn.zhouyafeng.itchat4j.Wechat;
import cn.zhouyafeng.itchat4j.api.WechatTools;
import cn.zhouyafeng.itchat4j.core.Core;
import cn.zhouyafeng.itchat4j.face.IMsgHandlerFace;
import cn.zhouyafeng.itchat4j.utils.MyHttpClient;
import cn.zhouyafeng.itchat4j.utils.enums.StorageLoginInfoEnum;

public class PicYourFriends implements IMsgHandlerFace {
private static Logger LOG = LoggerFactory.getLogger(PicYourFriends.class);
private static final Core core = Core.getInstance();
private static final MyHttpClient myHttpClient = core.getMyHttpClient();
private static final String path = "D://itchat4j//head";

@Override
public String textMsgHandle(JSONObject msg) {

if (!msg.getBoolean("groupMsg")) { // 群消息不处理
String text = msg.getString("Text"); // 发送文本消息,也可调用MessageTools.sendFileMsgByUserId(userId,text);
String baseUrl = "https://" + core.getIndexUrl(); // 基础URL
String skey = (String) core.getLoginInfo().get(StorageLoginInfoEnum.skey.getKey());
List<String> headUrlList = new ArrayList<String>(); // 好友头像URL列表
if (text.equals("111")) {
List<JSONObject> friends = WechatTools.getContactList();
for (JSONObject friend : friends) {
headUrlList.add(friend.getString("HeadImgUrl"));
}
}
for (int i = 0; i < headUrlList.size(); i++) {

String url = baseUrl + headUrlList.get(i) + skey;
LOG.info(url);
String headPicPath = path + File.separator + i + ".jpg";
HttpEntity entity = myHttpClient.doGet(url, null, true, null);
try {
OutputStream out = new FileOutputStream(headPicPath);
byte[] bytes = EntityUtils.toByteArray(entity);
out.write(bytes);
out.flush();
out.close();
try {
// CommonTools.printQr(qrPath); // 打开登陆二维码图片
} catch (Exception e) {
LOG.info(e.getMessage());
}

} catch (Exception e) {
LOG.info(e.getMessage());
}

}
System.out.println(headUrlList);
}
return null;
}

@Override
public String picMsgHandle(JSONObject msg) {
// TODO Auto-generated method stub
return null;
}

@Override
public String voiceMsgHandle(JSONObject msg) {
// TODO Auto-generated method stub
return null;
}

@Override
public String viedoMsgHandle(JSONObject msg) {
// TODO Auto-generated method stub
return null;
}

@Override
public String nameCardMsgHandle(JSONObject msg) {
// TODO Auto-generated method stub
return null;
}

public static void main(String[] args) {
String qrPath = "D://itchat4j//login"; // 保存登陆二维码图片的路径,这里需要在本地新建目录
IMsgHandlerFace msgHandler = new PicYourFriends(); // 实现IMsgHandlerFace接口的类
Wechat wechat = new Wechat(msgHandler, qrPath); // 【注入】
wechat.start(); // 启动服务,会在qrPath下生成一张二维码图片,扫描即可登陆,注意,二维码图片如果超过一定时间未扫描会过期,过期时会自动更新,所以你可能需要重新打开图片
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.zhouyafeng.itchat4j.demo.demo3;
package cn.zhouyafeng.itchat4j.demo.unuseful;

import java.io.IOException;

Expand Down

0 comments on commit 25c18e4

Please sign in to comment.