diff --git a/app/Http/Controllers/Api/TG/HookController.php b/app/Http/Controllers/Api/TG/HookController.php index abf3fd2..8b7467a 100755 --- a/app/Http/Controllers/Api/TG/HookController.php +++ b/app/Http/Controllers/Api/TG/HookController.php @@ -70,16 +70,29 @@ final class HookController $sClassPath = "App\\Services\\TG\\Hook\\Mod\\".$sClass; if (method_exists($sClassPath, $sFunc)) { - $oInstans = new $sClassPath(); $r = $oInstans->$sFunc($aParams); + // 可以不返回,里面直接send + if (isset($r)) { + // 返回可以是数组 + if (is_array($r)) { + foreach ($r as $row) { + $oTGHttp->sendToTG($row); + } + } else { + $oTGHttp->sendToTG($r); + } + + Http::response(200, 'ok'); + } + } else { throw new \Exception("没有这个方法"); } } catch (\Throwable $e) { - $sMessage = "错误: " . $e->getMessage(); + $sMessage = "错误(line:".$e->getLine()."): " . $e->getMessage(); $oTGHttp->sendToTG($sMessage); diff --git a/app/Http/Controllers/CronController.php b/app/Http/Controllers/CronController.php index 8270591..64959f9 100755 --- a/app/Http/Controllers/CronController.php +++ b/app/Http/Controllers/CronController.php @@ -15,6 +15,8 @@ class CronController private $sProjectPath; private $sProjectUrl; + private $aArticle; + // public function __construct() // { // $this->sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName; @@ -30,7 +32,14 @@ class CronController $s = date("s"); //test - $i = '03'; + $aGet = $_GET; + if (isset($aGet['is_test']) && $aGet['is_test'] == 1) { + if ($aGet["time_type"] == 'i') { + $i = $aGet['v']; + } else if ($aGet["time_type"] == 'h') { + $h = $aGet['v']; + } + } // i_in10规则 $iIin10 = $i[1]; @@ -56,6 +65,7 @@ class CronController $this->run($aArticle); } + } } catch (\Throwable $e) { @@ -76,6 +86,7 @@ class CronController $this->sProjectName = $row["project_name"]; $this->sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName; $this->sProjectUrl = env("github_url_base").":".$this->sUserName."/".$this->sProjectName.".git"; + $this->aArticle = $row; list($sClass, $sFunc) = explode('#', $row['mod']); @@ -125,12 +136,14 @@ class CronController exec($command, $output, $returnVar); if ($returnVar !== 0) { - // 将输出数组转换为 JSON 格式 - $jsonOutput = json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); - $errorMessage = "命令失败: $command\n" . $jsonOutput . "\n"; + $aMsg = [ + "命令" => $command, + "结果" => $output, + "文章" => $this->aArticle, + ]; - throw new \Exception($errorMessage); + throw new \Exception(json_encode($aMsg, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); } } @@ -183,8 +196,7 @@ class CronController { // 进入指定目录 if (!chdir($this->sProjectPath)) { - echo "无法进入目录 {$this->sProjectPath}\n"; - exit(1); + throw new \Exception("无法进入目录 {$this->sProjectPath}\n"); } // 添加更改到暂存区 diff --git a/app/Http/Controllers/TestsController.php b/app/Http/Controllers/TestsController.php index f2371d2..588a801 100755 --- a/app/Http/Controllers/TestsController.php +++ b/app/Http/Controllers/TestsController.php @@ -12,7 +12,11 @@ class TestsController public function hook() { - +// $t = []; +// $a = $t['zz']["cc"] += 10; +// $a = $t['zz']["cc"] += 10; +// tomd($a); +// exit; @$k = $_GET["k"]; @$s = $_GET["s"]; @@ -24,7 +28,7 @@ class TestsController //test // $s = "\/AticleSet__1115zz__sale_rate 1118.118"; - $s = "\/git#pullR 1"; + $s = "\/test#cron__i 14"; $oHttp = new Http(); $oHttp->sMethod = "post"; diff --git a/app/Services/TG/Hook/Mod/Base.php b/app/Services/TG/Hook/Mod/Base.php index 6046554..6aede03 100644 --- a/app/Services/TG/Hook/Mod/Base.php +++ b/app/Services/TG/Hook/Mod/Base.php @@ -11,22 +11,10 @@ class Base { public function __construct() { $this->oHttp = new Http(); $this->oTGHttp = new TGHttp(); - $this->oTGHttp->sApiToken = env("telegram_token"); - $this->oTGHttp->sApiChatId = env("telegram_chatid"); +// $this->oTGHttp->sApiToken = env("telegram_token"); +// $this->oTGHttp->sApiChatId = env("telegram_chatid"); } -// protected function sendMessage($sText = '?') { -// -// if (env('is_loc') === true) { -// echo "
";
-// var_dump($sText);
-// } else {
-// $url = "https://api.telegram.org/bot$this->sApiToken/sendMessage?chat_id=$this->sApiChatId&text=" . urlencode($sText);
-// file_get_contents($url);
-// }
-//
-// }
-
protected function cmdGood($sCmd) {
$sCmdNew = "";
diff --git a/app/Services/TG/Hook/Mod/Git.php b/app/Services/TG/Hook/Mod/Git.php
index ea3ae1b..676bb39 100644
--- a/app/Services/TG/Hook/Mod/Git.php
+++ b/app/Services/TG/Hook/Mod/Git.php
@@ -49,8 +49,7 @@ class Git extends Base {
// 进入指定目录
if (!chdir($sProjectPath)) {
- echo "无法进入目录 {$sProjectPath}\n";
- exit(1);
+ throw new \Exception("无法进入目录".$sProjectPath);
}
// 获取远程更新
@@ -58,8 +57,10 @@ class Git extends Base {
// 硬重置到远程主分支
$this->runCommand('git reset --hard origin/main');
-
- echo "操作完成:已重置到远程主分支。\n";
+
+ $this->oTGHttp->sendToTG("操作完成:已重置到远程主分支");
+
+ $this->oHttp::response(200, 'ok');
}
diff --git a/app/Services/TG/Hook/Mod/Test.php b/app/Services/TG/Hook/Mod/Test.php
index 94598bc..aa953ab 100644
--- a/app/Services/TG/Hook/Mod/Test.php
+++ b/app/Services/TG/Hook/Mod/Test.php
@@ -1,30 +1,27 @@
'SaleCode#List',
- '优惠码find' => 'SaleCode#Find xxhh',
- '优惠码rate' => 'SaleCode#SetRate__xxzz 6.7',
- '文章list' => 'Article#List',
- '文章like' => 'Article#Like__field like',
- '文章add' => 'Article#Add__name',
- '文章del' => 'Article#Del__id',
- '文章set' => 'Article#Set__id__fild set',
-// '添加github的sshhost' => "System#AddSshHostGithub",
- '文章clone' => "article#clone id",
- '手动pullR' => "article#clone id",
+ $this->oHttp->sMethod = "get";
+ $this->oHttp->sToUrl = env("APP_URL")."/cron";
+ $this->oHttp->bIsJson = true;
+ $this->oHttp->aData = [
+ "is_test" => true,
+ "time_type" => $sTimeType,
+ "v" => $sValue
];
+
+ $r = $this->oHttp->send();
- $this->oTGHttp->sendToTG(json_encode($aCmdList, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
-
- $this->oHttp::response(200, 'ok');
+ return $r;
}
diff --git a/app/Services/TG/Http.php b/app/Services/TG/Http.php
index bb6e9ea..9eab3b6 100644
--- a/app/Services/TG/Http.php
+++ b/app/Services/TG/Http.php
@@ -18,7 +18,7 @@ class Http {
if (env('is_loc') === true) {
echo "";
- var_dump($sMsg);
+ echo($sMsg);
} else {
$url = "https://api.telegram.org/bot$this->sApiToken/sendMessage?chat_id=$this->sApiChatId&text=" . urlencode($sMsg);
file_get_contents($url);
diff --git a/app/Services/TomTool/Http.php b/app/Services/TomTool/Http.php
index c0d3f1c..794e0f3 100755
--- a/app/Services/TomTool/Http.php
+++ b/app/Services/TomTool/Http.php
@@ -52,6 +52,11 @@ class Http
// 默认使用 GET 请求
curl_setopt($ch, CURLOPT_HTTPGET, true); // 设置为 GET 请求
curl_setopt($ch, CURLOPT_URL, $this->sToUrl . '?' . http_build_query($this->aData)); // 设置请求 URL
+ if ($this->bIsJson) {
+ curl_setopt($ch, CURLOPT_HTTPHEADER, [
+ 'Accept: application/json' // 设置接受的内容类型为 JSON
+ ]);
+ }
break;
}