Files
backend/app/Controller/admin/api/ArticleController.php
2025-06-18 10:31:43 +08:00

113 lines
3.9 KiB
PHP
Executable File

<?php
namespace App\Controller\admin\api;
use App\Controller\AbstractController;
use App\Enums\ArticlePublishedStatusEnum;
use App\FormModel\admin\articles\ModifyModel;
use App\Helpers\AppHelper;
use App\Model\AppArticle;
use App\Model\AppBrand;
use App\Model\AppNews;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
#[Controller(prefix: 'admin/api')]
class ArticleController extends AbstractController
{
/**
* 文章列表
* @url /admin/api/article
* @return array
*/
#[RequestMapping(path:'articles', methods: 'get')]
public function index(): array
{
$publishedStatus = $this->request->query('published_status', '');
$createdFilter = $this->request->query('created_at', [date('Y-m-d', strtotime('-1 month')), date('Y-m-d', time())]);
foreach ($createdFilter as $index => &$item) {
if ($index == 0) {
$item = strtotime($item . ' 00:00:00');
}
if ($index == 1) {
$item = strtotime($item . ' 23:59:59');
}
}
$query = AppNews::formatQuery(['module', 'published_status', 'location'])
->select(['id'])
->when($publishedStatus !== '', fn($q) => $q->where('published_status', $publishedStatus))
->whereBetween('created_at', $createdFilter)
->orderBy('created_at', 'desc');
$pagination = $query->paginate($this->request->input('limit', 10), page: $this->request->input('page'));
foreach ($pagination->items() as $item) {
$ids[] = $item->id;
}
$value = AppNews::find($ids, ['aid', 'title', 'module', 'published_status', 'location'])->toArray();
return ['code' => 0, 'msg' => 'ok', 'count' => $pagination->total(), 'data' => $value];
}
// /**
// * 新增/编辑文章
// * @url /api/v1/articles/save
// * @return array
// */
// #[RequestMapping(path:'save', methods: 'post')]
// public function save(): array
// {
// $id = $this->request->input('id');
// $model = new ArticlesModel();
// if ($id) {
// $model->edit();
// } else {
// $model->create();
// }
//
// return [
// 'code' => 0,
// 'message' => 'ok'
// ];
// }
#[RequestMapping(path:'brand-search', methods: 'get')]
public function brandSearch(): array
{
$input = $this->request->input('brand');
$query = AppBrand::query()->select(['name', 'cn_name', 'id'])->where('name', 'like', "%$input%")
->orWhere('cn_name', 'like', "%$input%")
->get()->toArray();
return ['code' => 0, 'msg' => 'ok', 'data' => $query];
}
#[RequestMapping(path:'view', methods: 'get')]
public function view(): \Psr\Http\Message\ResponseInterface
{
$query = AppNews::formatQuery(['images', 'brand_name', 'translate_title'])
->select(['brand as brand_name', 'brand', 'images', 'id', 'title', 'title as translate_title', 'cover', 'aid', 'location'])
->where('aid', $this->request->query('id'));
return $this->response->json(['code' => 0, 'msg' => 'ok', 'data' => $query->first()]);
}
#[RequestMapping(path:'publish', methods: 'post')]
public function publish()
{
$query = AppArticle::where('aid', $this->request->post('aid'))->first();
$query->published_status = ArticlePublishedStatusEnum::TRUE->value;
$query->published_at = time();
$query->save();
return $this->response->json(['code' => 0, 'msg' => 'ok']);
}
#[RequestMapping(path:'update', methods: 'post')]
public function update()
{
$model = new ModifyModel();
$model->setAttributes($this->request->post());
$model->update();
return $this->response->json(['code' => 0, 'msg' => 'ok']);
}
}