166 lines
6.1 KiB
PHP
166 lines
6.1 KiB
PHP
<?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\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->toArray() as $R) {
|
||
// unset($R["schema"]);
|
||
// tomd($R);
|
||
// }
|
||
|
||
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;
|
||
|
||
// 附加常规
|
||
$aCmd["key"] = $sCmdKey;
|
||
$aCmd["article_code"] = $oArticleSchema->code;
|
||
|
||
|
||
// 附加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)->enableJsonSend()->setDataA($aCmd)->send();
|
||
$aHttpResult = json_decode($sHttpResult, true);
|
||
|
||
$bHttpIsDone = $aHttpResult["isDone"] ?? false;
|
||
|
||
if (!$bHttpIsDone) {
|
||
$sMsg = "slave那边同步失败?";
|
||
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;
|
||
}
|
||
|
||
TeleSlave::notify()->send($aHttpResult["aData"]["aNewPath"]);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
echo "ok\n";
|
||
}
|
||
|
||
public static function cacheTgSave()
|
||
{
|
||
|
||
echo "\ncacheTgSave::开始\n";
|
||
$iCacheTimeDelay = 1800; // 延迟20分钟
|
||
|
||
$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;
|
||
}
|
||
|
||
$oCacheTime->time = 0;
|
||
$oCacheTime->save();
|
||
|
||
echo " - ok\n";
|
||
}
|
||
|
||
}
|