github/app/Services/TG/Hook/Mod/Article.php
2025-04-15 22:16:45 +08:00

137 lines
4.0 KiB
PHP
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\Article as ModelArticle;
class Article extends Base {
public function List()
{
$aArticleList = ModelArticle::orderBy("user_name")->get()->toArray();
foreach ($aArticleList as &$row) { // 使用引用来修改原数组
unset($row['updated_at']); // 移除 updated_at 字段
unset($row['created_at']); // 移除 created_at 字段
$this->oTGHttp->sendToTG(json_encode($row, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}
$this->oHttp::response(200, 'ok');
}
public function Find($aParam)
{
$iId = $aParam['aArg'][1];
$aSingle = ModelArticle::find($iId)->toArray();
unset($aSingle['updated_at']);
unset($aSingle['created_at']);
return json_encode($aSingle, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
public function Like($aParams)
{
$aArticleList = ModelArticle::where($this->cmdGood($aParams['aOption'][1]), 'like', '%' . $aParams['aArg'][1] . '%')
->orderBy("user_name")
->get()
->toArray();
if (empty($aArticleList)) {
$this->oTGHttp->sendToTG("没有");
} else {
foreach ($aArticleList as &$row) { // 使用引用来修改原数组
unset($row['updated_at']); // 移除 updated_at 字段
unset($row['created_at']); // 移除 created_at 字段
$this->oTGHttp->sendToTG(json_encode($row, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}
}
$this->oHttp::response(200, 'ok');
}
public function Clone($aParam)
{
$iId = $aParam['aArg'][1];
// 查找文章
$aArticle = ModelArticle::find($iId);
if ($aArticle) {
// 复制文章
$newArticle = $aArticle->replicate();
// 如果需要,可以修改新文章的某些字段,例如标题或状态
// $newArticle->title = $newArticle->title . ' - Copy'; // 修改标题
// $newArticle->status = 'draft'; // 例如,将状态设置为草稿
// 保存新文章
$newArticle->save();
// 发送新文章的 ID 到 Telegram
$sMsg = "新文章已创建ID: " . $newArticle->id;
$this->oTGHttp->sendToTG($sMsg);
// 返回 HTTP 响应
$this->oHttp::response(200, 'ok');
} else {
throw new \Exception("没有这个id");
}
}
public function Add($aParams)
{
$newArticle = new ModelArticle();
$newArticle->user_name = $aParams['aArg'][1];
$r = $newArticle->save();
$this->oTGHttp->sendToTG(json_encode($r, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
$this->oHttp::response(200, 'ok');
}
public function Del($aParams)
{
$article = ModelArticle::find($aParams['aArg'][1]);
if ($article) {
$r = $article->delete();
} else {
$r = false;
}
$this->oTGHttp->sendToTG(json_encode($r, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
$this->oHttp::response(200, 'ok');
}
// /ArticleSet__id__user_name xxx
public function Set($aParams)
{
$sField = $this->cmdGood($aParams['aOption'][2]);
$article = ModelArticle::find($aParams['aOption'][1]);
$sSet = $aParams['aArg'][1];
if ($article) {
$article->$sField = $sSet;
$r = $article->save();
} else {
// 处理未找到的情况
throw new \Exception("Article not found");
}
$this->oTGHttp->sendToTG(json_encode($r, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
$this->oHttp::response(200, 'ok');
}
}