Commit 926c7f0c authored by Paul0523's avatar Paul0523

增加缓存消息功能

parent 5e4f07e9
package com.yg84;
import com.yg84.weixin.Contact;
import com.yg84.weixin.Message;
import com.yg84.weixin.MessageHandler;
import com.yg84.weixin.WeChat;
import com.yg84.weixin.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
......@@ -22,7 +23,7 @@ public class Login {
public static WeChat weChat;
ArrayBlockingQueue<Message> messageQueue = new ArrayBlockingQueue<Message>(10000);
public void initWeChat() {
private void initWeChat() {
if (weChat != null) {
weChat.stopProcessThread();
}
......@@ -74,4 +75,26 @@ public class Login {
}
return messages;
}
@RequestMapping(value = "/getContactIcon")
public void getContactIcon(String userName, HttpServletResponse response) throws Exception{
File file = weChat.getContactIcon(userName);
if (file != null) {
response.addHeader("Content-Type", "image/x-png");
OutputStream out = response.getOutputStream();
InputStream in = new FileInputStream(file);
byte[] buf = new byte[2014];
int b = -1;
while ((b = in.read(buf)) != -1) {
out.write(buf, 0, b);
}
in.close();
out.close();
}
}
@RequestMapping(value = "/getMyAcount")
public MyAcount getMyAcount() {
return weChat.getMyAcount();
}
}
......@@ -12,12 +12,18 @@ import com.google.zxing.qrcode.encoder.QRCode;
import com.squareup.okhttp.*;
import org.apache.commons.lang.SystemUtils;
import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;
import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -27,6 +33,8 @@ import java.util.regex.Pattern;
*/
public class WeChat {
private File tempRootDir = new File(System.getProperty("user.home") + "/.wxBot");;
private OkHttpClient client = new OkHttpClient();
private final String ID_URL = "https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&fun=new&lang=zh_CN&_=";
......@@ -91,14 +99,20 @@ public class WeChat {
MyCookieStore cookieStore = new MyCookieStore();
client.setCookieHandler(new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL));
client.setReadTimeout(2, TimeUnit.MINUTES);
initTempDir();
}
public String run() throws Exception{
login();
init();
return "启动成功!";
}
public MyAcount getMyAcount() {
return myAcount;
}
public List<Contact> getContacts() {
return contacts;
}
......@@ -131,6 +145,17 @@ public class WeChat {
return "发送成功!";
}
public File getContactIcon(String userName) {
File file = new File(tempRootDir, "/icon/" + userName + ".png");
if (file.exists())
return file;
return null;
}
public void stopProcessThread() {
dealMsg = false;
}
private String login() throws Exception{
String url = ID_URL + getTimestamp();
Response response = get(url);
......@@ -266,6 +291,7 @@ public class WeChat {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 内容所使用字符集编码
QRCode qrCode = Encoder.encode(text, ErrorCorrectionLevel.L, hints);
File outputFile = new File(tempRootDir,"/qrcode/qrcode.png");
if (SystemUtils.IS_OS_LINUX) {
byte[][] matrix = qrCode.getMatrix().getArray();
System.out.print("\033[47;30m \033[0m");
......@@ -295,10 +321,8 @@ public class WeChat {
String format = "png";// 二维码的图片格式
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
// 生成二维码
File outputFile = new File("./result.png");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
Runtime run = Runtime.getRuntime();
System.out.println("cmd /c start " + outputFile.getAbsolutePath().replace(".\\", "").replace(":\\", ":\\\\"));
run.exec("cmd /c start " + outputFile.getAbsolutePath().replace(".\\", "").replace(":\\", ":\\\\"));
}else if (SystemUtils.IS_OS_MAC) {
int width = 300; // 二维码图片宽度
......@@ -306,7 +330,6 @@ public class WeChat {
String format = "png";// 二维码的图片格式
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
// 生成二维码
File outputFile = new File("./result.png");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
Runtime run = Runtime.getRuntime();
run.exec("open " + outputFile.getAbsolutePath());
......@@ -373,9 +396,10 @@ public class WeChat {
contacts = JSONObject.parseArray(JSONObject.parseObject(msg).getString("MemberList"), Contact.class);
contactByUserName = new HashMap<>();
contacts.forEach(contact -> {
contact.setHeadImgUrl(BASE_URL + contact.getHeadImgUrl() + skey);
contact.setHeadImgUrl(BASE_URL.replace("/cgi-bin/mmwebwx-bin", "") + contact.getHeadImgUrl() + skey);
contactByUserName.put(contact.getUserName(), contact);
});
cacheContactIcon(contacts);
return "获取朋友列表成功!";
}
......@@ -387,8 +411,27 @@ public class WeChat {
System.out.println(m.find());
}
public void stopProcessThread() {
dealMsg = false;
private void initTempDir() {
if (!tempRootDir.exists())
tempRootDir.mkdirs();
File qrcodedir = new File(tempRootDir, "/qrcode");
if (!qrcodedir.exists())
qrcodedir.mkdirs();
File icondir = new File(tempRootDir, "/icon");
if (!icondir.exists())
icondir.mkdirs();
}
private void cacheContactIcon(List<Contact> contacts) {
File iconDir = new File(tempRootDir, "/icon");
File[] files = iconDir.listFiles();
for (File file1 : files) {
file1.delete();
}
ExecutorService executor = Executors.newFixedThreadPool(20);
for (Contact contact : contacts) {
executor.submit(new CacheIcon(contact));
}
}
class ReceiveMsgThread extends Thread {
......@@ -404,4 +447,32 @@ public class WeChat {
}
}
class CacheIcon implements Runnable {
private Contact contact;
public CacheIcon(Contact contact) {
this.contact = contact;
}
@Override
public void run() {
try {
Response response = get(contact.getHeadImgUrl());
if (!response.isSuccessful())
return;
byte[] buf = new byte[2014];
int b = -1;
FileOutputStream out = new FileOutputStream(new File(tempRootDir, "/icon/" + contact.getUserName() + ".png"));
InputStream in = response.body().byteStream();
while ((b = in.read(buf)) != -1) {
out.write(buf, 0, b);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.yg84;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
* Created by fangzhipeng on 2017/8/2.
*/
public class CommonTest {
@Test
public void testHomeDir() throws Exception{
System.out.println(System.getProperty("user.home"));
File file = new File("C:\\Users\\Paul0\\.wxBot\\qrcode\\qrcode.txt");
OutputStream out = new FileOutputStream(file);
out.write("hello world".getBytes("utf-8"));
out.close();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment