dd/app/Services/Tg/Hook/Mod/ArticleSchema.php
2026-01-02 19:10:22 +08:00

152 lines
4.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
namespace App\Services\Tg\Hook\Mod;
use App\Models\ArticleCache as ModelArticleCache;
use App\Models\ArticleSchema as ModelArticleSchema;
use App\Services\Tg\Hook\Dog;
//use App\Services\Article\CacheTg as ServiceArticleCacheTg;
use App\Services\Article\Schema as ServiceArticleSchema;
use App\Services\Article\Cmd as ServiceArticleCmd;
use App\Services\TomTool\Flow;
use App\Services\TomTool\ThrowableHandler;
class ArticleSchema extends Base {
public function __construct($aUpdate)
{
$this->oDog = new Dog(new ModelArticleSchema());
$this->oDog->_setDogName("articleSchema");
$this->oDog->_setStrField(["schema"]);
$this->aUpdate = $aUpdate;
}
public function __call($sName, $aArg)
{
return $this->oDog->$sName($aArg[0]);
}
public function last()
{
$iLastId = ModelArticleSchema::where("status", 1)->orderBy("id", "desc")->first()?->id;
$aParam = [];
$aParam["aArg"][1] = $iLastId;
$r = $this->find($aParam);
return $r;
}
public function show($aParam)
{
$xId = $aParam["aArg"][1] ?? "last";
$sShowField = $aParam["aOption"][1] ?? "schema";
if ($xId == "?") {
return "#show__[field:content] [id:last]";
}
if ($xId == "last") {
$oArticle = ModelArticleSchema::orderBy("id", "desc")->first();
} else {
$oArticle = ModelArticleSchema::find($xId);
}
if (!$oArticle) {
return "没找到文章";
}
$sMsg = "";
if ($sShowField == "schema") {
$aContentFormat = json_decode($oArticle->schema, true);
$sMsg = json_encode($aContentFormat, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
} else {
$sMsg = $oArticle->$sShowField;
}
return $sMsg;
}
public function del_slave($aParam)
{
$iId = $aParam["aArg"][1] ?? false;
if (!$iId) {
return "没有id";
}
if ($iId == "?") {
return "#del [id]";
}
$oArticleSchema = ModelArticleSchema::where("id", $iId)->first();
$r = ServiceArticleCmd::startWithO($oArticleSchema)->syncOpen()->save("del");
return $r->getResult();
}
public function del_self($aParam)
{
$iId = $aParam["aArg"][1] ?? false;
if (!$iId) {
return "没有id";
}
if ($iId == "?") {
return "#del [id]";
}
$oArticleSchema = ModelArticleSchema::where("id", $iId)->first();
$aSchema = json_decode($oArticleSchema->schema, true);
$i = 0;
foreach ($aSchema as $aSchemaRow) {
if ($aSchemaRow["type"] == "img") {
$sImgA = $this->imgPathByUrl($aSchemaRow["content"]["img"]["base"]);
$sImgB = $this->imgPathByUrl($aSchemaRow["content"]["img"]["thumb"]);
$i += $this->imgDelByPath($sImgA);
$i += $this->imgDelByPath($sImgB);
}
}
$oArticleSchema->delete();
$sMsg = "ok同时删除了{$i}张图片。";
return $sMsg;
}
private function imgPathByUrl($sImgUrl)
{
list(, $sImgPath) = explode("upload", $sImgUrl, 2);
$sImgPath = "upload".$sImgPath;
return $sImgPath;
}
private function imgDelByPath($sFilePath)
{
if (is_file($sFilePath) && file_exists($sFilePath)) {
if (unlink($sFilePath)) {
return 1;
} else {
$sMsg = '删除失败(可能是权限或被占用)';
// throw new \Exception($sMsg);
}
} else {
$sMsg = '文件不存在或不是普通文件';
// throw new \Exception($sMsg);
}
return 0;
}
}