sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName; // $this->sProjectUrl = env("github_url_base").":".$this->sUserName."/".$this->sProjectName.".git"; // } public function index() { try { $h = date("H"); $i = date("i"); $s = date("s"); //test $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]; $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 (!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) { 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"; $this->aArticle = $row; list($sClass, $sFunc) = explode('#', $row['mod']); $sClass = "App\\Services\\Github\\Mod\\".$sClass; if (method_exists($sClass, $sFunc)) { $oInstans = new $sClass(); $sReadMe = $oInstans->$sFunc($row); } else { // 处理方法不存在的情况(可选) // $this->tgMessage("Method $sClass does not exist."); } // 没有文件夹,创建一个 if (!is_dir($this->sProjectPath)) { mkdir($this->sProjectPath, 0755, true); // 0755 是权限,true 表示递归创建目录 } // 没有仓库,创建一个 if (!is_dir($this->sProjectPath . "/.git")) { $this->gitInit(); } // 写入内容 file_put_contents($this->sProjectPath."/README.md", $this->formatstr($sReadMe)); file_put_contents($this->sProjectPath."/version.text", time()); // 提交 $this->gitPush(); } } 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) { $aMsg = [ "命令" => $command, "结果" => $output, "文章" => $this->aArticle, ]; throw new \Exception(json_encode($aMsg, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); } } 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)) { throw new \Exception("无法进入目录 {$this->sProjectPath}\n"); } // 添加更改到暂存区 $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"; } }