Files
backend/app/Rpc/v1/NewsService.php
toom1996 e9ab49ad24 update
2025-07-15 17:03:20 +08:00

79 lines
2.6 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
{
public const RELATION = [
'11' => [1, 11],
'10' => [9, 10],
];
/**
* 查看单个新闻详情
* @url /news/view
* @param $id
* @return array
*/
public function view($id, $app_id): array
{
$isRelation = isset(self::RELATION[$app_id]);
$query = AppNews::query()->where('id', $id)->where('platform', $isRelation ? current(self::RELATION[$app_id]) : $app_id)->where('is_delete', 0)->first()?->toArray();
if (!$query) {
return $this->getResponse()->setCode(404)->send();
}
// 相关文章
$query['about'] = AppNews::formatQuery(['created_at'])
->where('platform', $isRelation ? current(self::RELATION[$app_id]) : $app_id)
->where('is_delete', 0)
->select(['title', 'id'])
->limit(10)
->orderBy('id', 'desc')
->get()
->toArray();
// 上一篇文章
$query['prevNews'] = AppNews::query()->where('id', '<', $id)->select(['title', 'id'])->where('platform', $app_id)->where('is_delete', 0)->orderBy('id', 'desc')->first();
// 下一篇文章
$query['nextNews'] = AppNews::query()->where('id', '>', $id)->select(['title', 'id'])->where('platform', $app_id)->where('is_delete', 0)->first();
return $this->getResponse()->setData($query)->setCode(0)->send();
}
/**
* 查看所有新闻
* @url /news/index
* @param int $limit
* @param int $page
* @return array
*/
public function index(int $id, int $limit = 30, int $page = 1): array
{
$query = AppNews::formatQuery(['created_at'])
->where('is_delete', 0);
if (isset(self::RELATION[$id])) {
$query->whereIn('platform', self::RELATION[$id]);
} else {
$query->where('platform', $id);
}
$query->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();
}
}