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

添加图片窗口 #114

Open
wants to merge 2 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
1 change: 0 additions & 1 deletion src/main/java/cn/zhouyafeng/itchat4j/Wechat.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class Wechat {
public Wechat(IMsgHandlerFace msgHandler, String qrPath) {
System.setProperty("jsse.enableSNIExtension", "false"); // 防止SSL错误
this.msgHandler = msgHandler;

// 登陆
LoginController login = new LoginController();
login.login(qrPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.zhouyafeng.itchat4j.api.WechatTools;
import cn.zhouyafeng.itchat4j.core.Core;
import cn.zhouyafeng.itchat4j.service.ILoginService;
import cn.zhouyafeng.itchat4j.service.impl.FrameLoginServicelmpl;
import cn.zhouyafeng.itchat4j.service.impl.LoginServiceImpl;
import cn.zhouyafeng.itchat4j.thread.CheckLoginStatusThread;
import cn.zhouyafeng.itchat4j.utils.SleepUtils;
Expand All @@ -21,7 +22,7 @@
*/
public class LoginController {
private static Logger LOG = LoggerFactory.getLogger(LoginController.class);
private ILoginService loginService = new LoginServiceImpl();
private ILoginService loginService = new FrameLoginServicelmpl();
private static Core core = Core.getInstance();

public void login(String qrPath) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cn.zhouyafeng.itchat4j.service.impl;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;

import cn.zhouyafeng.itchat4j.utils.enums.URLEnum;
import cn.zhouyafeng.itchat4j.utils.tools.CommonTools;

public class FrameLoginServicelmpl extends LoginServiceImpl {
@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);
try {
byte[] bytes = EntityUtils.toByteArray(entity);
BufferedImage image=ImageIO.read(new ByteArrayInputStream(bytes));
JFrame jf=new JFrame();
JPanel jp=new JPanel() {
public void paint(Graphics g) {
super.paint(g);
g.drawImage(image,0,0,null);
}
};
jf.getContentPane().add(jp);
jf.setSize(image.getWidth(),image.getHeight());
jf.setVisible(true);
try {
CommonTools.printQr(qrPath); // 打开登陆二维码图片
} catch (Exception e) {
LOG.info(e.getMessage());
}

} catch (Exception e) {
LOG.info(e.getMessage());
return false;
}

return true;
}

private File File(String qrPath) {
// TODO Auto-generated method stub
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
*
*/
public class LoginServiceImpl implements ILoginService {
private static Logger LOG = LoggerFactory.getLogger(LoginServiceImpl.class);
protected static Logger LOG = LoggerFactory.getLogger(LoginServiceImpl.class);

private Core core = Core.getInstance();
private MyHttpClient httpClient = core.getMyHttpClient();
protected Core core = Core.getInstance();
protected MyHttpClient httpClient = core.getMyHttpClient();

private MyHttpClient myHttpClient = core.getMyHttpClient();
protected MyHttpClient myHttpClient = core.getMyHttpClient();

public LoginServiceImpl() {

Expand Down
16 changes: 16 additions & 0 deletions src/test/java/cn/zhouyafeng/itchat4j/demo/demo1/MyTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package cn.zhouyafeng.itchat4j.demo.demo1;

import java.io.File;
import java.util.Scanner;

import javax.swing.JOptionPane;

import cn.zhouyafeng.itchat4j.Wechat;
import cn.zhouyafeng.itchat4j.api.MessageTools;
import cn.zhouyafeng.itchat4j.api.WechatTools;
import cn.zhouyafeng.itchat4j.face.IMsgHandlerFace;

/**
Expand All @@ -13,8 +20,17 @@
public class MyTest {
public static void main(String[] args) {
String qrPath = "D://itchat4j//login"; // 保存登陆二维码图片的路径,这里需要在本地新建目录
File c=new File(qrPath);
c.mkdirs();
IMsgHandlerFace msgHandler = new SimpleDemo(); // 实现IMsgHandlerFace接口的类
Wechat wechat = new Wechat(msgHandler, qrPath); // 【注入】
wechat.start(); // 启动服务,会在qrPath下生成一张二维码图片,扫描即可登陆,注意,二维码图片如果超过一定时间未扫描会过期,过期时会自动更新,所以你可能需要重新打开图片
Scanner reader=new Scanner(System.in);
while(true){
if(WechatTools.getWechatStatus()) {
System.out.print("begin"+WechatTools.getContactNickNameList());
MessageTools.sendMsgByNickName(reader.nextLine(), "Edward Yu");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String textMsgHandle(BaseMsg msg) {
// MessageTools.sendFileMsgByUserId(userId, docFilePath); // 发送文件
// MessageTools.sendPicMsgByUserId(userId, docFilePath);
String text = msg.getText(); // 发送文本消息,也可调用MessageTools.sendFileMsgByUserId(userId,text);
LOG.info(text);
//LOG.info(text);
if (text.equals("111")) {
WechatTools.logout();
}
Expand Down