alroyso 1 month ago
parent
commit
d8f010d8d0
1 changed files with 46 additions and 15 deletions
  1. 46 15
      lib/app/controller/GlobalController.dart

+ 46 - 15
lib/app/controller/GlobalController.dart

@@ -80,26 +80,38 @@ class GlobalController extends GetxController {
   Future<bool> checkAllCoresStopped() async {
     bool allStopped = true;
 
-    // 检查 Clash 核心是否已停止
+    // 检查 Clash 核心是否已启动
     if (Platform.isWindows) {
-      allStopped =  controllers.service.coreStatus.value == RunningState.running;
+      allStopped = controllers.service.coreStatus.value == RunningState.running;
     } else if (Platform.isMacOS) {
       if(await controllers.cc_service.isCanOperationService()){
         allStopped = controllers.cc_service.coreStatus.value == RunningState.running;
       } else {
-        allStopped =  controllers.service.coreStatus.value == RunningState.running;
+        allStopped = controllers.service.coreStatus.value == RunningState.running;
       }
-
     }
 
-    // 检查其他可能的核心进程
-    // if (!controllers.service.serviceMode.value) {
-    //   if (Platform.isWindows) {
-    //     allStopped = allStopped && !(await isProcessRunning(path.basename(Files.assetsClashService.path)));
-    //   }
-    //   allStopped = allStopped && !(await isProcessRunning(path.basename(Files.assetsCCore.path)));
-    //   allStopped = allStopped && !(await isProcessRunning(path.basename(Files.assetsClashService.path)));
-    // }
+    // 如果核心未运行,尝试启动它
+    if (!allStopped) {
+      try {
+        if (Platform.isWindows) {
+          await controllers.service.startClashCore();
+        } else if (Platform.isMacOS) {
+          if (controllers.cc_service.serviceIsRuning) {
+            //await controllers.cc_service.fetchStart();
+          } else {
+            await controllers.service.startClashCore();
+          }
+        }
+        // 等待核心启动
+        await Future.delayed(Duration(seconds: 2));
+        // 再次检查核心状态
+        return await checkAllCoresStopped();
+      } catch (e) {
+        LogHelper().e("启动内核失败: $e");
+        return false;
+      }
+    }
 
     return allStopped;
   }
@@ -163,7 +175,16 @@ class GlobalController extends GetxController {
   }
 
   Future<void> fetchNodes() async {
-    nodeModes.value = await ApiService().getNode("/api/client/v4/nodes?vless=1");
+    try {
+      // 检查内核是否正在运行
+      if (!await checkAllCoresStopped()) {
+        throw Exception("内核未启动");
+      }
+      
+      nodeModes.value = await ApiService().getNode("/api/client/v4/nodes?vless=1");
+    } catch (e) {
+      handleApiError(e);
+    }
   }
 
   Future<void> updateNode() async {
@@ -189,7 +210,13 @@ class GlobalController extends GetxController {
   }
 
   Future<void> swift(String name) async {
-    try{
+    try {
+      // 检查内核是否正在运行
+      if (!await checkAllCoresStopped()) {
+        throw Exception("内核未启动");
+        return;
+      }
+
       var g = "proxy";
       if(modesSelect.value == "global") {
         g = "GLOBAL";
@@ -202,7 +229,7 @@ class GlobalController extends GetxController {
         if (it.chains.contains(name)) controllers.core.fetchCloseConnections(it.id);
       }
     } catch (e) {
-      //LogHelper().d(e.toString());
+      handleApiError(e);
     }
   }
   // 创建代理配置文件
@@ -360,6 +387,10 @@ class GlobalController extends GetxController {
     } else {
       LogHelper().d('Other error: $error');
       errorMsg.value = error.toString();
+      if (error.toString().contains("内核未启动")) {
+        // 可以在这里添加UI提示或其他处理
+        msgStatus.value = "内核未启动,请稍后重试";
+      }
     }
   }