| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- require 'Db.class.php';
- function loadListByChannelName($maxCount, $name) {
- try {
- // 1. 从pr_cms_channel表中通过name查询channel_id
- $channel = Db::table('pr_cms_channel')
- ->where('name', $name)
- ->first(['id']);
-
- // 如果没有找到对应的频道,返回空数组
- if (!$channel) {
- return [];
- }
-
- $channelId = $channel['id'];
-
- // 2. 在pr_cms_archives中通过channel_id按createtime倒序查询指定数量的最新文章
- $articles = Db::table('pr_cms_archives')
- ->where('channel_id', $channelId)
- ->order('createtime DESC')
- ->limit(0, $maxCount)
- ->get();
-
- return $articles ?? [];
- } catch (Exception $e) {
- // 发生异常时返回空数组
- return [];
- }
- }
- function loadListByChannelNameAndPage($maxCount, $name, $page, $searchKeyword = '') {
- try {
- // 1. 从pr_cms_channel表中通过name查询channel_id
- $channel = Db::table('pr_cms_channel')
- ->where('name', $name)
- ->first(['id']);
-
- // 如果没有找到对应的频道,返回包含空列表和0总页数的数组
- if (!$channel) {
- return ['list' => [], 'totalPages' => 0];
- }
-
- $channelId = $channel['id'];
-
- // 2. 查询符合条件的文章总数量
- $totalCountQuery = Db::table('pr_cms_archives')
- ->where('channel_id', $channelId);
-
- // 如果提供了搜索关键字,添加对title字段的模糊搜索
- if (!empty($searchKeyword)) {
- $totalCountQuery->where('title', 'like', '%' . $searchKeyword . '%');
- }
-
- $totalCount = $totalCountQuery->count();
-
- // 计算总页数
- $totalPages = $totalCount > 0 ? ceil($totalCount / $maxCount) : 0;
-
- // 3. 查询当前页的文章列表
- $articlesQuery = Db::table('pr_cms_archives')
- ->where('channel_id', $channelId);
-
- // 如果提供了搜索关键字,添加对title字段的模糊搜索
- if (!empty($searchKeyword)) {
- $articlesQuery->where('title', 'like', '%' . $searchKeyword . '%');
- }
-
- $articles = $articlesQuery
- ->order('createtime DESC')
- ->limit(($page - 1) * $maxCount, $maxCount)
- ->get();
-
- // 返回包含文章列表和总页数的数组
- return [
- 'list' => $articles ?? [],
- 'totalPages' => $totalPages
- ];
- } catch (Exception $e) {
- // 发生异常时返回包含空列表和0总页数的数组
- return ['list' => [], 'totalPages' => 0];
- }
- }
- function loadChildChannelByChannelName($name) {
- try {
- // 1. 从pr_cms_channel表中通过name查询channel_id
- $channel = Db::table('pr_cms_channel')
- ->where('name', $name)
- ->first(['id']);
-
- // 如果没有找到对应的频道,返回空数组
- if (!$channel) {
- return [];
- }
-
- $channelId = $channel['id'];
-
- // 2. 在pr_cms_channel中通过parent_id查询子频道
- $childChannels = Db::table('pr_cms_channel')
- ->where('parent_id', $channelId)
- ->get();
-
- return $childChannels ?? [];
- } catch (Exception $e) {
- // 发生异常时返回空数组
- return [];
- }
- }
- function getContentById($id) {
- try {
- // 1. 从pr_cms_archives表中获取基础信息
- $archive = Db::table('pr_cms_archives')
- ->where('id', $id)
- ->first();
-
- // 如果没有找到对应的归档信息,返回null
- if (!$archive) {
- print_r(1);
- return null;
- }
-
- // 2. 通过model_id从pr_cms_model表中获取table字段
- $model = Db::table('pr_cms_model')
- ->where('id', $archive['model_id'])
- ->first(['table']);
-
- // 如果没有找到对应的模型信息,返回归档信息
- if (!$model || empty($model['table'])) {
- print_r(2);
- return $archive;
- }
-
- // 3. 通过table指定的表通过id查出content
- $contentTable = "pr_".$model['table'];
- $content = Db::table($contentTable)
- ->where('id', $id)
- ->first(['content']);
-
- // 4. 合并返回结果
- if ($content && isset($content['content'])) {
- $archive['content'] = $content['content'];
- }
-
- return $archive;
- } catch (Exception $e) {
- // 发生异常时返回null
- print_r(3);
- print_r($e);
- return null;
- }
- }
|