Files
backend/app/Rpc/v1/NewsService.php
2025-06-18 10:31:43 +08:00

59 lines
1.7 KiB
PHP
Executable File

<?php
namespace App\Rpc\v1;
use App\Model\AppNews;
use App\Rpc\BaseService;
use Hyperf\RpcServer\Annotation\RpcService;
#[RpcService(name: "news", server: "jsonrpc-http", protocol: "jsonrpc-http")]
class NewsService extends BaseService
{
/**
* 查看单个新闻详情
* @url /news/view
* @param $id
* @return array
*/
public function view($id): array
{
$query = AppNews::find($id)->toArray();
// 相关文章
$query['about'] = AppNews::formatQuery(['created_at'])
->select(['title', 'id'])
->limit(10)
->orderBy('id', 'desc')
->get()
->toArray();
// 上一篇文章
$query['prevNews'] = AppNews::find($id - 1, ['title', 'id'])?->toArray();
// 下一篇文章
$query['nextNews'] = AppNews::find($id + 1, ['title', 'id'])?->toArray();
return $this->getResponse()->setData($query)->setCode(0)->send();
}
/**
* 查看所有新闻
* @url /news/index
* @param int $limit
* @param int $page
* @return array
*/
public function index(int $limit = 10, int $page = 1): array
{
$query = AppNews::formatQuery(['created_at'])
->select(['id'])
->orderBy('id', 'desc');;
$pagination = $query->paginate($limit, page: $page);
$ids = [];
foreach ($pagination->items() as $item) {
$ids[] = $item->id;
}
$value = AppNews::query()->whereIn('id', $ids)->orderBy('id', 'desc')->get()->toArray();
return $this->getResponse()->setData($value)->setCode(0)->send();
}
}