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

增加hotreload和群消息的发件人 #122

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
<version>3.0</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>

<!-- emoji -->
<dependency>
<groupId>com.vdurmont</groupId>
Expand All @@ -66,21 +72,26 @@
</dependency>

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/cn/zhouyafeng/itchat4j/Wechat.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@

public class Wechat {
private static final Logger LOG = LoggerFactory.getLogger(Wechat.class);
private IMsgHandlerFace msgHandler;
private final IMsgHandlerFace msgHandler;
private final LoginController login;

public Wechat(IMsgHandlerFace msgHandler, String qrPath) {
public Wechat(IMsgHandlerFace msgHandler, String qrPath, boolean hotReload) {
System.setProperty("jsse.enableSNIExtension", "false"); // 防止SSL错误
this.msgHandler = msgHandler;

// 登陆
LoginController login = new LoginController();
login.login(qrPath);
login = new LoginController();
login.login(qrPath, hotReload);
}

public void start() {
LOG.info("+++++++++++++++++++开始消息处理+++++++++++++++++++++");
new Thread(new Runnable() {
@Override
public void run() {
MsgCenter.handleMsg(msgHandler);
}
}).start();
new Thread(() -> MsgCenter.handleMsg(msgHandler)).start();
}

public LoginController getLogin() {
return login;
}

}
100 changes: 72 additions & 28 deletions src/main/java/cn/zhouyafeng/itchat4j/api/WechatTools.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package cn.zhouyafeng.itchat4j.api;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
Expand All @@ -17,12 +23,13 @@
import com.alibaba.fastjson.JSONObject;

import cn.zhouyafeng.itchat4j.core.Core;
import cn.zhouyafeng.itchat4j.utils.MyHttpClient;
import cn.zhouyafeng.itchat4j.utils.enums.StorageLoginInfoEnum;
import cn.zhouyafeng.itchat4j.utils.enums.URLEnum;

/**
* 微信小工具,如获好友列表等
*
*
* @author https://github.com/yaphone
* @date 创建时间:2017年5月4日 下午10:49:16
* @version 1.0
Expand All @@ -35,7 +42,7 @@ public class WechatTools {

/**
* 根据用户名发送文本消息
*
*
* @author https://github.com/yaphone
* @date 2017年5月4日 下午10:43:14
* @param msg
Expand All @@ -54,14 +61,14 @@ public static void sendMsgByUserName(String msg, String toUserName) {
* "@1212d3356aea8285e5bbe7b91229936bc183780a8ffa469f2d638bf0d2e4fc63",
* 可通过UserName发送消息
* </p>
*
*
* @author https://github.com/yaphone
* @date 2017年5月4日 下午10:56:31
* @param name
* @return
*/
public static String getUserNameByNickName(String nickName) {
for (JSONObject o : core.getContactList()) {
for (final JSONObject o : core.getContactList()) {
if (o.getString("NickName").equals(nickName)) {
return o.getString("UserName");
}
Expand All @@ -71,22 +78,22 @@ public static String getUserNameByNickName(String nickName) {

/**
* 返回好友昵称列表
*
*
* @author https://github.com/yaphone
* @date 2017年5月4日 下午11:37:20
* @return
*/
public static List<String> getContactNickNameList() {
List<String> contactNickNameList = new ArrayList<String>();
for (JSONObject o : core.getContactList()) {
final List<String> contactNickNameList = new ArrayList<>();
for (final JSONObject o : core.getContactList()) {
contactNickNameList.add(o.getString("NickName"));
}
return contactNickNameList;
}

/**
* 返回好友完整信息列表
*
*
* @date 2017年6月26日 下午9:45:39
* @return
*/
Expand All @@ -96,7 +103,7 @@ public static List<JSONObject> getContactList() {

/**
* 返回群列表
*
*
* @author https://github.com/yaphone
* @date 2017年5月5日 下午9:55:21
* @return
Expand All @@ -107,7 +114,7 @@ public static List<JSONObject> getGroupList() {

/**
* 获取群ID列表
*
*
* @date 2017年6月21日 下午11:42:56
* @return
*/
Expand All @@ -117,7 +124,7 @@ public static List<String> getGroupIdList() {

/**
* 获取群NickName列表
*
*
* @date 2017年6月21日 下午11:43:38
* @return
*/
Expand All @@ -127,7 +134,7 @@ public static List<String> getGroupNickNameList() {

/**
* 根据groupIdList返回群成员列表
*
*
* @date 2017年6月13日 下午11:12:31
* @param groupId
* @return
Expand All @@ -138,7 +145,7 @@ public static JSONArray getMemberListByGroupId(String groupId) {

/**
* 退出微信
*
*
* @author https://github.com/yaphone
* @date 2017年5月18日 下午11:56:54
*/
Expand All @@ -147,43 +154,43 @@ public static void logout() {
}

private static boolean webWxLogout() {
String url = String.format(URLEnum.WEB_WX_LOGOUT.getUrl(),
final String url = String.format(URLEnum.WEB_WX_LOGOUT.getUrl(),
core.getLoginInfo().get(StorageLoginInfoEnum.url.getKey()));
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
final List<BasicNameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("redirect", "1"));
params.add(new BasicNameValuePair("type", "1"));
params.add(
new BasicNameValuePair("skey", (String) core.getLoginInfo().get(StorageLoginInfoEnum.skey.getKey())));
try {
HttpEntity entity = core.getMyHttpClient().doGet(url, params, false, null);
String text = EntityUtils.toString(entity, Consts.UTF_8); // 无消息
final HttpEntity entity = core.getMyHttpClient().doGet(url, params, false, null);
final String text = EntityUtils.toString(entity, Consts.UTF_8); // 无消息
return true;
} catch (Exception e) {
} catch (final Exception e) {
LOG.debug(e.getMessage());
}
return false;
}

public static void setUserInfo() {
for (JSONObject o : core.getContactList()) {
for (final JSONObject o : core.getContactList()) {
core.getUserInfoMap().put(o.getString("NickName"), o);
core.getUserInfoMap().put(o.getString("UserName"), o);
}
}

/**
*
*
* 根据用户昵称设置备注名称
*
*
* @date 2017年5月27日 上午12:21:40
* @param userName
* @param remName
*/
public static void remarkNameByNickName(String nickName, String remName) {
String url = String.format(URLEnum.WEB_WX_REMARKNAME.getUrl(), core.getLoginInfo().get("url"),
final String url = String.format(URLEnum.WEB_WX_REMARKNAME.getUrl(), core.getLoginInfo().get("url"),
core.getLoginInfo().get(StorageLoginInfoEnum.pass_ticket.getKey()));
Map<String, Object> msgMap = new HashMap<String, Object>();
Map<String, Object> msgMap_BaseRequest = new HashMap<String, Object>();
final Map<String, Object> msgMap = new HashMap<>();
final Map<String, Object> msgMap_BaseRequest = new HashMap<>();
msgMap.put("CmdId", 2);
msgMap.put("RemarkName", remName);
msgMap.put("UserName", core.getUserInfoMap().get(nickName).get("UserName"));
Expand All @@ -193,23 +200,60 @@ public static void remarkNameByNickName(String nickName, String remName) {
msgMap_BaseRequest.put("DeviceID", core.getLoginInfo().get(StorageLoginInfoEnum.deviceid.getKey()));
msgMap.put("BaseRequest", msgMap_BaseRequest);
try {
String paramStr = JSON.toJSONString(msgMap);
HttpEntity entity = core.getMyHttpClient().doPost(url, paramStr);
final String paramStr = JSON.toJSONString(msgMap);
final HttpEntity entity = core.getMyHttpClient().doPost(url, paramStr);
// String result = EntityUtils.toString(entity, Consts.UTF_8);
LOG.info("修改备注" + remName);
} catch (Exception e) {
} catch (final Exception e) {
LOG.error("remarkNameByUserName", e);
}
}

/**
* 获取微信在线状态
*
*
* @date 2017年6月16日 上午12:47:46
* @return
*/
public static boolean getWechatStatus() {
return core.isAlive();
}

/**
* 暂存cookie
*/
public static void dumpCookie() {

FileOutputStream fs;
try {
fs = new FileOutputStream(Core.getHotReloadDir());
final ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(MyHttpClient.getCookieStore());
os.close();
} catch (final IOException e) {
LOG.error("", e);
}

}

/**
* 读取cookie
*
* @return
*/
public static CookieStore readCookieStore() {
FileInputStream fs;
try {
fs = new FileInputStream(Core.getHotReloadDir());

final ObjectInputStream ois = new ObjectInputStream(fs);
final CookieStore cookieStore = (CookieStore) ois.readObject();
ois.close();
return cookieStore;
} catch (IOException | ClassNotFoundException e) {
LOG.error("", e);
}
return null;
}

}
Loading