Pārlūkot izejas kodu

9/18 application improve

xiexiang 1 gadu atpakaļ
vecāks
revīzija
65c1f97b60

+ 18 - 89
src/main/java/com/example/mos/MosApplication.java

@@ -7,9 +7,12 @@ import com.example.mos.weChatLogin.awt.MyImageShowFrame;
 import com.example.mos.weChatLogin.model.WxResultBody;
 import com.example.mos.weChatLogin.okhttp.MyCookieStore;
 import com.example.mos.weChatLogin.utils.HttpUtils;
+import jakarta.annotation.PostConstruct;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.context.ApplicationContext;
 import org.springframework.scheduling.annotation.EnableScheduling;
 
 import javax.swing.*;
@@ -26,93 +29,12 @@ import java.util.concurrent.FutureTask;
 @SpringBootApplication
 @EnableScheduling
 public class MosApplication {
+    @Autowired
+    private MainView mainView;
+
     //    private static MyImageShowFrame myImageShowFrame;
     public static void main(String[] args) throws IOException {
-
-//        //图片显示控件
-//        myImageShowFrame = new MyImageShowFrame();
-//
-//        // 1. POST请求开始登录接口,初始化cookie
-//        String sessionid = "" + System.currentTimeMillis() + (int)(Math.random()*100);//生成一个包含当前时间戳以及随机数的字符串
-//        WxResultBody wxResultBody = WeiXinApi.startLogin(sessionid);
-//        System.out.println("---请求开始登录接口 返回结果:" + wxResultBody.toString());
-//        // 2. 请求获取二维码图片接口,得到流
-//        InputStream qrCodeIs = WeiXinApi.getQRCode();
-//        //转成图片显示
-//        myImageShowFrame.setTitle("请使用微信扫码登录");
-//        myImageShowFrame.showImage(qrCodeIs);
-//        myImageShowFrame.setLocation(500,100);
-//        // 3.轮询二维码状态接口
-//        FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
-//            @Override
-//            public Integer call() throws Exception {
-//                while (true) {
-//                    WxResultBody askQRCode = WeiXinApi.askQRCode();
-//                    Integer status = askQRCode.getStatus();
-//                    if (status == 3) {
-//                        System.out.println("二维码已过期");
-//                        return 3;
-//                    } else if (status == 4) {
-//                        System.out.println("已扫码,等待确认");
-//                    } else if (status == 1) {
-//                        System.out.println("已确认登录,请稍后...");
-//                        return 1;
-//                    }
-//                    try {
-//                        Thread.sleep(1000);
-//                    } catch (InterruptedException e) {
-//                        e.printStackTrace();
-//                    }
-//                }
-//            }
-//        });
-//
-//        while (true) {
-//            Thread queryThread = new Thread(futureTask);
-//            queryThread.start();
-//            if (!futureTask.isDone()) {
-//                System.out.println("--正在查询扫码状态,请尽快扫码!");
-//            }
-//
-//            try {
-//                Integer integer = futureTask.get();
-//                if (integer == 1) {
-//                    //说明扫码确认成功,否则重新获取图片
-//                    //关闭图片显示
-//                    myImageShowFrame.close();
-//                    break;
-//                } else {
-//                    //重新获取二维码图片显示
-//                    myImageShowFrame.showImage(WeiXinApi.getQRCode());
-//                }
-//            } catch (Exception e) {
-//                System.out.println("查询接口出错了");
-//                break;
-//            }
-//        }
-//
-//        // 4.确认登录后,请求登录接口,拿到登录状态的cookie
-//        WxResultBody bizlogin = WeiXinApi.bizlogin();
-//        //重定向地址
-//        String redirect_url = bizlogin.getRedirect_url();
-//        //解析成键值对
-//        Map<String, String> loginRes = HttpUtils.parseQueryParams(redirect_url);
-//        //得到token
-//        String token = loginRes.get("token");
-//        //设置全局token值
-//        MyCookieStore.setToken(token);
-//
-//        System.out.println("---恭喜你,登录成功!");
-
-//        SpringApplication.run(MosApplication.class, args);
-//        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "后台程序已启动,请运行前台");
-//        GraphicsEnvironment.isHeadless();
-//        System.out.println(GraphicsEnvironment.isHeadless());
-//        SwingUtilities.invokeLater(() -> {
-//                new MainView();
-//        });
-
-
+//        ApplicationContext ctx = new SpringApplicationBuilder(MosApplication.class).headless(false).run(args);
         // 设置虚拟机参数允许图形界面操作
         System.setProperty("java.awt.headless", "false");
         // 检查系统是否支持 Headless 模式
@@ -121,11 +43,18 @@ public class MosApplication {
         } else {
             SpringApplication.run(MosApplication.class, args);
             System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "后台程序已启动,请运行前台");
-            // 在非 Headless 模式下运行 Swing GUI
-            SwingUtilities.invokeLater(() -> {
-                new MainView();
-            });
+//            // 在非 Headless 模式下运行 Swing GUI
+//            SwingUtilities.invokeLater(() -> {
+//                new MainView();
+//            });
         }
     }
 
+    @PostConstruct
+    public void init() {
+        SwingUtilities.invokeLater(() -> {
+            mainView.setVisible(true);
+        });
+    }
+
 }

+ 8 - 0
src/main/java/com/example/mos/container/MainView.java

@@ -33,6 +33,7 @@ import java.util.List;
  * @Author xiexiang
  * @Date 2024/6/19
  */
+@org.springframework.stereotype.Component
 public class MainView extends JFrame {
 
     OpenSearchPanelHandler openSearchPanelHandler;
@@ -560,4 +561,11 @@ public class MainView extends JFrame {
             return checkBox; // 返回复选框组件
         }
     }
+
+    public static void main(String[] args) {
+        SwingUtilities.invokeLater(()-> {
+            MainView mainView = new MainView();
+            mainView.setVisible(true);
+        });
+    }
 }

+ 1 - 1
src/main/java/com/example/mos/service/ArticleInfoService.java

@@ -78,7 +78,7 @@ public class ArticleInfoService extends ServiceImpl<ArticleInfoMapper, ArticleIn
             LambdaQueryWrapper<ArticleInfo> queryWrapper = new LambdaQueryWrapper<>();
             queryWrapper.eq(ArticleInfo::getTitle, title);
             // 使用 selectOne 进行查询,只关心是否存在记录
-            ArticleInfo article = this.getOne(queryWrapper);
+            ArticleInfo article = this.getOne(queryWrapper, false);
             return article != null;
         } catch (Exception e) {
             // 日志记录或其他处理方式