Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.完善获取群成员,2.群内消息保存发送消息者的username #39

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
93 changes: 55 additions & 38 deletions src/main/java/cn/zhouyafeng/itchat4j/api/WechatTools.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cn.zhouyafeng.itchat4j.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import cn.zhouyafeng.itchat4j.utils.MyHttpClient;
import cn.zhouyafeng.itchat4j.utils.enums.parameters.BaseParaEnum;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.message.BasicNameValuePair;
Expand Down Expand Up @@ -33,6 +32,8 @@ public class WechatTools {

private static Core core = Core.getInstance();

private static MyHttpClient myHttpClient = core.getMyHttpClient();

/**
* 根据用户名发送文本消息
*
Expand Down Expand Up @@ -76,22 +77,12 @@ public static String getUserNameByNickName(String nickName) {
* @date 2017年5月4日 下午11:37:20
* @return
*/
public static List<String> getContactNickNameList() {
List<String> contactNickNameList = new ArrayList<String>();
public static List<String> getContactList() {
List<String> contactList = new ArrayList<String>();
for (JSONObject o : core.getContactList()) {
contactNickNameList.add(o.getString("NickName"));
contactList.add(o.getString("NickName"));
}
return contactNickNameList;
}

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

/**
Expand All @@ -105,35 +96,61 @@ public static List<JSONObject> getGroupList() {
return core.getGroupList();
}

/**
* 获取群ID列表
*
* @date 2017年6月21日 下午11:42:56
* @return
*/
public static List<String> getGroupIdList() {
return core.getGroupIdList();
}

/**
* 获取群NickName列表
*
* @date 2017年6月21日 下午11:43:38
* @return
*/
public static List<String> getGroupNickNameList() {
return core.getGroupNickNameList();
}

/**
* 根据groupIdList返回群成员列表
*
*
* @date 2017年6月13日 下午11:12:31
* @param groupId
* @param groupIdList
* @return
*/
public static JSONArray getMemberListByGroupId(String groupId) {
return core.getGroupMemeberMap().get(groupId);
public static JSONObject getMemberListByGroupId(String groupIdList) {

JSONArray memberList=null;
for (JSONObject o : getGroupList()) {
if (o.getString("UserName").equals(groupIdList)) {
memberList=o.getJSONArray("MemberList");
}
}

Map<String, String> resultMap = new HashMap<String, String>();
// 组装请求URL和参数
String url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact?type=ex&pass_ticket="+StorageLoginInfoEnum.pass_ticket.getType()+"&" +
"r="+String.valueOf(new Date().getTime());
JSONObject postData = new JSONObject();

JSONObject baseRequest_JSON = new JSONObject();
for (BaseParaEnum baseRequest : BaseParaEnum.values()) {
baseRequest_JSON.put(baseRequest.para().toLowerCase(),
core.getLoginInfo().get(baseRequest.value()).toString());
}
JSONArray Liat_JSON = new JSONArray();
for(Object memberO:memberList){
JSONObject member= (JSONObject) memberO;
String UserName= (String) member.get("UserName");
JSONObject User = new JSONObject();
User.put("UserName",UserName);
User.put("EncryChatRoomId","");
Liat_JSON.add(User);
}
postData.put("BaseRequest",baseRequest_JSON);
postData.put("Count",memberList.size());
postData.put("List",Liat_JSON);

try {
HttpEntity entity = myHttpClient.doPost(url, postData.toJSONString());
String result = EntityUtils.toString(entity, Consts.UTF_8);
JSONObject fullFriendsJsonList = JSON.parseObject(result);

return fullFriendsJsonList;
} catch (Exception e) {
e.printStackTrace();
}
return null;

}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/zhouyafeng/itchat4j/core/MsgCenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public static JSONArray produceMsg(JSONArray msgList) {
// 群消息与普通消息不同的是在其消息体(Content)中会包含发送者id及":<br/>"消息,这里需要处理一下,去掉多余信息,只保留消息内容
if (m.getString("Content").contains("<br/>")) {
String content = m.getString("Content").substring(m.getString("Content").indexOf("<br/>") + 5);

//获取发送者的ID
m.put("fasongzheID",m.getString("Content").substring(0,m.getString("Content").indexOf("<br/>")-1));

m.put("Content", content);
m.put("groupMsg", true);
}
Expand Down