Files
jccp_a/laravel/app/Http/Controllers/Api/ArticleController.php
T

184 lines
6.0 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Services\Ranking\ArticleDocument;
use App\Services\Ranking\SitePublisher;
use App\Services\Ranking\WeeklyRanking;
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"] ?? '';
$publisher = new SitePublisher(dirname(base_path()), env('vue_path_node', 'node'));
$publisher->locked(function () use ($publisher) {
if ($this->sCmd == "del") {
$this->cmd_del();
} elseif ($this->sCmd == "new") {
$this->cmd_new();
} else {
throw new \Exception("未找到cmd的对应处理:".$this->sCmd);
}
// Updating availability never changes this week's membership or score.
(new WeeklyRanking($publisher))->renderCurrent();
$publisher->build();
});
$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"];
if (!preg_match('/^[A-Za-z0-9_:-]+$/', $this->sArticleCode)) {
throw new \RuntimeException('文章编号格式异常');
}
foreach ($aCategory as $sCategoryRow) {
if (!is_string($sCategoryRow) || $sCategoryRow === '' || preg_match('~[\\\\/]|^\.{1,2}$~', $sCategoryRow)) {
throw new \RuntimeException('文章分类格式异常');
}
$sFileDir = dirname(base_path())."/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);
}
}
}
public function cmd_new()
{
$categories = $this->aConfig['category'] ?? [];
$tags = $this->aConfig['tag'] ?? [];
$title = $this->aArg['article']['title'] ?? '⚠️ 未得到title';
$content = $this->aArg['article']['content'] ?? '⚠️ 未得到content';
$parts = explode('-', $this->sArticleCode, 2);
if (count($parts) !== 2 || !preg_match('/^[A-Za-z0-9_:-]+$/', $this->sArticleCode)) {
throw new \RuntimeException('文章编号格式异常');
}
foreach ($categories as $category) {
if (!is_string($category) || $category === '' || preg_match('~[\\\\/]|^\.{1,2}$~', $category)) {
throw new \RuntimeException('文章分类格式异常');
}
$path = dirname(base_path()).'/vuepress/docs/'.$category.'/'.$this->sArticleCode.'.md';
$existing = is_file($path) ? ArticleDocument::read($path)['meta'] : [];
$meta = $existing;
$meta['title'] = $title;
$meta['baseTitle'] = \App\Services\Ranking\Score::baseTitle($title);
$meta['tags'] = $tags;
$meta['createTime'] = $existing['createTime'] ?? $this->sDate;
$meta['permalink'] = $existing['permalink'] ?? '/'.$category.'/'.$parts[1].'/';
$meta['articleCode'] = $this->sArticleCode;
$document = ArticleDocument::enrich(['meta' => $meta, 'body' => $content]);
ArticleDocument::write($path, ArticleDocument::render($document));
$this->aNewPath[] = $document['meta']['title'].' -> new https://'.$_ENV['APP_URL'].$meta['permalink'];
}
}
}
//---
//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"
// }
//}