dd/app/Services/Article/Schema.php
2025-11-14 15:06:20 +08:00

282 lines
10 KiB
PHP
Raw 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
declare(strict_types=1);
namespace App\Services\Article;
use App\Models\ArticleSchema as ModelArticleSchema;
use App\Models\SiteMap as ModelSiteMap;
use App\Models\Option as ModelOption;
use App\Services\TomTool\Telegram\Slave as TeleSlave;
use App\Services\TomTool\Flow;
//use App\Services\Article\Cmd as ServiceArticleCmd;
use App\Models\DogOption as ModelDogOption;
final class Schema
{
// 必须
public string $sArticleCode;
public string $sSiteCode;
// 自动必有
public object $oArticle;
public string $sSiteGroupCode;
public string $sSiteName;
public string $sSiteUrl;
// public string $aArticleConfig;
// 调试
public bool $isTest = false;
public static function start($sArticleCode, $sSiteCode)
{
$oArticleSchema = ModelArticleSchema::where("code", $sArticleCode)->first();
$oSiteMap = ModelSiteMap::where("code", $sSiteCode)->first();
$sSiteGroupCode = $oSiteMap->group_code;
$sSiteName = $oSiteMap->name;
// $sArticleConfig = ModelArticleConfig::where("article_group_code", $oArticleSchema->group_code)->where("site_group_code", $sSiteGroupCode)->first()?->config;
// $this->aArticleConfig = json_decode($sArticleConfig, true);
$oInstance = new self();
$oInstance->sArticleCode = $sArticleCode;
$oInstance->sSiteCode = $sSiteCode;
$oInstance->oArticle = $oArticleSchema;
$oInstance->sSiteGroupCode = $oSiteMap->group_code;
$oInstance->sSiteName = $oSiteMap->name;
$oInstance->sSiteUrl = $oSiteMap->url;
// $oInstance->sArticleConfig = $sArticleConfig;
return $oInstance;
}
public function build()
{
$sMethod = "build_article__".$this->sSiteGroupCode;
if (method_exists($this, $sMethod)) {
$oArticleFlow = $this->$sMethod();
} else {
return Flow::start("build")->fail("没有".$sMethod."的对应处理");
}
return $oArticleFlow;
}
// 废弃,改直接推了
public function api_buildArticle()
{
$oFlow = Flow::start("api_buildArticle");
$sArg = file_get_contents('php://input');
$aArg = json_decode($sArg, true);
$sArticleCode = $aArg[""] ?? '';
$sSiteCode = $aArg["sSiteCode"] ?? ''; // ffcp
$sSiteName = $aArg["sSiteName"] ?? ''; // ttcp
$sSiteUrl = $aArg["sSiteUrl"] ?? ''; // ttcp.com
$isTest = $aArg["isTest"] ?? false;
$sSiteGroupCode = ModelSiteMap::where("code", $sSiteCode)->first()?->group_code;
$oArticleSchema = ModelArticleSchema::where("code", $sArticleCode)->first();
$aArg = [
"sArticleCode" => $sArticleCode,
"sSiteGroupCode"=> $sSiteGroupCode,
"sSiteCode" => $sSiteCode,
"sSiteName" => $sSiteName,
"sSiteUrl" => $sSiteUrl,
"isTest" => $isTest,
"aSchema" => json_decode($oArticleSchema->schema, true),
];
$sMethod = "build_article__".$sSiteGroupCode;
if (method_exists($this, $sMethod)) {
$oArticleFlow = $this->$sMethod($aArg);
} else {
$aResult = $oFlow->fail("没有".$sMethod."的对应处理")->getResult();
echo json_encode($aReturn);
exit;
}
echo json_encode($oArticleFlow->getResult());
exit;
}
private function build_article__jccp_a()
{
$oFlow = Flow::start("build_article__cp_a");
$sArticleCode = $this->sArticleCode;
$sSiteGroupCode = $this->sSiteGroupCode;
$sSiteCode = $this->sSiteCode;
$sSiteName = $this->sSiteName;
$sSiteUrl = $this->sSiteUrl;
$aSchema = json_decode($this->oArticle->schema, true);
$isTest = $this->isTest;
$aArticle["data"] = [];
foreach ($aSchema as $k => $aArticleSchemaRow) {
$sContent = $aArticleSchemaRow["content"]["base"] ?? "⚠️ 没有content";
if ($aArticleSchemaRow['type'] == 'str') {
if ($aArticleSchemaRow['name'] == "schema_name") {
$sSchemaName = $aArticleSchemaRow["content"]["base"] ?? "⚠️ 提取name失败";
$aArticle["name"] = $sSchemaName;
$aArticle["title"] = $this->nameToTitleCS($sSchemaName);
}
$sContent = str_replace("{{sAuthorName}}", $sSiteName, $sContent);
$sContent = str_replace("{{sIconText}}", "### <img style='height:16px;' src='/icon.png'> ", $sContent);
$aArticleSchemaRow["content"]["base"] = $sContent;
$aArticle["data"][] = $aArticleSchemaRow;
} else if ($aArticleSchemaRow['type'] == 'img') {
$sContent = "<img class='img_cesu' src='".$aArticleSchemaRow["content"]["img"]["base"]."'>";
$aArticleSchemaRow["content"]["base"] = $sContent;
$aArticle["data"][] = $aArticleSchemaRow;
} else {
$aArticle["data"][] = $aArticleSchemaRow;
}
}
////// step 2 排序
//// 找排序option没有就走默认
$sOrderKey = "article::schema::order::{$sSiteCode}::name";
$sOrderName = ModelOption::_hit($sOrderKey) ?? '';
$aOrderName = explode(',', $sOrderName);
if (!$sOrderName) {
TeleSlave::notify()->send("未设置option".$sOrderKey."使用默认。");
$sDefaultOrderKey = "article::schema::order::default::type";
$sDefaultOrderType = ModelOption::_hit($sDefaultOrderKey) ?? '';
$aDefaultOrderType = explode(',', $sDefaultOrderType);
if (!$sDefaultOrderType) {
TeleSlave::fail()->send("未设置option".$sOrderKey."使用默认。");
return $oFlow->fail("未设置option的default排序");
}
}
if ($sOrderName) {
$aArticle["dataOrder"] = $this->articleSort($aArticle["data"], "name", $aOrderName);
} else {
$aArticle["dataOrder"] = $this->articleSort($aArticle["data"], "type", $aDefaultOrderType);
}
////// step 3 文章化
$aArticleOrder = $aArticle["dataOrder"];
$sEnter = "
";
$sArticle = '';
foreach ($aArticleOrder as $aArticleOrderRow) {
$sContent = $aArticleOrderRow["content"]["base"] ?? "⚠️ content里的base没有";
$sContent = trim($sContent);
$sContent = nl2br($sContent);
$sContent = str_replace("<br />", " ", $sContent);
$sTmpName = $aArticleOrderRow["name"] ?? '';
if ($sTmpName == "str_jieshao") {
$sContent = preg_replace('/套餐.*'.$sEnter.'/', '', $sContent);
$sContent = preg_replace('/论坛.*'.$sEnter.'/', '', $sContent);
$sArticle .= $sEnter;
$sArticle .= $sEnter;
$sArticle .= "## 机场介绍";
$sArticle .= $sEnter;
$sArticle .= $sEnter;
} else if ($sTmpName == "table_taocan") {
$sArticle .= $sEnter;
$sArticle .= $sEnter;
$sArticle .= "## 套餐价格";
$sArticle .= $sEnter;
$sArticle .= $sEnter;
} else if ($sTmpName == "img_cesu") {
$sArticle .= $sEnter;
$sArticle .= $sEnter;
$sArticle .= "## 节点测试";
$sArticle .= $sEnter;
$sArticle .= $sEnter;
} else if ($sTmpName == "tag_tiqu") {
$sArticle .= $sEnter;
$sArticle .= $sEnter;
} else {
$sArticle .= $sEnter;
$sArticle .= $sEnter;
$sArticle .= "## 其他";
$sArticle .= $sEnter;
$sArticle .= $sEnter;
}
$sArticle .= $sContent;
}
////// step 5 返回 通知提取
$aResultData = [
"title" => $aArticle["title"] ?? "⚠️ 没有titile",
"content" => $sArticle,
];
return $oFlow->setDataA($aResultData)->done();
}
private function articleSort(array $data, string $field, array $priorityOrder): array
{
// 保留原始键名
uasort($data, function ($a, $b) use ($field, $priorityOrder) {
$aValue = $a[$field] ?? null;
$bValue = $b[$field] ?? null;
$aIndex = array_search($aValue, $priorityOrder);
$bIndex = array_search($bValue, $priorityOrder);
// 如果找不到,排在最后
$aIndex = $aIndex === false ? PHP_INT_MAX : $aIndex;
$bIndex = $bIndex === false ? PHP_INT_MAX : $bIndex;
return $aIndex <=> $bIndex;
});
return $data;
}
private function nameToTitleCS($sName)
{
$sTitle = $sName;
$iCesuCount = ModelArticleSchema::where("name", $sName)->count();
if ($iCesuCount > 1) {
$sWrap = ModelDogOption::_hit("article", "ffq_tg-title_wrap");
if (!$sWrap) {
return "未设置dog_option::ffq_tg-title_wrap";
}
$sTitle = str_replace('{{name}}', $sName, $sWrap);
$sTitle = str_replace('{{cesu_count}}', (string) $iCesuCount, $sTitle);
} else {
$sWrap = ModelDogOption::_hit("article", "ffq_tg-title_wrap-first");
if (!$sWrap) {
return "未设置dog_option::ffq_tg-title_wrap-first";
}
$sTitle = str_replace('{{name}}', $sName, $sWrap);
}
return $sTitle;
}
}