303 lines
8.5 KiB
PHP
Executable File
303 lines
8.5 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Services\Tg\Hook\Mod;
|
||
|
||
use App\Models\ArticleCache as ModelArticleCache;
|
||
use App\Models\SiteMap as ModelSiteMap;
|
||
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 Arma 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 new($aParam)
|
||
{
|
||
$sSiteGroup = $aParam["aArg"][1] ?? "";
|
||
$sCmdType = "put";
|
||
|
||
if ($sSiteGroup == "?") {
|
||
return "#new [siteGroup]";
|
||
}
|
||
|
||
if (!$sSiteGroup) {
|
||
return "参数不足";
|
||
}
|
||
|
||
$aOpt = [];
|
||
if ($sSiteGroup == "fn_b") {
|
||
$aOpt = [
|
||
"sCategoryName" => "Windows",
|
||
"iIsTop" => "skip",
|
||
"iLikeCount" => "skip",
|
||
"i7like" => "skip",
|
||
"i365like" => "skip",
|
||
"sTag" => "clash,ssr",
|
||
];
|
||
}
|
||
$sOpt = json_encode($aOpt);
|
||
|
||
$sTime = time();
|
||
|
||
$this->oDog->oModelSelf->code = $sSiteGroup.'-'.$sTime;
|
||
$this->oDog->oModelSelf->group_code = $sSiteGroup;
|
||
$this->oDog->oModelSelf->opt = $sOpt;
|
||
$this->oDog->oModelSelf->save();
|
||
|
||
// ServiceArticleCmd::startWithO($this->oDog->oModelSelf)->setSiteGroupCode($sSiteGroup)->syncOpen()->save($sCmdType);
|
||
|
||
return $this->toJson($this->oDog->oModelSelf);
|
||
}
|
||
|
||
public function cmd($aParam)
|
||
{
|
||
$id = $aParam["aOption"][1] ?? false;
|
||
$sSiteGroup = $aParam["aArg"][1] ?? false;
|
||
$sCmdType = $aParam["aArg"][2] ?? "put";
|
||
|
||
if ($sSiteGroup === "?") {
|
||
return "#cmd__[id] [siteGroupCode] [cmd:put]";
|
||
}
|
||
|
||
if (!$id || !$sSiteGroup) {
|
||
return "参数不足";
|
||
}
|
||
|
||
$oArticle = ModelArticleSchema::where("id", $id)->first();
|
||
$r = ServiceArticleCmd::startWithO($oArticle)->setSiteGroupCode($sSiteGroup)->syncOpen()->save($sCmdType);
|
||
$aArticle = $oArticle->toArray();
|
||
unset($aArticle["schema"]);
|
||
|
||
return $this->toJson($aArticle['cmd']);
|
||
}
|
||
|
||
public function thumbUse($aParam)
|
||
{
|
||
$iId = $aParam["aOption"][1] ?? 0;
|
||
$sThumbUse = $aParam["aArg"][1] ?? "";
|
||
|
||
if ($sThumbUse == "?") {
|
||
return "/thumbUse__[id?last] [a,b,c,full*多个]";
|
||
}
|
||
|
||
if (!$sThumbUse) {
|
||
return "空值不可以";
|
||
}
|
||
|
||
if (!$iId) {
|
||
$iId = ModelArticleSchema::where("status", 1)->orderBy("id", "desc")->first()?->id;
|
||
}
|
||
|
||
$oArticleSchema = ModelArticleSchema::where("id", $iId)->first();
|
||
|
||
if (!$oArticleSchema) {
|
||
return "没有这个id的数据";
|
||
}
|
||
|
||
$sOpt = $oArticleSchema->opt;
|
||
$aOpt = json_decode($sOpt, true);
|
||
|
||
$aThumbUse = explode(",", $sThumbUse);
|
||
|
||
$aOpt["thumb_use"] = $aThumbUse;
|
||
|
||
$oArticleSchema->opt = $aOpt;
|
||
$oArticleSchema->save();
|
||
|
||
$r = ServiceArticleCmd::startWithO($oArticleSchema)->syncOpen()->save("opt");
|
||
|
||
$aResult = [
|
||
"id" => $oArticleSchema->id,
|
||
"code" => $oArticleSchema->code,
|
||
"opt" => $oArticleSchema->opt,
|
||
];
|
||
|
||
return $this->toJson($aResult);
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
public function del_self_xiuren($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["data"] as $aSchemaDataRow) {
|
||
if ($aSchemaDataRow["type"] == "photo") {
|
||
foreach ($aSchemaDataRow["photo"] as $aSchemaDataRowPhoto) {
|
||
$sImgA = $this->imgPathByUrl($aSchemaDataRowPhoto["file_path"]);
|
||
$i += $this->imgDelByPath($sImgA);
|
||
}
|
||
}
|
||
}
|
||
|
||
$aSchemaPro = json_decode($oArticleSchema->schema_pro, true);
|
||
|
||
$ii = 0;
|
||
foreach ($aSchemaPro as $aSchemaProRow) {
|
||
if ($aSchemaProRow["type"] == "photo") {
|
||
foreach ($aSchemaProRow["data"] as $sSchemaProRowData) {
|
||
$sFilePath = env("dir_base")."public/".$sSchemaProRowData;
|
||
$sImgA = $this->imgPathByUrl($sFilePath);
|
||
$ii += $this->imgDelByPath($sImgA);
|
||
}
|
||
}
|
||
}
|
||
|
||
$oArticleSchema->delete();
|
||
|
||
$sMsg = "ok,同时删除了{$i}张原始图片,和{$ii}张pro图片。";
|
||
|
||
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;
|
||
}
|
||
|
||
}
|