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

控制台打印二维码 #58

Open
wants to merge 4 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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
<dependencies>

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->


<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/cn/zhouyafeng/itchat4j/core/MsgCenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,12 @@ public static JSONArray produceMsg(JSONArray msgList) {
&& !core.getGroupIdList().contains(m.getString("ToUserName"))) {
core.getGroupIdList().add((m.getString("ToUserName")));
}
// 群消息与普通消息不同的是在其消息体(Content)中会包含发送者id及":<br/>"消息,这里需要处理一下,去掉多余信息,只保留消息内容
if (m.getString("Content").contains("<br/>")) {
String content = m.getString("Content").substring(m.getString("Content").indexOf("<br/>") + 5);
m.put("Content", content);
m.put("groupMsg", true);
}
} else {
CommonTools.msgFormatter(m, "Content");

m.put("groupMsg", true);
}
CommonTools.msgFormatter(m, "Content");


if (m.getInteger("MsgType").equals(MsgCodeEnum.MSGTYPE_TEXT.getCode())) { // words
// 文本消息
if (m.getString("Url").length() != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

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

import cn.zhouyafeng.itchat4j.beans.BaseMsg;
import cn.zhouyafeng.itchat4j.core.Core;
import cn.zhouyafeng.itchat4j.core.MsgCenter;
import cn.zhouyafeng.itchat4j.service.ILoginService;
import cn.zhouyafeng.itchat4j.utils.Config;
import cn.zhouyafeng.itchat4j.utils.MyHttpClient;
import cn.zhouyafeng.itchat4j.utils.QRterminal;
import cn.zhouyafeng.itchat4j.utils.SleepUtils;
import cn.zhouyafeng.itchat4j.utils.enums.ResultEnum;
import cn.zhouyafeng.itchat4j.utils.enums.RetCodeEnum;
Expand All @@ -42,6 +39,10 @@
import cn.zhouyafeng.itchat4j.utils.enums.parameters.UUIDParaEnum;
import cn.zhouyafeng.itchat4j.utils.tools.CommonTools;

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

/**
* 登陆服务实现类
*
Expand Down Expand Up @@ -130,26 +131,28 @@ public String getUuid() {

@Override
public boolean getQR(String qrPath) {
qrPath = qrPath + File.separator + "QR.jpg";
String qrUrl = URLEnum.QRCODE_URL.getUrl() + core.getUuid();
HttpEntity entity = myHttpClient.doGet(qrUrl, null, true, null);

// qrPath = qrPath + File.separator + "QR.jpg";
// String qrUrl = URLEnum.QRCODE_URL.getUrl() + core.getUuid();
// HttpEntity entity = myHttpClient.doGet(qrUrl, null, true, null);
try {
OutputStream out = new FileOutputStream(qrPath);
byte[] bytes = EntityUtils.toByteArray(entity);
out.write(bytes);
out.flush();
out.close();
try {
CommonTools.printQr(qrPath); // 打开登陆二维码图片
} catch (Exception e) {
LOG.info(e.getMessage());
}

// OutputStream out = new FileOutputStream(qrPath);
// byte[] bytes = EntityUtils.toByteArray(entity);
// out.write(bytes);
// out.flush();
// out.close();
// try {
// CommonTools.printQr(qrPath); // 打开登陆二维码图片
// } catch (Exception e) {
// LOG.info(e.getMessage());
// }
String qrUrl2 = URLEnum.cAPI_qrcode.getUrl() + core.getUuid();
String qrString=QRterminal.getQr(qrUrl2);
LOG.error(qrString);
} catch (Exception e) {
LOG.info(e.getMessage());
return false;
}

return true;
}

Expand Down Expand Up @@ -251,7 +254,7 @@ public void run() {
while (core.isAlive()) {
try {
Map<String, String> resultMap = syncCheck();
LOG.info(JSONObject.toJSONString(resultMap));
LOG.info("startReceiving()--->" +JSONObject.toJSONString(resultMap));
String retcode = resultMap.get("retcode");
String selector = resultMap.get("selector");
if (retcode.equals(RetCodeEnum.UNKOWN.getCode())) {
Expand Down Expand Up @@ -312,6 +315,7 @@ public void run() {
JSONObject obj = webWxSync();
}
} catch (Exception e) {
e.printStackTrace();
LOG.info(e.getMessage());
retryCount += 1;
if (core.getReceivingRetryCount() < retryCount) {
Expand Down Expand Up @@ -451,7 +455,6 @@ public String checklogin(String result) {
*
* @author https://github.com/yaphone
* @date 2017年4月9日 下午12:16:26
* @param result
*/
private void processLoginInfo(String loginContent) {
String regEx = "window.redirect_uri=\"(\\S+)\";";
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/cn/zhouyafeng/itchat4j/utils/QRterminal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cn.zhouyafeng.itchat4j.utils;

import java.util.Hashtable;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRterminal {

public static String getQr(String text) {
String s = "生成二维码失败";
int width = 40;
int height = 40;
// 用于设置QR二维码参数
Hashtable<EncodeHintType, Object> qrParam = new Hashtable<EncodeHintType, Object>();
// 设置QR二维码的纠错级别——这里选择最高H级别
qrParam.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
qrParam.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, qrParam);
s = toAscii(bitMatrix);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}

public static String toAscii(BitMatrix bitMatrix) {
StringBuilder sb = new StringBuilder();
for (int rows = 0; rows < bitMatrix.getHeight(); rows++) {
for (int cols = 0; cols < bitMatrix.getWidth(); cols++) {
boolean x = bitMatrix.get(rows, cols);
if (!x) {
// white
sb.append("\033[47m \033[0m");
} else {
sb.append("\033[40m \033[0m");
}
}
sb.append("\n");
}
return sb.toString();
}

public static void main(String[] args) throws Exception {
String text = "https://github.com/zhangshanhai/java-qrcode-terminal ";

System.out.println(getQr(text));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum URLEnum {


BASE_URL("https://login.weixin.qq.com","基本的URL"),
cAPI_qrcode("https://login.weixin.qq.com/l/", "API_qrcode"),
UUID_URL(BASE_URL.url+"/jslogin","UUIDLURL"),
QRCODE_URL(BASE_URL.url+"/qrcode/","初始化URL"),
STATUS_NOTIFY_URL(BASE_URL.url+"/webwxstatusnotify?lang=zh_CN&pass_ticket=%s","微信状态通知"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ public static void emojiFormatter(JSONObject d, String k) {
* @param k
*/
public static void msgFormatter(JSONObject d, String k) {
d.put(k, d.getString(k).replace("<br/>", "\n"));
String content = d.getString(k);
if(content.startsWith("@")){
content=content.substring(content.indexOf(":")+1);
}
d.put(k, content.replace("<br/>", "\n"));
emojiFormatter(d, k);
// TODO 与emoji表情有部分兼容问题,目前暂未处理解码处理 d.put(k,
// StringEscapeUtils.unescapeHtml4(d.getString(k)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
public class MyTest {
public static void main(String[] args) {
String qrPath = "D://itchat4j//login"; // 保存登陆二维码图片的路径,这里需要在本地新建目录
String qrPath = "./"; // 保存登陆二维码图片的路径,这里需要在本地新建目录
IMsgHandlerFace msgHandler = new SimpleDemo(); // 实现IMsgHandlerFace接口的类
Wechat wechat = new Wechat(msgHandler, qrPath); // 【注入】
wechat.start(); // 启动服务,会在qrPath下生成一张二维码图片,扫描即可登陆,注意,二维码图片如果超过一定时间未扫描会过期,过期时会自动更新,所以你可能需要重新打开图片
Expand Down