jccp_a/laravel/app/Http/Controllers/Api/ArticleController.php
2025-11-22 20:18:12 +08:00

232 lines
6.6 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Api;
use App\Services\TomTool\Flow;
use App\Services\TomTool\HttpV2;
use App\Services\TomTool\Telegram\Slave as TeleSlave;
use App\Services\TomTool\ThrowableHandler;
class ArticleController
{
public $sCmd = '';
public $sValue = '';
public $sArticleCode = '';
public $sSiteCode = '';
public $sKey = '';
public $aConfig = [];
public $aArg = [];
public $sDate = '';
public $aNewPath = [];
public function receiveSchema()
{
$oFlow = Flow::start("receiveSachema");
try {
$sArg = file_get_contents('php://input');
$aArg = json_decode($sArg, true);
$this->aArg = $aArg;
$this->sDate = $aArg["date"] ?? '';
$this->sCmd = $aArg["cmd"] ?? '';
$this->sValue = $aArg["value"] ?? '';
$this->sArticleCode = $aArg["article_code"] ?? '';
$this->sSiteCode = $aArg["site_code"] ?? '';
$this->sKey = $aArg["key"] ?? '';
$this->aConfig = $aArg["config"] ?? '';
if ($this->sCmd == "del") {
$this->cmd_del();
} else if ($this->sCmd == "new") {
$this->cmd_new();
} else {
$sMsg = "未找到cmd的对应处理".$this->sCmd;
echo $sMsg;
throw new \Exception($sMsg);
}
$this->_npmBuild();
$arr["aNewPath"] = $this->aNewPath;
echo json_encode($oFlow->setDataA($arr)->done()->getResult());
exit;
} catch (\Throwable $e) {
TeleSlave::warn()->enableSlaveQueue(false)->send($e->getMessage());
echo json_encode($oFlow->fail($e->getMessage())->getResult());
exit;
}
}
private function cmd_del()
{
$aCategory = $this->aConfig["category"] ?? ["⚠️ 未得到category"];
foreach ($aCategory as $sCategoryRow) {
$sFileDir = "../../vuepress/docs/{$sCategoryRow}/";
$sFileName = $this->sArticleCode.".md";
$sFilePath = $sFileDir.$sFileName;
if (is_file($sFilePath) && file_exists($sFilePath)) {
if (unlink($sFilePath)) {
$this->aNewPath[] = $_ENV["APP_URL"]."::".$this->sArticleCode."删除成功";
} else {
$sMsg = '删除失败(可能是权限或被占用)';
throw new \Exception($sMsg);
}
} else {
$sMsg = '文件不存在或不是普通文件';
throw new \Exception($sMsg);
}
}
}
private function _npmBuild()
{
$sVuePathNode = $_ENV['vue_path_node'];
$sVuePath = base_path();
$sVuePath = dirname($sVuePath);
$sVuePath .= "/vuepress";
$sCmd = "cd {$sVuePath} && {$sVuePathNode} ./node_modules/.bin/vuepress build docs";
exec($sCmd . " 2>&1", $output, $returnVar);
$logFile = $sVuePath . "/build.log";
file_put_contents($logFile, implode("\n", $output) . "\n", FILE_APPEND);
if ($returnVar === 0) {
// echo " - VuePress 构建成功!静态文件已生成在 docs/.vuepress/dist\n";
} else {
$sMsg = "VuePress构建失败请查看日志: ".json_encode($output);
throw new \Exception($sMsg);
}
}
public function cmd_new()
{
$aCategory = $this->aConfig["category"] ?? ["⚠️ 未得到category"];
$aTag = $this->aConfig["tag"] ?? ["⚠️ 未得到tag"];
$sTitle = $this->aArg["article"]["title"] ?? "⚠️ 未得到title";
$sContent = $this->aArg["article"]["content"] ?? "⚠️ 未得到content";
$sEnter = "
";
list(, $iCode) = explode("-", $this->sArticleCode);
foreach ($aCategory as $sCategoryRow) {
$sHead = "---";
$sHead .= $sEnter;
$sHead .= "title: ".$sTitle;
$sHead .= $sEnter;
$sHead .= "tags: ";
$sHead .= $sEnter;
foreach ($aTag as $sTagRow){
$sHead .= " - ".$sTagRow;
$sHead .= $sEnter;
}
$sHead .= "createTime: ".$this->sDate;
$sHead .= $sEnter;
$sTmp = "/{$sCategoryRow}/".$iCode."/";
$this->aNewPath[] = $sTitle." -> new https://".$_ENV["APP_URL"].$sTmp;
$sHead .= "permalink: ".$sTmp;
$sHead .= $sEnter;
$sHead .= "---";
$sArticleFull = $sHead;
$sArticleFull .= $sContent;
$sFileDir = "../../vuepress/docs/{$sCategoryRow}/";
$sFileName = $this->sArticleCode.".md";
$sFilePath = $sFileDir.$sFileName;
if (!file_exists($sFileDir)) {
if (mkdir($sFileDir, 0777, true)) {
// $oFlow->step("文件夹创建成功:$sFileDir\n");
} else {
$sMsg = "创建失败,请检查权限。\n";
echo $sMsg;
throw new \Exception($sMsg);
}
}
$fileHandle = fopen($sFilePath, 'w');
if ($fileHandle === false) {
$sMsg = "错误:无法打开或创建文件 $sFilePath。";
echo $sMsg;
throw new \Exception($sMsg);
} else {
fwrite($fileHandle, $sArticleFull);
fclose($fileHandle);
}
}
}
}
//---
//title: 自定义组件
//tags:
// - 预览
// - 组件
//createTime: 2025/10/19 17:47:57
//permalink: /demo/zzyg5nqpwh/
//---
//array(8) {
// ["site_code"]=>
// string(4) "ttcp"
// ["cmd"]=>
// string(3) "new"
// ["value"]=>
// string(0) ""
// ["date"]=>
// string(19) "2025-11-09 17:30:33"
// ["key"]=>
// string(28) "|jccp_a|ttcp|20251109173033|"
// ["article_code"]=>
// string(24) "ffq_tg_a::20251105175210"
// ["config"]=>
// array(2) {
// ["category"]=>
// array(2) {
// [0]=>
// string(12) "每日测速"
// [1]=>
// string(6) "测评"
// }
// ["tag"]=>
// array(2) {
// [0]=>
// string(2) "xx"
// [1]=>
// string(2) "cc"
// }
// }
// ["article"]=>
// array(2) {
// ["title"]=>
// string(37) "CPP Zone 机场第6次测评 & 测速"
// ["content"]=>
// string(2897) "CPP Zone"
// }
//}