fn_b/app/Http/Controllers/Api/v1/ArticlesController.php
2026-09-11 16:33:27 +08:00

336 lines
12 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\Http\Controllers\Api\v1;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\TomTool\Flow;
use App\Services\TomTool\Telegram\Slave as TeleSlave;
use App\Models\Articles;
use App\Models\Categories;
use App\Models\ArticleFreenodeThumbNum;
use App\Models\ArticleThumbNum;
use App\Services\TomTool\HttpV2;
use App\Services\TagService;
use App\Services\IndexNowService;
class ArticlesController extends Controller
{
public function find(Request $oRequest)
{
$aData = $oRequest->all();
$sArticleType = $aData["articleType"] ?? "freenode";
$sArticleCode = $aData["articleCode"] ?? "fn".date("Ymd");
$iCategoryId = Categories::where("sSlug", "freenode")->first()?->id;
$oArticle = Articles::where("iCategoryId", $iCategoryId)->where("sSlug", $sArticleCode)->first();
if (!$oArticle) {
$oArticle = Articles::where("iCategoryId", $iCategoryId)->orderBy("id", "desc")->first();
}
return response()->json($oArticle);
}
public function receive(Request $oRequest)
{
$aArticle = $oRequest->input("_raw");
$sTitle = $aArticle["name"] ?? "";
$sSchema = $aArticle["schema"] ?? "";
$sSlug = "m".$aArticle["id"];
$sOpt = $aArticle["opt"] ?? "[]";
$aOpt = json_decode($sOpt, true);
$iOptIsTop = $aOpt["iIsTop"] ?? 0;
$sOptCategoryName = $aOpt["sCategoryName"] ?? "";
$sOptTag = $aOpt["sTag"] ?? "";
$aOptTag = explode(",", $sOptTag);
$oCategory = Categories::where("sName", $sOptCategoryName)->first();
if (!$oCategory) {
$sMsg = "不存在的文章分类:{$sOptCategoryName}";
TeleSlave::fail()->send($sMsg);
exit($sMsg);
}
$aPutField = [
"iCategoryId" => $oCategory->id,
"iLikeCount" => $aOpt["iLikeCount"] ?? "skip",
"i7like" => $aOpt["i7like"] ?? "skip",
"i365like" => $aOpt["i365like"] ?? "skip",
"iIsTop" => $aOpt["iIsTop"] ?? "skip",
"sThumb" => $aOpt["sThumb"] ?? "skip",
];
$oArticle = Articles::where("sSlug", $sSlug)->first();
if (!$oArticle) {
$oArticle = new Articles();
$oArticle->sSlug = $sSlug;
}
foreach ($aPutField as $k => $v) {
if ($v !== "skip") {
$oArticle->{$k} = $v;
}
}
$oArticle->sContent = $sSchema;
$oArticle->sTitle = $sTitle;
$oArticle->sTitleSub = mb_substr(strip_tags($sSchema), 0, 150, 'utf-8');
$oArticle->save();
(new TagService())->syncArticleTagsByName($oArticle, $aOptTag);
$sMsg = "文章put成功:{$oArticle->sSlug}";
TeleSlave::notify()->send($sMsg);
return [
"isDone" => true,
];
}
public function receiveShadowrocketids(Request $oRequest)
{
$sObjName = "fn-b:article_receive_shadowrocketids";
$oFlow = Flow::make($sObjName);
$aData = $oRequest->all();
$sArticle = $aData["content"] ?? '';
$sTitle = $aData["title"] ?? '';
$sTitleSub = $aData["title_sub"] ?? '';
$aArticleTags = $aData["tags"] ?? [];
$sSlug = 'ssi'.date("Ymd");
// $sImgAuthor = "tg_xiuren_a";
$sSubKey = $_ENV['site_code_short'].'::ssi';
$i7like = $aData["7like"] ?? 0;
$oThumbNum = ArticleThumbNum::firstOrCreate(["author_code" => $sSubKey, "type" => "shadowrocketids"]);
$iThumbNum = $oThumbNum->num;
$sImagesApi = "https://".config("path.url_resource_base")."/api/images/get/url-by-subkey";
$aApiArg = [
"sSubKey" => $sSubKey,
"iNum" => $iThumbNum+1,
];
$sApiResult = HttpV2::make($sImagesApi, "post")->withToken()->setDataA($aApiArg)->send();
$aApiResult = json_decode($sApiResult, true);
if (!isset($aApiResult["isDone"]) || !$aApiResult["isDone"]) {
echo $sApiResult;
return response()->json($oFlow->fail("{$sObjName}的图片api调用失败"));
}
$sThumbPath = $aApiResult["aData"]["sFileUrl"];
$sThumbNumTrue = $aApiResult["aData"]["iTrueNum"];
$oCategories = Categories::where("sSlug", "shadowrocketids")->first();
$oArticle = Articles::where('sSlug', $sSlug)->first();
$aAttributes = [
'sThumb' => $sThumbPath,
'iCategoryId' => $oCategories->id,
'sTitle' => '「'.date("n月j日").'」'.$sTitle,
'sTitleSub' => $sTitleSub,
'sContent' => $sArticle,
'iStatus' => 1,
'iUserId' => 1,
];
// 只有新创建时才写入初始点赞数
if (!$oArticle) {
$aAttributes['i7like'] = $i7like;
}
$oArticle = Articles::updateOrCreate(
['sSlug' => $sSlug],
$aAttributes
);
(new TagService())->syncArticleTagsByName($oArticle, $aArticleTags);
ArticleThumbNum::where("author_code", $sSubKey)->where("type", "shadowrocketids")->update(["num" => $sThumbNumTrue]);
$sArticleUrl = config("app.url")."/a/".$sSlug;
$oIndexNow = new IndexNowService();
$bIndexR = $oIndexNow->pushUrls([$sArticleUrl.".html"]);
TeleSlave::notify()->send($sArticleUrl." 发布成功 | "."indexNow推送:".$bIndexR);
return response()->json($oFlow->done());
}
public function receiveFreenode(Request $oRequest)
{
$sObjName = "nodehub:article_receive_freenodeV2";
$oFlow = Flow::make($sObjName);
$aData = $oRequest->all();
$aArticle = $aData["article"];
$sArticleTitle = $aArticle["title"];
$sArticleTitleSub = $aArticle["title_sub"];
$sArticleContent = $aArticle["content"];
$aArticleTags = $aArticle["tags"];
$sSlug = 'fn'.date("Ymd");
// $sImgAuthor = "tg_xiuren_a";
$sSubKey = $_ENV['site_code_short'].'::fn';
$i7like = $aArticle["7like"];
$oThumbNum = ArticleFreenodeThumbNum::firstOrCreate(["author_code" => $sSubKey]);
$iThumbNum = $oThumbNum->num;
$sImagesApi = "https://".config("path.url_resource_base")."/api/images/get/url-by-subkey";
$aApiArg = [
"sSubKey" => $sSubKey,
"iNum" => $iThumbNum+1,
];
$sApiResult = HttpV2::make($sImagesApi, "post")->withToken()->setDataA($aApiArg)->send();
$aApiResult = json_decode($sApiResult, true);
if (!isset($aApiResult["isDone"]) || !$aApiResult["isDone"]) {
echo $sApiResult;
return response()->json($oFlow->fail("{$sObjName}的图片api调用失败"));
}
$sThumbPath = $aApiResult["aData"]["sFileUrl"];
$sThumbNumTrue = $aApiResult["aData"]["iTrueNum"];
$oCategories = Categories::where("sSlug", "freenode")->first();
$oArticle = Articles::where('sSlug', $sSlug)->first();
$aAttributes = [
'sThumb' => $sThumbPath,
'iCategoryId' => $oCategories->id,
'sTitle' => $sArticleTitle,
'sTitleSub' => $sArticleTitleSub,
'sContent' => $sArticleContent,
'iStatus' => 1,
'iUserId' => 1,
];
// 只有新创建时才写入初始点赞数
if (!$oArticle) {
$aAttributes['i7like'] = $i7like;
}
$oArticle = Articles::updateOrCreate(
['sSlug' => $sSlug],
$aAttributes
);
(new TagService())->syncArticleTagsByName($oArticle, $aArticleTags);
ArticleFreenodeThumbNum::where("author_code", $sSubKey)->update(["num" => $sThumbNumTrue]);
$sArticleUrl = config("app.url")."/a/".$sSlug;
$oIndexNow = new IndexNowService();
$bIndexR = $oIndexNow->pushUrls([$sArticleUrl.".html"]);
TeleSlave::notify()->send($sArticleUrl." 发布成功 | "."indexNow推送:".$bIndexR); // indexR是true或false,发送到tg什么都没有?
return response()->json($oFlow->done());
}
public function receiveFreenode_laji(Request $oRequest)
{
$sObjName = "nodehub:article_receive_freenode";
$oFlow = Flow::make($sObjName);
$aData = $oRequest->all();
$aArticle = $aData["article"];
$sArticleTitle = $aArticle["title"];
$sArticleTitleSub = $aArticle["title_sub"];
$sArticleContent = $aArticle["content"];
$aArticleTags = $aArticle["tags"];
$sSlug = 'fn'.date("Ymd");
$sImgAuthor = "tg_xiuren_a";
$i7like = $aArticle["7like"];
$oThumbNum = ArticleFreenodeThumbNum::firstOrCreate(["author_code" => $sImgAuthor]);
$iThumbNum = $oThumbNum->num;
$sImagesApi = "https://".config("path.url_resource_base")."/api/images/get/url-by-num";
$aApiArg = [
"sAuthorCode" => $sImgAuthor,
"iNum" => $iThumbNum+1,
];
$sApiResult = HttpV2::make($sImagesApi, "post")->withToken()->setDataA($aApiArg)->send();
$aApiResult = json_decode($sApiResult, true);
if (!isset($aApiResult["isDone"]) || !$aApiResult["isDone"]) {
echo $sApiResult;
return response()->json($oFlow->fail("{$sObjName}的图片api调用失败"));
}
$sThumbPath = $aApiResult["aData"]["sFileUrl"];
$sThumbNumTrue = $aApiResult["aData"]["iTrueNum"];
$oCategories = Categories::where("sSlug", "freenode")->first();
$oArticle = Articles::updateOrCreate(
['sSlug' => $sSlug],
[
'sThumb' => $sThumbPath,
'iCategoryId' => $oCategories->id,
'sTitle' => $sArticleTitle,
'sTitleSub' => $sArticleTitleSub,
'sContent' => $sArticleContent,
'i7like' => $i7like,
'iStatus' => 1,
'iUserId' => 1,
],
);
(new TagService())->syncArticleTagsByName($oArticle, $aArticleTags);
ArticleFreenodeThumbNum::where("author_code", $sImgAuthor)->update(["num" => $sThumbNumTrue]);
$sArticleUrl = config("app.url")."/feenode/".$sSlug;
TeleSlave::notify()->send($sArticleUrl." 发布成功");
return response()->json($oFlow->done());
}
}
//array(13) {
// ["id"]=>
// int(134)
// ["code"]=>
// string(15) "fn_b-1773480551"
// ["name"]=>
// string(1) "1"
// ["schema"]=>
// string(1) "1"
// ["schema_pro"]=>
// NULL
// ["group_code"]=>
// string(4) "fn_b"
// ["cmd"]=>
// string(114) "{"|fn_b|vfnfull|20260314172911|":{"site_code":"vfnfull","cmd":"put","value":"value","date":"2026-03-14 17:29:11"}}"
// ["cmd_log"]=>
// NULL
// ["opt"]=>
// string(95) "{"sCategoryName":"Windows","iIsTop":0,"iLikeCount":0,"i7like":0,"i365like":0,"tag":"clash,ssr"}"
// ["enable_sync"]=>
// int(1)
// ["status"]=>
// int(1)
// ["created_at"]=>
// string(27) "2026-03-14T09:29:11.000000Z"
// ["updated_at"]=>
// string(27) "2026-03-14T09:29:11.000000Z"
//}