Skip to content

Commit

Permalink
增加获取群好友昵称功能,修复已知问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
yaphone committed Jun 23, 2017
1 parent df9f0aa commit 447d193
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

## 更新日志

- 2017-6-23:增加获取群好友昵称功能,修复已知问题。

- 2017-6-16:增加微信状态维护

- 2017-6-13:修复获取群列表为空问题,增加根据群ID获取群成员列表方法
Expand All @@ -46,8 +48,6 @@

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



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

```
Expand All @@ -67,7 +67,7 @@ public static List<String> getGroupIdList()
此方法根据群ID(格式为`@@d052d34b9c9228830363013ee53deb461404f80ea353dbdd8fc9391cbf5f1c46`)获取群成员列表。其函数声明为:

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

#### 4.退出微信 WechatTools.logout()
Expand All @@ -86,55 +86,63 @@ public static void logout()
public static boolean getWechatStatus()
```

#### 6.根据用户昵称修改用户备注MessageTools.remarkNameByNickName(String nickName, String remName)
#### 6.获取群昵称列表WechatTools.getGroupNickNameList()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Expand Down
11 changes: 3 additions & 8 deletions src/main/java/cn/zhouyafeng/itchat4j/api/WechatTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,11 @@ public static List<String> getGroupNickNameList() {
* 根据groupIdList返回群成员列表
*
* @date 2017年6月13日 下午11:12:31
* @param groupIdList
* @param groupId
* @return
*/
public static JSONArray getMemberListByGroupId(String groupIdList) {
for (JSONObject o : getGroupList()) {
if (o.getString("UserName").equals(groupIdList)) {
return o.getJSONArray("MemberList");
}
}
return null;
public static JSONArray getMemberListByGroupId(String groupId) {
return core.getGroupMemeberMap().get(groupId);
}

/**
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/cn/zhouyafeng/itchat4j/core/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.zhouyafeng.itchat4j.utils.MyHttpClient;
Expand Down Expand Up @@ -46,7 +47,7 @@ public static Core getInstance() {
private List<JSONObject> memberList = new ArrayList<JSONObject>(); // 好友+群聊+公众号+特殊账号
private List<JSONObject> contactList = new ArrayList<JSONObject>();;// 好友
private List<JSONObject> groupList = new ArrayList<JSONObject>();; // 群
private List<JSONObject> groupMemeberList = new ArrayList<JSONObject>();; // 群聊成员字典
private Map<String, JSONArray> groupMemeberMap = new HashMap<String, JSONArray>(); // 群聊成员字典
private List<JSONObject> publicUsersList = new ArrayList<JSONObject>();;// 公众号/服务号
private List<JSONObject> specialUsersList = new ArrayList<JSONObject>();;// 特殊账号
private List<String> groupIdList = new ArrayList<String>(); // 群ID列表
Expand Down Expand Up @@ -189,14 +190,6 @@ public void setGroupList(List<JSONObject> groupList) {
this.groupList = groupList;
}

public List<JSONObject> getGroupMemeberList() {
return groupMemeberList;
}

public void setGroupMemeberList(List<JSONObject> groupMemeberList) {
this.groupMemeberList = groupMemeberList;
}

public List<JSONObject> getPublicUsersList() {
return publicUsersList;
}
Expand Down Expand Up @@ -261,4 +254,12 @@ public void setGroupNickNameList(List<String> groupNickNameList) {
this.groupNickNameList = groupNickNameList;
}

public Map<String, JSONArray> getGroupMemeberMap() {
return groupMemeberMap;
}

public void setGroupMemeberMap(Map<String, JSONArray> groupMemeberMap) {
this.groupMemeberMap = groupMemeberMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,16 @@ public void WebWxBatchGetContact() {
JSONObject obj = JSON.parseObject(text);
JSONArray contactList = obj.getJSONArray("ContactList");
for (int i = 0; i < contactList.size(); i++) { // 群好友
// TODO
if (contactList.getJSONObject(i).getString("UserName").indexOf("@@") > -1) { // 群
core.getGroupNickNameList().add(contactList.getJSONObject(i).getString("NickName")); // 更新群昵称列表
core.getGroupList().add(contactList.getJSONObject(i)); // 更新群信息(所有)列表
core.getGroupMemeberMap().put(contactList.getJSONObject(i).getString("UserName"),
contactList.getJSONObject(i).getJSONArray("MemberList")); // 更新群成员Map
}
}
} catch (Exception e) {
LOG.info(e.getMessage());
}
// TODO
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.alibaba.fastjson.JSONObject;

import cn.zhouyafeng.itchat4j.api.WechatTools;
import cn.zhouyafeng.itchat4j.core.Core;
import cn.zhouyafeng.itchat4j.face.IMsgHandlerFace;
import cn.zhouyafeng.itchat4j.utils.enums.MsgTypeEnum;
import cn.zhouyafeng.itchat4j.utils.tools.DownloadTools;
Expand Down Expand Up @@ -40,9 +41,9 @@ public String textMsgHandle(JSONObject msg) {
WechatTools.remarkNameByNickName("yaphone", "Hello");
}
if (text.equals("333")) { // 测试群列表
LOG.info(WechatTools.getGroupNickNameList());
LOG.info(WechatTools.getGroupIdList().size());
LOG.info(WechatTools.getContactList());
System.out.print(WechatTools.getGroupNickNameList());
System.out.print(WechatTools.getGroupIdList());
System.out.print(Core.getInstance().getGroupMemeberMap());
}
return text;
}
Expand Down

0 comments on commit 447d193

Please sign in to comment.