Skip to content

Commit

Permalink
Merge pull request yaphone#41 from Jray616/master
Browse files Browse the repository at this point in the history
新增被动接收好友请求
  • Loading branch information
yaphone committed Jun 28, 2017
2 parents 26a0c22 + 14bc2bf commit 29b561b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
62 changes: 56 additions & 6 deletions src/main/java/cn/zhouyafeng/itchat4j/api/MessageTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.*;

import javax.activation.MimetypesFileTypeMap;

import cn.zhouyafeng.itchat4j.utils.enums.StorageLoginInfoEnum;
import com.sun.xml.internal.bind.v2.TODO;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
Expand Down Expand Up @@ -75,8 +75,8 @@ public static void sendMsgById(String text, String id) {
*
* @author https://github.com/yaphone
* @date 2017年5月4日 下午11:17:38
* @param msg
* @param toUserName
* @param text
* @param nickName
*/
public static boolean sendMsgByNickName(String text, String nickName) {
if (nickName != null) {
Expand Down Expand Up @@ -364,4 +364,54 @@ private static boolean webWxSendAppMsg(String userId, Map<String, String> data)
return false;
}

/**
* 被动添加好友
* @param core
* @param userName 对方用户名
* @param status 2 是添加 3是接受
* @param ticket
*/
public static void addFriend(Core core,String userName, Integer status,String ticket) {

String url = String.format(URLEnum.WEB_WX_VERIFYUSER.getUrl(),
core.getLoginInfo().get("url"),
String.valueOf(System.currentTimeMillis() / 3158L),
core.getLoginInfo().get("pass_ticket"));

List<Map<String,Object>> verifyUserList = new ArrayList<>();
Map<String,Object> verifyUser = new HashMap<String,Object>();
verifyUser.put("Value",userName);
verifyUser.put("VerifyUserTicket",ticket);
verifyUserList.add(verifyUser);

List<Integer> sceneList = new ArrayList<>();
sceneList.add(33);

JSONObject body = new JSONObject();
body.put("BaseRequest", core.getParamMap().get("BaseRequest"));
body.put("Opcode",status);
body.put("VerifyUserListSize",1);
body.put("VerifyUserList",verifyUserList);
body.put("VerifyContent","");
body.put("SceneListCount",1);
body.put("SceneList",sceneList);
body.put("skey", (String) core.getLoginInfo().get(StorageLoginInfoEnum.skey.getKey()));

String result = null;
try {
String paramStr = JSON.toJSONString(body);
HttpEntity entity = myHttpClient.doPost(url, paramStr);
result = EntityUtils.toString(entity, Consts.UTF_8);
} catch (Exception e) {
LOG.error("webWxSendMsg", e);
}

if (StringUtils.isBlank(result)) {
LOG.error("被动添加好友失败");
}

LOG.debug(result);

}

}
18 changes: 18 additions & 0 deletions src/main/java/cn/zhouyafeng/itchat4j/core/MsgCenter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package cn.zhouyafeng.itchat4j.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;

import cn.zhouyafeng.itchat4j.utils.enums.StorageLoginInfoEnum;
import cn.zhouyafeng.itchat4j.utils.enums.URLEnum;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -84,6 +95,10 @@ public static JSONArray produceMsg(JSONArray msgList) {
m.put("Type", MsgTypeEnum.VOICE.getType());
} else if (m.getInteger("MsgType").equals(MsgCodeEnum.MSGTYPE_VERIFYMSG.getCode())) {// friends
// 好友确认消息
JSONObject recommendInfo = m.getJSONObject("RecommendInfo");
String userName = recommendInfo.getString("UserName");
String ticket = recommendInfo.getString("Ticket");
MessageTools.addFriend(core,userName,3,ticket);

} else if (m.getInteger("MsgType").equals(MsgCodeEnum.MSGTYPE_SHARECARD.getCode())) { // 共享名片
m.put("Type", MsgTypeEnum.NAMECARD.getType());
Expand Down Expand Up @@ -167,4 +182,7 @@ public static void handleMsg(IMsgHandlerFace msgHandler) {
}
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum MsgCodeEnum {
MSGTYPE_STATUSNOTIFY(51, ""),
MSGTYPE_SYSNOTICE(9999, ""),
MSGTYPE_POSSIBLEFRIEND_MSG(40, ""),
MSGTYPE_VERIFYMSG(37, ""),
MSGTYPE_VERIFYMSG(37, "好友请求"),
MSGTYPE_SHARECARD(42, ""),
MSGTYPE_SYS(10000, "系统消息"),
MSGTYPE_RECALLED(10002, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public enum URLEnum {
WEB_WX_PUSH_LOGIN("%s/webwxpushloginurl", "不扫码登陆"),
WEB_WX_LOGOUT("%s/webwxlogout", "退出微信"),
WEB_WX_BATCH_GET_CONTACT("%s/webwxbatchgetcontact?type=ex&r=%s&lang=zh_CN&pass_ticket=%s", "查询群信息"),
WEB_WX_REMARKNAME("%s/webwxoplog?lang=zh_CN&pass_ticket=%s", "修改好友备注")
WEB_WX_REMARKNAME("%s/webwxoplog?lang=zh_CN&pass_ticket=%s", "修改好友备注"),
WEB_WX_VERIFYUSER("%s/webwxverifyuser?r=%s&lang=zh_CN&pass_ticket=%s", "被动添加好友")





Expand Down

0 comments on commit 29b561b

Please sign in to comment.