no message

This commit is contained in:
hellcat 2025-04-15 18:56:13 +08:00
parent d16755f4a9
commit 2ff7fe2075
8 changed files with 68 additions and 48 deletions

View File

@ -70,16 +70,29 @@ final class HookController
$sClassPath = "App\\Services\\TG\\Hook\\Mod\\".$sClass; $sClassPath = "App\\Services\\TG\\Hook\\Mod\\".$sClass;
if (method_exists($sClassPath, $sFunc)) { if (method_exists($sClassPath, $sFunc)) {
$oInstans = new $sClassPath(); $oInstans = new $sClassPath();
$r = $oInstans->$sFunc($aParams); $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 { } else {
throw new \Exception("没有这个方法"); throw new \Exception("没有这个方法");
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
$sMessage = "错误: " . $e->getMessage(); $sMessage = "错误line:".$e->getLine().": " . $e->getMessage();
$oTGHttp->sendToTG($sMessage); $oTGHttp->sendToTG($sMessage);

View File

@ -15,6 +15,8 @@ class CronController
private $sProjectPath; private $sProjectPath;
private $sProjectUrl; private $sProjectUrl;
private $aArticle;
// public function __construct() // public function __construct()
// { // {
// $this->sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName; // $this->sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName;
@ -30,7 +32,14 @@ class CronController
$s = date("s"); $s = date("s");
//test //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规则 // i_in10规则
$iIin10 = $i[1]; $iIin10 = $i[1];
@ -56,6 +65,7 @@ class CronController
$this->run($aArticle); $this->run($aArticle);
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -76,6 +86,7 @@ class CronController
$this->sProjectName = $row["project_name"]; $this->sProjectName = $row["project_name"];
$this->sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName; $this->sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName;
$this->sProjectUrl = env("github_url_base").":".$this->sUserName."/".$this->sProjectName.".git"; $this->sProjectUrl = env("github_url_base").":".$this->sUserName."/".$this->sProjectName.".git";
$this->aArticle = $row;
list($sClass, $sFunc) = explode('#', $row['mod']); list($sClass, $sFunc) = explode('#', $row['mod']);
@ -125,12 +136,14 @@ class CronController
exec($command, $output, $returnVar); exec($command, $output, $returnVar);
if ($returnVar !== 0) { 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)) { if (!chdir($this->sProjectPath)) {
echo "无法进入目录 {$this->sProjectPath}\n"; throw new \Exception("无法进入目录 {$this->sProjectPath}\n");
exit(1);
} }
// 添加更改到暂存区 // 添加更改到暂存区

View File

@ -12,7 +12,11 @@ class TestsController
public function hook() public function hook()
{ {
// $t = [];
// $a = $t['zz']["cc"] += 10;
// $a = $t['zz']["cc"] += 10;
// tomd($a);
// exit;
@$k = $_GET["k"]; @$k = $_GET["k"];
@$s = $_GET["s"]; @$s = $_GET["s"];
@ -24,7 +28,7 @@ class TestsController
//test //test
// $s = "\/AticleSet__1115zz__sale_rate 1118.118"; // $s = "\/AticleSet__1115zz__sale_rate 1118.118";
$s = "\/git#pullR 1"; $s = "\/test#cron__i 14";
$oHttp = new Http(); $oHttp = new Http();
$oHttp->sMethod = "post"; $oHttp->sMethod = "post";

View File

@ -11,22 +11,10 @@ class Base {
public function __construct() { public function __construct() {
$this->oHttp = new Http(); $this->oHttp = new Http();
$this->oTGHttp = new TGHttp(); $this->oTGHttp = new TGHttp();
$this->oTGHttp->sApiToken = env("telegram_token"); // $this->oTGHttp->sApiToken = env("telegram_token");
$this->oTGHttp->sApiChatId = env("telegram_chatid"); // $this->oTGHttp->sApiChatId = env("telegram_chatid");
} }
// protected function sendMessage($sText = '?') {
//
// if (env('is_loc') === true) {
// echo "<pre>";
// 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) { protected function cmdGood($sCmd) {
$sCmdNew = ""; $sCmdNew = "";

View File

@ -49,8 +49,7 @@ class Git extends Base {
// 进入指定目录 // 进入指定目录
if (!chdir($sProjectPath)) { if (!chdir($sProjectPath)) {
echo "无法进入目录 {$sProjectPath}\n"; throw new \Exception("无法进入目录".$sProjectPath);
exit(1);
} }
// 获取远程更新 // 获取远程更新
@ -58,8 +57,10 @@ class Git extends Base {
// 硬重置到远程主分支 // 硬重置到远程主分支
$this->runCommand('git reset --hard origin/main'); $this->runCommand('git reset --hard origin/main');
echo "操作完成:已重置到远程主分支。\n"; $this->oTGHttp->sendToTG("操作完成:已重置到远程主分支");
$this->oHttp::response(200, 'ok');
} }

View File

@ -1,30 +1,27 @@
<?php <?php
namespace App\Services\TG\Hook\Mod; namespace App\Services\TG\Hook\Mod;
use App\Models\Article as ModelArticle; //use App\Services\TomTool\Http;
class Cmd extends Base { class Test extends Base {
public function List() public function Cron($aParam)
{ {
$sTimeType = $aParam["aOption"][1];
$sValue = $aParam["aArg"][1];
$aCmdList = [ $this->oHttp->sMethod = "get";
'优惠码list' => 'SaleCode#List', $this->oHttp->sToUrl = env("APP_URL")."/cron";
'优惠码find' => 'SaleCode#Find xxhh', $this->oHttp->bIsJson = true;
'优惠码rate' => 'SaleCode#SetRate__xxzz 6.7', $this->oHttp->aData = [
'文章list' => 'Article#List', "is_test" => true,
'文章like' => 'Article#Like__field like', "time_type" => $sTimeType,
'文章add' => 'Article#Add__name', "v" => $sValue
'文章del' => 'Article#Del__id',
'文章set' => 'Article#Set__id__fild set',
// '添加github的sshhost' => "System#AddSshHostGithub",
'文章clone' => "article#clone id",
'手动pullR' => "article#clone id",
]; ];
$r = $this->oHttp->send();
$this->oTGHttp->sendToTG(json_encode($aCmdList, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); return $r;
$this->oHttp::response(200, 'ok');
} }

View File

@ -18,7 +18,7 @@ class Http {
if (env('is_loc') === true) { if (env('is_loc') === true) {
echo "<pre>"; echo "<pre>";
var_dump($sMsg); echo($sMsg);
} else { } else {
$url = "https://api.telegram.org/bot$this->sApiToken/sendMessage?chat_id=$this->sApiChatId&text=" . urlencode($sMsg); $url = "https://api.telegram.org/bot$this->sApiToken/sendMessage?chat_id=$this->sApiChatId&text=" . urlencode($sMsg);
file_get_contents($url); file_get_contents($url);

View File

@ -52,6 +52,11 @@ class Http
// 默认使用 GET 请求 // 默认使用 GET 请求
curl_setopt($ch, CURLOPT_HTTPGET, true); // 设置为 GET 请求 curl_setopt($ch, CURLOPT_HTTPGET, true); // 设置为 GET 请求
curl_setopt($ch, CURLOPT_URL, $this->sToUrl . '?' . http_build_query($this->aData)); // 设置请求 URL 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; break;
} }