diff --git a/app/Http/Controllers/Api/TG/HookController.php b/app/Http/Controllers/Api/TG/HookController.php index 81ec09c..50308e7 100755 --- a/app/Http/Controllers/Api/TG/HookController.php +++ b/app/Http/Controllers/Api/TG/HookController.php @@ -67,7 +67,7 @@ final class HookController $aParams['aOption'] = $aOption; $aParams['aArg'] = $aArg; - $sClassPath = "App\\Services\\TG\\Hook\\".$sClass; + $sClassPath = "App\\Services\\TG\\Hook\\Mod\\".$sClass; if (method_exists($sClassPath, $sFunc)) { diff --git a/app/Http/Controllers/CronController.php b/app/Http/Controllers/CronController.php index 582d98a..8270591 100755 --- a/app/Http/Controllers/CronController.php +++ b/app/Http/Controllers/CronController.php @@ -4,8 +4,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Article; -//use App\Services\TomTool\Http; -//use voku\helper\AntiXSS; +use App\Services\TG\Http as TGHttp; class CronController { @@ -13,37 +12,61 @@ class CronController private $sUserName; private $sProjectName; + private $sProjectPath; + private $sProjectUrl; + +// public function __construct() +// { +// $this->sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName; +// $this->sProjectUrl = env("github_url_base").":".$this->sUserName."/".$this->sProjectName.".git"; +// } + public function index() { - - $h = date("H"); - $i = date("i"); - $s = date("s"); - - //test - $i = 7; - - - $aConditions = Article::select('cron_condition') - ->where('status', 1) - ->distinct() - ->get() - ->toArray(); + try { + + $h = date("H"); + $i = date("i"); + $s = date("s"); + + //test + $i = '03'; + + // i_in10规则 + $iIin10 = $i[1]; + $aArticle = Article::where("i_in10", $iIin10)->where("status", 1)->get()->toArray(); + + $this->run($aArticle); + + + $aConditions = Article::select('cron_condition') + ->where('status', 1) + ->distinct() + ->get() + ->toArray(); - foreach ($aConditions as $row) { - - $sCondition = $row['cron_condition']; - - if (eval("return $sCondition;")) { + foreach ($aConditions as $row) { - $aArticle = Article::where("cron_condition", $sCondition)->where("status", 1)->get()->toArray(); - - $this->run($aArticle); + $sCondition = $row['cron_condition']; + if (!empty($sCondition) && eval("return $sCondition;")) { + + $aArticle = Article::where("cron_condition", $sCondition)->where("status", 1)->get()->toArray(); + + $this->run($aArticle); + + } } + + } catch (\Throwable $e) { + + $sMessage = "错误: " . $e->getMessage(); + + $oTGHttp = new TGHttp(); + $oTGHttp->sendToTG($sMessage); } - + } private function run($aArticle) @@ -51,68 +74,130 @@ class CronController foreach ($aArticle as $row) { $this->sUserName = $row["user_name"]; $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"; list($sClass, $sFunc) = explode('#', $row['mod']); - $sClass = "App\\Services\\Github\\File\\".$sClass; + $sClass = "App\\Services\\Github\\Mod\\".$sClass; if (method_exists($sClass, $sFunc)) { $oInstans = new $sClass(); $sReadMe = $oInstans->$sFunc($row); -// var_dump($sReadMe); } else { // 处理方法不存在的情况(可选) // $this->tgMessage("Method $sClass does not exist."); } - // 目录 - $sBaseDir = env("BASE_DIR"); - $sUserProjectDir = $sBaseDir."github/".$this->sUserName."-".$this->sProjectName; - $sGitShDir = $sBaseDir."github"; - // 没有文件夹,创建一个 - if (!is_dir($sUserProjectDir)) { - mkdir($sUserProjectDir, 0755, true); // 0755 是权限,true 表示递归创建目录 + if (!is_dir($this->sProjectPath)) { + + mkdir($this->sProjectPath, 0755, true); // 0755 是权限,true 表示递归创建目录 + } // 没有仓库,创建一个 - if (!is_dir($sUserProjectDir . "/.git")) { + if (!is_dir($this->sProjectPath . "/.git")) { - $sGithubProjectUrl = env("github_url_base").":".$this->sUserName."/".$this->sProjectName.".git"; + $this->gitInit(); - $output = shell_exec("sh " . $sGitShDir . "/init.sh " . escapeshellarg($sUserProjectDir) . " " . escapeshellarg($sGithubProjectUrl) . " " . escapeshellarg($this->sUserName)); - - echo "Output from init.sh:\n$output\n"; - - $output = shell_exec("git config --global --add safe.directory $sUserProjectDir 2>&1"); -// -// echo "
Output from --add safa.directory:\n$output\n"; } // 写入内容 - file_put_contents($sUserProjectDir."/README.md", $this->formatstr($sReadMe)); - file_put_contents($sUserProjectDir."/version.text", time()); + file_put_contents($this->sProjectPath."/README.md", $this->formatstr($sReadMe)); + file_put_contents($this->sProjectPath."/version.text", time()); // 提交 - $output = shell_exec("sh ".$sGitShDir."/push.sh " . escapeshellarg($sUserProjectDir)); - echo "Output from push.sh:\n$output\n"; + $this->gitPush(); } } - private function tgMessage($sMsg) - { - if (env("is_loc") == true) { - echo $sMsg; - } else { - echo 11; - } - } - private function formatstr($str) { $str = substr($str, 1, -1); return $str; } + private function runCommand($command) { + $output = []; + $returnVar = 0; + exec($command, $output, $returnVar); + + if ($returnVar !== 0) { + // 将输出数组转换为 JSON 格式 + $jsonOutput = json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); + + $errorMessage = "命令失败: $command\n" . $jsonOutput . "\n"; + + throw new \Exception($errorMessage); + } + } + + + private function gitInit() + { + // 进入指定目录 + if (!chdir($this->sProjectPath)) { + throw new \Exception("无法进入目录 {$this->sProjectPath}\n"); + } + + // 初始化 Git 仓库 + $this->runCommand('git init'); + + // 添加安全目录 + $this->runCommand("git config --local --add safe.directory \"{$this->sProjectPath}\""); + + // 设置用户 + $this->runCommand("git config --local user.name \"{$this->sUserName}\""); + $this->runCommand("git config --local user.email \"{$this->sUserName}@tuta.io\""); + + // 创建并切换到 main 分支 + $this->runCommand('git branch -M main'); + + // 添加远程仓库 + $this->runCommand("git remote add origin \"{$this->sProjectUrl}\""); + + echo "操作完成:以初始化仓库。\n"; + + } + +// private function gitPullR() +// { +// // 进入指定目录 +// if (!chdir($this->sProjectPath)) { +// echo "无法进入目录 {$this->sProjectPath}\n"; +// exit(1); +// } +// +// // 获取远程更新 +// $this->runCommand('git fetch origin'); +// +// // 硬重置到远程主分支 +// $this->runCommand('git reset --hard origin/main'); +// +// echo "操作完成:已重置到远程主分支。\n"; +// } + + private function gitPush() + { + // 进入指定目录 + if (!chdir($this->sProjectPath)) { + echo "无法进入目录 {$this->sProjectPath}\n"; + exit(1); + } + + // 添加更改到暂存区 + $this->runCommand('git add .'); + + // 提交更改,使用当前日期作为提交信息 + $commitMessage = date('Y-m-d H:i:s'); // 使用当前日期和时间作为提交信息 + $this->runCommand("git commit -m \"$commitMessage\""); + + // 推送到远程主分支 + $this->runCommand('git push -u origin main'); // 确保将 'main' 替换为你的默认分支名称 + + echo "操作完成:已提交并推送更改。\n"; + } + } diff --git a/app/Http/Controllers/TestsController.php b/app/Http/Controllers/TestsController.php index 7ba6eb5..f2371d2 100755 --- a/app/Http/Controllers/TestsController.php +++ b/app/Http/Controllers/TestsController.php @@ -24,7 +24,7 @@ class TestsController //test // $s = "\/AticleSet__1115zz__sale_rate 1118.118"; - $s = "\/sdjkf"; + $s = "\/git#pullR 1"; $oHttp = new Http(); $oHttp->sMethod = "post"; diff --git a/app/Services/Github/File/FreeNode.php b/app/Services/Github/Mod/FreeNode.php similarity index 94% rename from app/Services/Github/File/FreeNode.php rename to app/Services/Github/Mod/FreeNode.php index 198c178..770264e 100755 --- a/app/Services/Github/File/FreeNode.php +++ b/app/Services/Github/Mod/FreeNode.php @@ -1,5 +1,5 @@ '/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", - ]; - - $this->oTGHttp->sendToTG(json_encode($aCmdList, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); - - $this->oHttp::response(200, 'ok'); - - } - -} diff --git a/app/Services/TG/Hook/Git.php b/app/Services/TG/Hook/Git.php deleted file mode 100644 index a69f54e..0000000 --- a/app/Services/TG/Hook/Git.php +++ /dev/null @@ -1,52 +0,0 @@ -oTGHttp->sendToTG($sMsg); - - $this->oHttp::response(200, 'ok'); - - } - - // /Git#PullR 3 - public function PullR($aParam) - { - - $iId = $aParam['aArg'][1]; - - $aArticle = ModelArticle::find($iId); - - tomd($aArticle, 1); - - } - -} diff --git a/app/Services/TG/Hook/Article.php b/app/Services/TG/Hook/Mod/Article.php similarity index 98% rename from app/Services/TG/Hook/Article.php rename to app/Services/TG/Hook/Mod/Article.php index b984d7b..57c500c 100644 --- a/app/Services/TG/Hook/Article.php +++ b/app/Services/TG/Hook/Mod/Article.php @@ -1,6 +1,6 @@ '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->oTGHttp->sendToTG(json_encode($aCmdList, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); + + $this->oHttp::response(200, 'ok'); + + } + +} diff --git a/app/Services/TG/Hook/Mod/Git.php b/app/Services/TG/Hook/Mod/Git.php new file mode 100644 index 0000000..8452ad4 --- /dev/null +++ b/app/Services/TG/Hook/Mod/Git.php @@ -0,0 +1,110 @@ +oTGHttp->sendToTG($sMsg); + + $this->oHttp::response(200, 'ok'); + + } + + public function Clone($aParam) + { + $iId = $aParam['aArg'][1]; + + // 查找文章 + $aArticle = ModelArticle::find($iId); + + if ($aArticle) { + // 复制文章 + $newArticle = $aArticle->replicate(); + + // 如果需要,可以修改新文章的某些字段,例如标题或状态 +// $newArticle->title = $newArticle->title . ' - Copy'; // 修改标题 + // $newArticle->status = 'draft'; // 例如,将状态设置为草稿 + + // 保存新文章 + $newArticle->save(); + + // 发送新文章的 ID 到 Telegram + $sMsg = "新文章已创建,ID: " . $newArticle->id; + $this->oTGHttp->sendToTG($sMsg); + + // 返回 HTTP 响应 + $this->oHttp::response(200, 'ok'); + } else { + throw new \Exception("没有这个id"); + } + } + + // /Git#PullR 3 + public function PullR($aParam) + { + + $iId = $aParam['aArg'][1]; + + $aArticle = ModelArticle::find($iId); + + $sProjectPath = env("BASE_DIR")."github/".$aArticle->user_name."-".$aArticle->project_name; + + // 进入指定目录 + if (!chdir($sProjectPath)) { + echo "无法进入目录 {$sProjectPath}\n"; + exit(1); + } + + // 获取远程更新 + $this->runCommand('git fetch origin'); + + // 硬重置到远程主分支 + $this->runCommand('git reset --hard origin/main'); + + echo "操作完成:已重置到远程主分支。\n"; + + } + + private function runCommand($command) { + $output = []; + $returnVar = 0; + exec($command, $output, $returnVar); + + if ($returnVar !== 0) { + // 将输出数组转换为 JSON 格式 + $jsonOutput = json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); + + $errorMessage = "命令失败: $command\n" . $jsonOutput . "\n"; + + throw new \Exception($errorMessage); + } + } + +} diff --git a/app/Services/TG/Hook/SaleCode.php b/app/Services/TG/Hook/Mod/SaleCode.php similarity index 97% rename from app/Services/TG/Hook/SaleCode.php rename to app/Services/TG/Hook/Mod/SaleCode.php index 834e8b5..a638ae8 100644 --- a/app/Services/TG/Hook/SaleCode.php +++ b/app/Services/TG/Hook/Mod/SaleCode.php @@ -1,6 +1,6 @@ sApiToken = env('telegram_token'); + $this->sApiChatId = env('telegram_chatid'); + } + public function sendToTG($sMsg) { diff --git a/github/init.sh b/github/init.sh deleted file mode 100755 index 5a824d7..0000000 --- a/github/init.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash - -# 检查是否提供了目录和远程仓库地址参数 -if [ -z "$1" ] || [ -z "$2" ]; then - echo "请提供要操作的目录和远程仓库地址." - exit 1 -fi - -# 进入指定目录 -cd "$1" || { echo "无法进入目录 $1"; exit 1; } - -# 初始化 Git 仓库 -git init - -# 添加安全目录 -git config --local --add safe.directory "$1" - -# 设置用户 -git config --local user.name "$3" -git config --local user.email "$3"."@tuta.io" - -# 添加文件到暂存区 -git add . - -# 提交第一次更改 -git commit -m "first commit" - -# 创建并切换到 main 分支 -git branch -M main - -# 添加远程仓库 -git remote add origin "$2" - -# 推送到远程仓库 -# git push -u origin main diff --git a/github/pull.sh b/github/pull.sh deleted file mode 100755 index 56ea510..0000000 --- a/github/pull.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -cd $1 - -git fetch origin - -git reset --hard origin/main diff --git a/github/push.sh b/github/push.sh deleted file mode 100755 index 131023f..0000000 --- a/github/push.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -cd $1 - -git add . - -git commit -m "$(date)" - -git push origin main # 确保将 'main' 替换为你的默认分支名称 - diff --git a/github/testgeigei-test b/github/testgeigei-test deleted file mode 160000 index 689aab7..0000000 --- a/github/testgeigei-test +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 689aab7911bc0911dc5491ff51167f1016d550ad diff --git a/github/testgeigei-ttt b/github/testgeigei-ttt deleted file mode 160000 index fad2d8f..0000000 --- a/github/testgeigei-ttt +++ /dev/null @@ -1 +0,0 @@ -Subproject commit fad2d8f73aea5dbb8783f265b426971af2aafad5 diff --git a/github/testgeigei-xiahe2 b/github/testgeigei-xiahe2 deleted file mode 160000 index f76ea02..0000000 --- a/github/testgeigei-xiahe2 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f76ea021e2dab961b8e727e208dbde6a4ef7876a diff --git a/github/xiahe2-xiahe2 b/github/xiahe2-xiahe2 deleted file mode 160000 index c10878f..0000000 --- a/github/xiahe2-xiahe2 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c10878f28055ed1dfd1863870829015c3f9be576