dd/app/Services/CacheTg.php
2026-07-27 18:46:48 +08:00

215 lines
7.0 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;
use App\Models\CacheTg as ModelCacheTg;
use App\Models\CacheTgTime as ModelCacheTgTime;
use App\Models\ArticleSchema as ModelArticleSchema;
use App\Services\TomTool\Flow;
use Illuminate\Support\Facades\Http;
//use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use App\Models\TelegramKey as ModelTelegramKey;
use Illuminate\Support\Str;
use App\Services\Article\Cmd as ServiceArticleCmd;
use App\Models\Option;
final class CacheTg
{
public function add($sContent, $sGroupCode)
{
$oFlow = Flow::start("CacheTg::add");
$oModelCacheTg = new ModelCacheTg();
$oModelCacheTg->content = $sContent;
$oModelCacheTg->group_code = $sGroupCode;
$r = $oModelCacheTg->save();
if (!$r) {
return $oFlow->fail("add失败");
}
$r = ModelCacheTgTime::updateOrCreate(
['group_code' => $sGroupCode], // 查找条件:有没有这个组?
['time' => time()] // 更新或创建的数据:就改这个时间!
);
if (!$r->exists) {
return $oFlow->fail("cache写入成功但time更新失败");
}
return $oFlow->done();
}
public static function cron_saveToSchema()
{
$sCronMsg = '';
$sCronMsg .= "\n\nCacheTg::saveToSchema() - 开始";
$bHaveTime = ModelCacheTgTime::where("time", ">", 0)->exists();
if (!$bHaveTime) {
$sCronMsg .= "\n - 没有活跃time正常跳出";
return $sCronMsg;
}
$iTimeNow = time();
$oModelCacheTgTime = ModelCacheTgTime::where("time", ">", 0)->first();
$iTimeDiff = $iTimeNow - $oModelCacheTgTime->time;
if ($iTimeDiff < $oModelCacheTgTime->delay) {
$sCronMsg .= "\n - time活跃但未到delay时间";
return $sCronMsg;
}
$oFlow = (new self())->saveToSchema($oModelCacheTgTime->group_code);
if ($oFlow->isFail()) {
$sCronMsg .= "\n - ".$oFlow->toJson();
return $sCronMsg;
}
$oModelCacheTgTime->time = 0;
$oModelCacheTgTime->save();
$sCronMsg .= "\n - ok";
return $sCronMsg;
}
public function saveToSchema($sGroupCode)
{
$oFlow = Flow::start();
$aSchema = [];
$aSchema["type"] = "cache_tg";
// $aSchema["group"] = $sGroupCode;
$aSchema["data"] = [];
$oModelCacheTg = ModelCacheTg::where("status", 1)->where("group_code", $sGroupCode)->get();
if ($oModelCacheTg->isEmpty()) {
return $oFlow->done();
}
$sUlid = (string) Str::ulid();
$sCode = $sGroupCode."-".$sUlid;
$sBotToken = ModelTelegramKey::botTokenByCode($sGroupCode);
// $sDocumentRoot = $_SERVER['DOCUMENT_ROOT'];
//// test
// $sBotToken = ModelTelegramKey::botTokenByCode("test");
//// test end
foreach ($oModelCacheTg as $oModelCacheTgRow) {
$sCacheContent = $oModelCacheTgRow->content;
$aCacheContent = json_decode($sCacheContent, true);
if (isset($aCacheContent["message"]["photo"])) {
$aSchemaRow = [];
$aSchemaRow["type"] = "photo";
$aSchemaRow["photo_fullSizeKey"] = 0;
$aSchemaRow["photo"] = [];
// $aSchemaRow["title"] = "xxx"; 意思一下,这么扩展
$aTgPhoto = $aCacheContent["message"]["photo"];
$iFullSize = 0;
foreach ($aTgPhoto as $k => $aTgPhotoRow) {
$aPhotoRow = $aTgPhotoRow;
$sFileId = $aTgPhotoRow["file_id"];
if ($aTgPhotoRow["file_size"] > $iFullSize) {
$iFullSize = $aTgPhotoRow["file_size"];
$aSchemaRow["photo_fullSizeKey"] = $k;
}
$oResponse = Http::get("https://api.telegram.org/bot{$sBotToken}/getFile", [
'file_id' => $sFileId
]);
if (!$oResponse->successful()) {
throw new \Exception("获取路径失败:" . $oResponse->body());
}
$sFileName = $oResponse->json('result.file_path');
$sDownloadUrl = "https://api.telegram.org/file/bot{$sBotToken}/{$sFileName}";
$sFileContents = Http::get($sDownloadUrl)->body();
$sFileContents = Http::get($sDownloadUrl)
->throw()
->body();
$sFileNameBase = basename($sFileName);
$sFileSaveName = $sCode."-".$sFileNameBase;
$sLocalPath = public_path()."/upload/" . $sFileSaveName;
$b = File::put($sLocalPath, $sFileContents);
if (!$b) {
throw new \Exception("文件保存失败:" . $sLocalPath);
}
$aPhotoRow["file_path"] = $sLocalPath;
unset($aPhotoRow["file_id"]);
unset($aPhotoRow["file_unique_id"]);
$aSchemaRow["photo"][] = $aPhotoRow;
}
$aSchema["data"][] = $aSchemaRow;
} else {
// 不在预设里的,先不管
}
}
$oModelArticleSchema = new ModelArticleSchema();
$oModelArticleSchema->code = $sCode;
$oModelArticleSchema->schema = json_encode($aSchema);
$oModelArticleSchema->group_code = $sGroupCode;
$oModelArticleSchema->save();
$jDefaultThumbUse = Option::where("k", "cache_tg_new::default_thumb_use::{$sGroupCode}")->first()?->k;
if (!$aDefaultThumbUse) {
$jDefaultThumbUse = Option::where("k", "cache_tg_new::default_thumb_use")->first()?->k;
}
$aDefaultThumbUse = json_decode($jDefaultThumbUse, true);
if (!$aDefaultThumbUse) {
$aDefaultThumbUse = ["c"];
}
$oModelArticleSchema->_opt([
"thumb_use" => $aDefaultThumbUse
]);
ServiceArticleCmd::startWithO($oModelArticleSchema)->syncOpen()->save("new");
$oModelCacheTg->each->update(['status' => 0, 'code' => $sCode]);
// $oModelCacheTg->each->update(['status' => 1, 'code' => $sCode]); // ! test
return $oFlow->done();
}
}