191 lines
6.2 KiB
PHP
Executable File
191 lines
6.2 KiB
PHP
Executable File
<?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\Services\TomTool\HttpV2;
|
||
use App\Services\TagService;
|
||
|
||
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["tag"] ?? "";
|
||
$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",
|
||
];
|
||
|
||
$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->save();
|
||
|
||
(new TagService())->syncArticleTagsByName($oArticle, $aOptTag);
|
||
|
||
// $aArticle = $oArticle->toArray();
|
||
// unset($aArticle["sContent"]);
|
||
// unset($aArticle["sTitleSub"]);
|
||
// $jArticle = json_encode($aArticle, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
|
||
|
||
$sMsg = "文章put成功:{$oArticle->sSlug}";
|
||
TeleSlave::notify()->send($sMsg);
|
||
|
||
return [
|
||
"isDone" => true,
|
||
];
|
||
}
|
||
|
||
public function receiveFreenode(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"
|
||
//}
|