102 lines
3.5 KiB
PHP
Executable File
102 lines
3.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Rpc\v1;
|
|
|
|
use App\Model\AppNews;
|
|
use App\Model\AppNewsColumn;
|
|
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['created_at'] = date('Y-m-d', $query['created_at']);
|
|
$columnTag = AppNewsColumn::find($query['column_tag']);
|
|
$query['column_tag'] = $columnTag->name ?? '';
|
|
$query['column_tag_url'] = $columnTag->url ?? '';
|
|
|
|
// 相关文章
|
|
$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', 'column_tag'])
|
|
->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');
|
|
$total = $query->count();
|
|
$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();
|
|
|
|
foreach ($value as &$item) {
|
|
$item['created_at'] = date('Y-m-d', $item['created_at']);
|
|
$columnTag = AppNewsColumn::find($item['column_tag']);
|
|
$item['column_tag'] = $columnTag->name ?? '';
|
|
$item['column_tag_url'] = $columnTag->url ?? '';
|
|
}
|
|
|
|
$hot = AppNews::formatQuery(['created_at'])
|
|
->where('platform', $id)
|
|
->where('is_delete', 0)
|
|
->select(['title', 'id', 'cover'])
|
|
->limit(5)
|
|
->orderBy('id', 'desc')
|
|
->get()
|
|
->toArray();
|
|
|
|
return $this->getResponse()->setExtra('hot', $hot)->setExtra('total', ceil($total / $limit))->setData($value)->setCode(0)->send();
|
|
}
|
|
} |