You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package xyz.wbsite.itchat4j;
import cn.hutool.core.io.FileUtil;
import cn.zhouyafeng.itchat4j.Wechat;
import xyz.wbsite.itchat4j.util.Logger;
import java.io.File;
/**
* 服务线程
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
public class JMainService {
/**
* 单例对象
*/
private static JMainService instance;
private static Wechat wechat;
public static synchronized JMainService getInstance() {
if (instance == null) {
JMainService.instance = new JMainService();
}
return instance;
}
/**
* 服务运行状态
*/
public boolean run;
/**
* 守护线程
*/
public LoginThread loginThread;
/**
* 服务内部构造器
*/
private JMainService() {
Logger.info("初始化服务");
}
public static void init() {
}
public void createDaemon() {
Logger.info("初始化线程");
this.loginThread = new LoginThread();
this.loginThread.setDaemon(true);
Logger.info("启动线程");
this.run = true;
this.loginThread.start();
}
public static boolean start() {
if (JMainService.getInstance().run) {
Logger.info("服务已启动");
return false;
}
// 创建线程
JMainService.getInstance().createDaemon();
return true;
}
public static boolean stop() {
if (!JMainService.getInstance().run) {
Logger.info("服务未启动");
return false;
}
// 停止服务
JMainService.getInstance().run = false;
// 关闭守护线程
JMainService.getInstance().loginThread.interrupt();
Logger.info("正在停止脚本");
return true;
}
/**
* 登录线程
*/
public class LoginThread extends Thread {
@Override
public void run() {
// 保存登陆二维码图片的路径,这里需要在本地新建目录
File qrPath = FileUtil.mkdir(new File(FileUtil.getTmpDir(), "itchat4j"));
wechat = new Wechat(new JMsgHandler(), qrPath.getAbsolutePath());
// 启动服务会在qrPath下生成一张二维码图片扫描即可登陆注意二维码图片如果超过一定时间未扫描会过期过期时会自动更新所以你可能需要重新打开图片
wechat.start();
}
}
}

Powered by TurnKey Linux.