dd/app/Services/Cron/ArticleSchema.php
2026-03-15 12:18:31 +08:00

185 lines
7.1 KiB
PHP
Executable File
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\Cron;
use App\Models\SiteMap as ModelSiteMap;
use App\Models\SitePathMap as ModelSitePathMap;
use App\Models\ArticleConfig as ModelArticleConfig;
use App\Models\ArticleCache as ModelArticleCache;
use App\Models\ArticleSchema as ModelArticleSchema;
use App\Models\ArticleCacheTime as ModelArticleCacheTime;
use App\Services\Article\CacheTg as ServiceArticleCacheTg;
use App\Services\Article\Cmd as ServiceArticleCmd;
use App\Services\TomTool\HttpV2;
use App\Services\TomTool\Telegram\Slave as TeleSlave;
use App\Services\Article\Schema as ServiceArticleSchema;
final class ArticleSchema
{
public static function pushToSlave()
{
echo "\npushToSlave::开始\n";
$oArticleSchemaList = ModelArticleSchema::where("enable_sync", 1)->where("status", 1)->get();
if ($oArticleSchemaList->isEmpty()) {
echo " - 没有需要同步的文章\n";
}
foreach ($oArticleSchemaList as $oArticleSchema) {
$aCmdList = json_decode($oArticleSchema->cmd ?? [], true) ?? [];
foreach ($aCmdList as $sCmdKey => $aCmd) {
// 得值
$sSiteCode = $aCmd["site_code"] ?? "none";
$oSite = ModelSiteMap::where("code", $sSiteCode)->first();
if (!$oSite) {
$sMsg = "siteMap里的code没有映射{$sSiteCode}\n";
TeleSlave::warn()->send($sMsg);
echo $sMsg;
continue;
}
$sSiteGroupCode = $oSite->group_code;
$sSiteUrlBase = $oSite->url;
$sSiteArticleApiPath = ModelSitePathMap::where("site_group_code", $sSiteGroupCode)->where("path_group_code", "article_receive_schema")->first()?->path;
if (!$sSiteArticleApiPath) {
$sMsg = "sitePathMap里没找到映射{$sSiteGroupCode}:article_receive_schema\n";
TeleSlave::warn()->send($sMsg);
echo $sMsg;
continue;
}
$sSiteArticleApiUrl = "https://".$sSiteUrlBase.$sSiteArticleApiPath;
// 直接附加raw就该
$aCmd["_raw"] = $oArticleSchema->toArray();
// 附加常规
$aCmd["key"] = $sCmdKey;
$aCmd["article_code"] = $oArticleSchema->code;
$aCmd["group_code"] = $oArticleSchema->group_code;
$aCmd["article_opt"] = json_decode($oArticleSchema->opt ?? '', true) ?? [];
// 附加config
$sArticleConfig = ModelArticleConfig::where("article_group_code", $oArticleSchema->group_code)->where("site_group_code", $sSiteGroupCode)->first()?->config ?? '';
$aArticleConfig = json_decode($sArticleConfig, true);
$aCmd["config"] = $aArticleConfig;
// 按需要附加其他
if ($aCmd["cmd"] == "new") {
$oArticleFlow = ServiceArticleSchema::start($oArticleSchema->code, $sSiteCode)->build(); // 关键
if ($oArticleFlow->isFail()) {
$sMsg = "文章build失败";
TeleSlave::warn()->send([
"msg" => $sMsg,
"result"=> $oArticleFlow->getResult()
]);
echo $sMsg;
continue;
}
$aCmd["article"] = $oArticleFlow->getDataA(); // 关键
}
// 发送
$sHttpResult = HttpV2::make($sSiteArticleApiUrl)->withToken()->enableAsync(false)->enableJsonSend()->setDataA($aCmd)->send();
$aHttpResult = json_decode($sHttpResult, true);
if (config("app.is_loc") == true) {
// echo $sHttpResult;exit; //// test
}
$bHttpIsDone = $aHttpResult["isDone"] ?? false;
if (!$bHttpIsDone) {
$sMsg = "slave那边同步失败-> {$sHttpResult}";
TeleSlave::warn()->send([
"msg" => $sMsg,
"url" => $sSiteArticleApiUrl,
"result" => $sHttpResult,
"resultA"=> $aHttpResult,
]);
echo $sMsg;
continue;
}
$oCmdFLow = ServiceArticleCmd::startWithO($oArticleSchema)->moveToLog($sCmdKey);
if ($oCmdFLow->isFail()) {
$sMsg = "movetolog失败";
TeleSlave::warn()->send([
"msg" => $sMsg,
"result" => $oCmdFLow->getResult(),
]);
echo $sMsg;
continue;
}
echo($sHttpResult);
//// 丑陋缝补一下
if (data_get($oArticleSchema, 'group_code') === "ffq_tg_a") {
TeleSlave::notify()->send($aHttpResult["aData"]["aNewPath"]);
} else {
TeleSlave::notify()->send();
}
}
}
echo "ok\n";
}
public static function cacheTgSave()
{
echo "\n\ncacheTgSave::开始\n";
$iCacheTimeDelay = 60; // 延迟5分钟
$oCacheTime = ModelArticleCacheTime::first();
$iCacheTime = $oCacheTime->time;
if ($iCacheTime == 0) {
echo " - 没有新cache的time跳过\n";
return false;
}
$iTimeDiff = time() - $iCacheTime;
if ($iTimeDiff < $iCacheTimeDelay) {
echo " - 有缓存但没到更新时间\n";
return false;
}
$oSaveFlow = ServiceArticleCacheTg::start()->saveToSchema();
if ($oSaveFlow->isFail()) {
var_dump($oSaveFlow->getResult());
TeleSlave::warn()->send("cron的cacheTgSave处理失败:".json_encode($oSaveFlow->getResult()));
return false;
}
//// 如果cache里还存在未处理的就维持cron
//// schema那边一次只处理一种group_code等于就是多种group_code时维持cron一次处理一种
$b = ModelArticleCache::where('status', 1)->exists();
if (!$b) {
$oCacheTime->time = 0;
$oCacheTime->save();
} else {
echo " - 有其他group_code维持cron\n";
}
echo " - ok\n";
}
}