From 66b7df8cb3d60b7b447c641d4816c24d935aeb56 Mon Sep 17 00:00:00 2001 From: toom1996 Date: Mon, 4 Aug 2025 12:03:15 +0800 Subject: [PATCH] update --- app/Rpc/baoxian/NewsService.php | 143 ++++++++++++++++++++++++++++++++ app/Rpc/v1/ColumnService.php | 2 + app/Rpc/v1/NewsService.php | 1 + 3 files changed, 146 insertions(+) create mode 100644 app/Rpc/baoxian/NewsService.php diff --git a/app/Rpc/baoxian/NewsService.php b/app/Rpc/baoxian/NewsService.php new file mode 100644 index 0000000..fd26c91 --- /dev/null +++ b/app/Rpc/baoxian/NewsService.php @@ -0,0 +1,143 @@ +where('id', $id)->where('platform', $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 ?? ''; + + if ($query['second_column']) { + $columnTag = AppNewsSecondColumn::find($query['second_column']); + $query['second_column_tag'] = $columnTag->name ?? ''; + $query['second_column_tag_url'] = $columnTag->url ?? ''; + } + + // 相关文章 + $query['about'] = AppNews::formatQuery(['created_at']) + ->where('platform', $app_id) + ->where('is_delete', 0) + ->select(['title', 'id', 'cover', 'created_at', 'description']) + ->limit(10) + ->orderBy('id', 'desc') + ->get() + ->toArray(); + + // 随机推荐 + $query['recommend'] = AppNews::formatQuery(['created_at']) + ->where('platform', $app_id) + ->where('is_delete', 0) + ->select(['title', 'id', 'cover', 'created_at']) + ->limit(10) + ->orderBy(Db::raw('RAND()')) + ->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 $tag + * @param int $limit + * @param int $page + * @param array $filter + * @return array + */ + public function index(int $id, int $limit = 30, int $page = 1, array $filter = []): array + { + $filterParams = []; + if (isset($filter['column_tag'])) { + $filterParams[] = "column_tag = {$filter['column_tag']}"; + } + + if (isset($filter['second_column'])) { + $filterParams[] = "second_column = {$filter['second_column']}"; + } + + $filter = implode(' AND ', $filterParams) ? implode(' AND ', $filterParams) . ' AND ' : ''; + // 使用 DB 和 Query Builder + // 使用 DB 和 Query Builder 执行窗口函数查询 + // 使用原生 SQL 查询 + $sql = " + SELECT + id, + second_column, + cover, + title, + description, + platform, + column_tag + FROM + ( SELECT second_column, id, cover, title, description, platform,column_tag, ROW_NUMBER() OVER ( PARTITION BY second_column ORDER BY id DESC ) AS rn FROM app_news ) AS ranked + WHERE + rn <= 30 AND + {$filter} + platform = {$id} + "; + + $articles = []; + // 执行原生 SQL 查询 + $query = Db::select($sql); + + $totalSql = " + SELECT + count(`id`) as `total` + FROM + app_news + WHERE + {$filter} + platform = {$id} + "; + $total = Db::select($totalSql); + $res = []; + $cache = []; + foreach ($query as $item) { + $tag = $item->second_column; + if (!isset($cache[$tag])) { + $cache[$tag] = AppNewsSecondColumn::find($tag)?->toArray(); + } + + if ($cache[$tag]) { + $res[$tag]['title'] = $cache[$tag]['name']; + $res[$tag]['desc'] = $cache[$tag]['desc']; + $res[$tag]['url'] = $cache[$tag]['url']; + $res[$tag]['item'][] = $item; + } + } + + $articles['articles'] = $res; + $total = current($total)?->total ?? 0; + return $this->getResponse()->setExtra('total', ceil($total / $limit))->setData($articles)->setCode(0)->send(); + } +} \ No newline at end of file diff --git a/app/Rpc/v1/ColumnService.php b/app/Rpc/v1/ColumnService.php index 9218e59..18d2c94 100644 --- a/app/Rpc/v1/ColumnService.php +++ b/app/Rpc/v1/ColumnService.php @@ -47,6 +47,8 @@ class ColumnService extends BaseService 'path' => "{$column['url']}{$secondCol['url']}", 'desc' => $secondCol['desc'], 'item' => $appendArr, + 'name' => $secondCol['name'], + 'id' => $secondCol['id'], ]; } } diff --git a/app/Rpc/v1/NewsService.php b/app/Rpc/v1/NewsService.php index 242e720..b148f82 100755 --- a/app/Rpc/v1/NewsService.php +++ b/app/Rpc/v1/NewsService.php @@ -125,6 +125,7 @@ class NewsService extends BaseService ->toArray(); // 猜你喜欢 + // 这里放的都是未收录的文章。 $guess = AppNews::formatQuery(['created_at']) ->where('platform', $id) ->where('is_delete', 0)