507 lines
16 KiB
PHP
Executable File
507 lines
16 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Services\Tg\Hook\Mod;
|
||
|
||
use App\Models\Article as ModelArticle;
|
||
use App\Models\ArticleCache as ModelArticleCache;
|
||
use App\Models\ArticleCacheTime as ModelArticleCacheTime;
|
||
use App\Services\Tg\Hook\Dog;
|
||
use App\Services\Article as ServiceArticle;
|
||
use App\Services\TomTool\Flow;
|
||
|
||
class Article extends Base {
|
||
|
||
private $oDog;
|
||
|
||
public function __construct($aUpdate)
|
||
{
|
||
|
||
$this->oDog = new Dog(new ModelArticle());
|
||
// $this->oDog->_setPrimaryKey("id"); // 可选,默认id
|
||
$this->oDog->_setDogName("article"); // 必须,me用,正常就是类名,只不过后期没准改名,所以就这里写死,要在最前面
|
||
$this->oDog->_setStrField(["content"]); // 可选,指定长文本字段
|
||
|
||
}
|
||
|
||
public function __call($sName, $aArg)
|
||
{
|
||
|
||
return $this->oDog->$sName($aArg[0]);
|
||
|
||
}
|
||
|
||
public function save($aParam)
|
||
{
|
||
$sFrom = $aParam["aArg"][1] ?? false;
|
||
$sCacheToView = $aParam["aOption"][1] ?? "y";
|
||
$sCode = "ffq_tg:".date("YmdHis");
|
||
|
||
|
||
//// 先确定code
|
||
$oArticle = new ModelArticle();
|
||
$oArticle->code = $sCode;
|
||
$oArticle->save();
|
||
|
||
ModelArticleCache::where("status", 1)->update(["code" => $sCode]);
|
||
//// end
|
||
|
||
$this->cacheToArticle();
|
||
}
|
||
|
||
public function cacheToArticle($aParam)
|
||
{
|
||
$oArticleCache = ModelArticleCache::where("status", 1)->get();
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
///////////////// 垃圾
|
||
public function show($aParam)
|
||
{
|
||
$xId = $aParam["aArg"][1] ?? "last";
|
||
$sShowField = $aParam["aOption"][1] ?? "content";
|
||
|
||
if ($xId == "?") {
|
||
return "#show__[field:content] [id:last]";
|
||
}
|
||
|
||
if ($xId == "last") {
|
||
$oArticle = ModelArticle::orderBy("id", "desc")->first();
|
||
} else {
|
||
$oArticle = ModelArticle::find($xId);
|
||
}
|
||
|
||
if (!$oArticle) {
|
||
return "没找到文章";
|
||
}
|
||
|
||
$sMsg = "";
|
||
|
||
if ($sShowField == "content") {
|
||
$sMsg = $oArticle->content;
|
||
} else if ($sShowField == "content_schema") {
|
||
$aContentFormat = json_decode($oArticle->content_schema, true);
|
||
|
||
$sMsg = json_encode($aContentFormat, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
||
} else {
|
||
$sMsg = $oArticle->$sShowField;
|
||
}
|
||
|
||
return $sMsg;
|
||
}
|
||
|
||
public function showContent($aParam)
|
||
{
|
||
$aParam["aOption"][1] = "content";
|
||
|
||
return $this->show($aParam);
|
||
}
|
||
|
||
public function showFormat($aParam)
|
||
{
|
||
$aParam["aOption"][1] = "content_schema";
|
||
|
||
return $this->show($aParam);
|
||
}
|
||
|
||
public function cache($aParam)
|
||
{
|
||
|
||
$sFrom = $aParam["aOption"][1] ?? "ffq_tg";
|
||
$sContent = implode(' ', $aParam["aArg"]);
|
||
|
||
$oModelArticleCache = new ModelArticleCache();
|
||
$oModelArticleCache->content = $sContent;
|
||
$oModelArticleCache->from_code = $sFrom;
|
||
$oModelArticleCache->created_at = time();
|
||
$r = $oModelArticleCache->save();
|
||
|
||
$oModelArticleCacheTime = ModelArticleCacheTime::where("from_code", $sFrom)->first();
|
||
$oModelArticleCacheTime->time = time();
|
||
$r = $oModelArticleCacheTime->save();
|
||
|
||
if ($r) {
|
||
return "缓存成功";
|
||
}
|
||
|
||
}
|
||
|
||
public function api_getByCode($sArticleCode)
|
||
{
|
||
$oArticle = ModelArticle::where("code", $sArticleCode)->first();
|
||
|
||
|
||
}
|
||
|
||
public function api_saveAuto_tg($sFrom)
|
||
{
|
||
$iCacheTimeDelay = 60; // 延迟1分钟
|
||
$iCacheTime = ModelArticleCacheTime::where("from_code", $sFrom)->first();
|
||
|
||
$aInfo = [
|
||
"code" => 0,
|
||
];
|
||
|
||
if ($iCacheTime->time == 0) {
|
||
$aProcess[] = "api_saveAuto_tg:xxxak";
|
||
$aInfo = [
|
||
"code" => 0,
|
||
"msg" => "没有新cache的time(不需要更新)",
|
||
"process" => $aProcess,
|
||
];
|
||
return $aInfo;
|
||
}
|
||
|
||
$aProcess[] = "start";
|
||
|
||
if (!$iCacheTime) {
|
||
$aProcess[] = "api_saveAuto_tg:11";
|
||
$aInfo["code"] = 0;
|
||
$aInfo["msg"] = "article_cache_time表里没有对应from_code=".$sFrom;
|
||
$aInfo["process"] = $aProcess;
|
||
return $aInfo;
|
||
}
|
||
|
||
$iTimeDiff = time() - $iCacheTime->time;
|
||
|
||
if ($iTimeDiff > $iCacheTimeDelay) { // 差值大于60,等于最后cache超过一分钟
|
||
$aReturn = $this->saveFFQ([]);
|
||
if ($aReturn["code"] == 1) {
|
||
$aProcess[] = "api_saveAuto_tg:ok";
|
||
$aInfo["code"] = 1;
|
||
$aInfo["msg"] = "ok";
|
||
} else {
|
||
$aProcess[] = "api_saveAuto_tg:sjijs81";
|
||
$aInfo["code"] = 0;
|
||
$aInfo["msg"] = "失败附value";
|
||
$aInfo["value"] = $aReturn;
|
||
$aInfo["process"] = $aProcess;
|
||
}
|
||
} else {
|
||
$aProcess[] = "api_saveAuto_tg:jsjs22a";
|
||
$aInfo["code"] = -1;
|
||
$aInfo["msg"] = "缓存小于".$iCacheTimeDelay."秒";
|
||
$aInfo["process"] = $aProcess;
|
||
}
|
||
|
||
return $aInfo;
|
||
}
|
||
|
||
public function saveFFQ($aParam)
|
||
{
|
||
$aParam["aArg"][1] = "ffq_tg";
|
||
|
||
return $this->save($aParam);
|
||
}
|
||
|
||
// public function showFormat($aParam)
|
||
// {
|
||
// $xId = $aParam["aArg"][1] ?? "last";
|
||
//
|
||
// if ($xId == "?") {
|
||
// return "#showFormat [id,:last]";
|
||
// }
|
||
//
|
||
// if ($xId == "last") {
|
||
// $oModelArticle = ModelArticle::orderBy("id", "desc")->first();
|
||
// } else {
|
||
// $oModelArticle = ModelArticle::find($xId);
|
||
// }
|
||
//
|
||
// if (!$oModelArticle) {
|
||
// return "没找到文章";
|
||
// }
|
||
//
|
||
// $aContentFormat = json_decode($oModelArticle->content_schema, true);
|
||
//
|
||
// return json_encode($aContentFormat, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
||
//
|
||
// }
|
||
|
||
|
||
public function viewRe($aParam)
|
||
{
|
||
$xId = $aParam["aArg"][1] ?? "first";
|
||
|
||
if ($xId == "?") {
|
||
return "#viewRe [id,:first]";
|
||
}
|
||
|
||
$oArticle = ModelArticle::orderBy("id", "desc")->first();
|
||
|
||
if (!$oArticle) {
|
||
return "没找到article";
|
||
}
|
||
|
||
$aUpdate = [
|
||
'status' => 1
|
||
];
|
||
|
||
ModelArticleCache::where("code", $oArticle?->code)->update($aUpdate);
|
||
|
||
$aParam["aArg"][1] = $oArticle?->from_code;
|
||
return $this->cacheToStr($aParam);
|
||
}
|
||
|
||
// 这里现在绑定save了,recahe替代
|
||
private function cacheToStr($aParam) : array
|
||
{
|
||
$sFrom = $aParam["aArg"][1] ?? false;
|
||
$sType = $aParam["aOption"][1] ?? "test";
|
||
|
||
$aInfo = [];
|
||
$aInfo["code"] = 0;
|
||
$aInfo["value"] = "";
|
||
|
||
if (!$sFrom) {
|
||
$aInfo["code"] = 0;
|
||
$aInfo["value"] = "需要from";
|
||
$aInfo["process"] = "66";
|
||
return $aInfo;
|
||
}
|
||
|
||
if ($sFrom == "ffq") {
|
||
$sFrom = "ffq_tg";
|
||
}
|
||
|
||
$oModelArticleCache = ModelArticleCache::where("status", 1)->where("type", "cache")->orderBy('id', 'asc')->get();
|
||
|
||
if ($oModelArticleCache->isEmpty()) {
|
||
$aInfo["code"] = 0;
|
||
$aInfo["value"] = "没有cache";
|
||
$aInfo["process"] = "77xx";
|
||
return $aInfo;
|
||
}
|
||
|
||
$sArticleStr = '';
|
||
$oServiceArticle = new ServiceArticle();
|
||
|
||
ModelArticleCache::where("type", "view")->delete();
|
||
|
||
$i = 0;
|
||
foreach ($oModelArticleCache as $oModelArticleCacheRow) {
|
||
|
||
// $sCode = "ffq_tg:".date("Ymd").":".uniqid();
|
||
|
||
// $oModelArticleCacheRow->code = $sCode;
|
||
// $oModelArticleCacheRow->save();
|
||
|
||
$aData = $oServiceArticle->format($sFrom, $oModelArticleCacheRow->content);
|
||
|
||
foreach ($aData as $aDataRow) {
|
||
if ($aDataRow["status"] == 1) {
|
||
if ($aDataRow["content_type"] == "img") {
|
||
|
||
$aContentFormat = [
|
||
"content_type" => $aDataRow["content_type"],
|
||
"title" => $aDataRow["title"],
|
||
"img" => $aDataRow["img"],
|
||
"name" => $aDataRow["name"],
|
||
];
|
||
|
||
$oModelArticleCacheTmp = new ModelArticleCache();
|
||
$oModelArticleCacheTmp->content = "<img class='img_cesu' src='".$aDataRow["img"]["base"]."' alt='".$aDataRow["title"]."'>";
|
||
// $oModelArticleCacheTmp->content = $aDataRow["file_path"]["img"];
|
||
$oModelArticleCacheTmp->content_schema = json_encode($aContentFormat);
|
||
$oModelArticleCacheTmp->content_schema_type = "img";
|
||
$oModelArticleCacheTmp->name = $aDataRow["name"];
|
||
// $oModelArticleCacheTmp->code = $sCode;
|
||
$oModelArticleCacheTmp->type = "view";
|
||
$i += $oModelArticleCacheTmp->save();
|
||
}
|
||
if ($aDataRow["content_type"] == "str") {
|
||
|
||
$aContentFormat = [
|
||
"content_type" => "str",
|
||
"content" => $aDataRow["content"],
|
||
"name" => $aDataRow["name"],
|
||
];
|
||
|
||
$oModelArticleCacheTmp = new ModelArticleCache();
|
||
$oModelArticleCacheTmp->content = $aDataRow["content"];
|
||
$oModelArticleCacheTmp->content_schema = json_encode($aContentFormat);
|
||
$oModelArticleCacheTmp->content_schema_type = "str";
|
||
$oModelArticleCacheTmp->name = $aDataRow["name"];
|
||
// $oModelArticleCacheTmp->code = $sCode;
|
||
$oModelArticleCacheTmp->type = "view";
|
||
$i += $oModelArticleCacheTmp->save();
|
||
}
|
||
if ($aDataRow["content_type"] == "table") {
|
||
|
||
$aContentFormat = [
|
||
"content_type" => "table",
|
||
"content" => $aDataRow["content"],
|
||
"name" => $aDataRow["name"],
|
||
];
|
||
|
||
$oModelArticleCacheTmp = new ModelArticleCache();
|
||
$oModelArticleCacheTmp->content = $aDataRow["content"];
|
||
$oModelArticleCacheTmp->content_schema = json_encode($aContentFormat);
|
||
$oModelArticleCacheTmp->content_schema_type = "table";
|
||
$oModelArticleCacheTmp->name = $aDataRow["name"];
|
||
// $oModelArticleCacheTmp->code = $sCode;
|
||
$oModelArticleCacheTmp->type = "view";
|
||
$i += $oModelArticleCacheTmp->save();
|
||
}
|
||
// 不在类型里的直接忽视 没有else
|
||
} else {
|
||
// 不在类型里的直接忽视
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
if ($sType == "test") {
|
||
$oModelArticleCacheView = ModelArticleCache::where("status", 1)->where("type", "view")->get();
|
||
$aInfo["code"] = 1;
|
||
$aInfo["value"] = $oModelArticleCacheView;
|
||
$aInfo["process"] = 88;
|
||
return $aInfo;
|
||
}
|
||
|
||
$aInfo["code"] = 1;
|
||
$aInfo["value"] = $i;
|
||
$aInfo["process"] = 99;
|
||
return $aInfo;
|
||
}
|
||
|
||
public function save($aParam)
|
||
{
|
||
$sFrom = $aParam["aArg"][1] ?? false;
|
||
$sCacheToView = $aParam["aOption"][1] ?? "y";
|
||
$sCode = "ffq_tg:".date("YmdHis");
|
||
|
||
|
||
//// 先确定code
|
||
$oArticle = new ModelArticle();
|
||
$oArticle->code = $sCode;
|
||
$oArticle->save();
|
||
|
||
ModelArticleCache::where("status", 1)->update(["code" => $sCode]);
|
||
//// end
|
||
}
|
||
|
||
public function save_old($aParam)
|
||
{
|
||
$sFrom = $aParam["aArg"][1] ?? false;
|
||
$sCacheToView = $aParam["aOption"][1] ?? "y";
|
||
$sCode = "ffq_tg:".date("YmdHis");
|
||
|
||
$aInfo = [
|
||
"code" => 0,
|
||
];
|
||
|
||
$aProcess[] = "start";
|
||
|
||
//// test
|
||
|
||
//// end
|
||
if (!$sFrom) {
|
||
return "需要from";
|
||
}
|
||
|
||
if ($aParam["aArg"][1] == '?') {
|
||
return "/article#save [from]";
|
||
}
|
||
|
||
if ($sFrom == "ffq") {
|
||
$sFrom = "ffq_tg";
|
||
}
|
||
|
||
if ($sCacheToView == "y") {
|
||
$aParam["aOption"][1] = "done";
|
||
$aData = $this->cacheToStr($aParam);
|
||
|
||
if ($aData["code"] != 1) {
|
||
return $aData;
|
||
}
|
||
}
|
||
|
||
$aOrderTypes = ['str', 'table', 'img'];
|
||
|
||
$oModelArticleView = ModelArticleCache::where("status", 1)
|
||
->where("type", "view")
|
||
->orderBy('id', 'asc')
|
||
// ->orderByRaw("FIELD(type, 'img', 'str')")
|
||
->orderByRaw("FIELD(type, '" . implode("','", $aOrderTypes) . "')")
|
||
->get();
|
||
|
||
if ($sFrom == "ffq_tg") {
|
||
|
||
$sArticle = '';
|
||
$aContentFormatMix = [];
|
||
$oServiceArticle = new ServiceArticle();
|
||
$oModelArticle = new ModelArticle();
|
||
foreach ($oModelArticleView as $oModelArticleViewRow) {
|
||
|
||
$aContentFormat = $oModelArticleViewRow->content_schema;
|
||
$aContentFormatMix[] = json_decode($aContentFormat);
|
||
|
||
$sArticle .= $oServiceArticle->contentReplace($oModelArticleViewRow->content_schema_type, $oModelArticleViewRow->content);
|
||
$sArticle .= "\n\n";
|
||
|
||
if ($oModelArticleViewRow->name == "name") {
|
||
$oModelArticle->name = $oModelArticleViewRow->content;
|
||
}
|
||
|
||
if ($oModelArticleViewRow->name == "title") {
|
||
$oModelArticle->title = $oModelArticleViewRow->content;
|
||
}
|
||
|
||
// $oModelArticle->code = $oModelArticleViewRow->code;
|
||
|
||
}
|
||
|
||
$oModelArticle->from_code = "ffq_tg";
|
||
$oModelArticle->to_type = "vp_md";
|
||
$oModelArticle->content = $sArticle;
|
||
$oModelArticle->content_schema = json_encode($aContentFormatMix);
|
||
$oModelArticle->code = $sCode;
|
||
$r = $oModelArticle->save();
|
||
}
|
||
|
||
$r = ModelArticleCache::where('status', 1)
|
||
->update([
|
||
'status' => 0,
|
||
'code' => $sCode,
|
||
]);
|
||
|
||
$oModelArticleCacheTime = ModelArticleCacheTime::where("from_code", $sFrom)->first();
|
||
$oModelArticleCacheTime->time = 0;
|
||
$r = $oModelArticleCacheTime->save();
|
||
|
||
if ($r) {
|
||
|
||
$aProcess[] = "ok";
|
||
|
||
// to 通知同步api
|
||
|
||
$aInfo = [
|
||
"code" => 1,
|
||
"msg" => "ok",
|
||
"process" => $aProcess,
|
||
];
|
||
|
||
return $aInfo;
|
||
}
|
||
|
||
$aProcess[] = "xkxk221";
|
||
|
||
$aInfo = [
|
||
"code" => 0,
|
||
"msg" => "未能保存?",
|
||
"process" => $aProcess,
|
||
];
|
||
|
||
return $aInfo;
|
||
|
||
}
|
||
|
||
}
|