where('name', $name) ->where('status', 'normal') ->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) ->where('status', 'normal') ->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) ->where('status', 'normal') ->first(['id']); // 如果没有找到对应的频道,返回包含空列表和0总页数的数组 if (!$channel) { return ['list' => [], 'totalPages' => 0]; } $channelId = $channel['id']; // 2. 查询符合条件的文章总数量 $totalCountQuery = Db::table('pr_cms_archives') ->where('channel_id', $channelId) ->where('status', 'normal'); // 如果提供了搜索关键字,添加对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) ->where('status', 'normal'); // 如果提供了搜索关键字,添加对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 loadListByChannelIdInNameAndPage($maxCount, $channelIds, $page, $searchKeyword = '') { try { // 查询符合条件的文章总数量 $totalCountQuery = Db::table('pr_cms_archives') ->in('channel_id', $channelIds) ->where('status', 'normal'); // 如果提供了搜索关键字,添加对title字段的模糊搜索 if (!empty($searchKeyword)) { $totalCountQuery->where('title', 'like', '%' . $searchKeyword . '%'); } $totalCount = $totalCountQuery->count(); // 计算总页数 $totalPages = $totalCount > 0 ? ceil($totalCount / $maxCount) : 0; // 查询当前页的文章列表 $articlesQuery = Db::table('pr_cms_archives') ->in('channel_id', $channelIds) ->where('status', 'normal'); // 如果提供了搜索关键字,添加对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 loadListPage($maxCount, $page, $searchKeyword = '') { try { // 2. 查询符合条件的文章总数量 $totalCountQuery = Db::table('pr_cms_archives') ->where('status', 'normal'); // 如果提供了搜索关键字,添加对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('status', 'normal'); // 如果提供了搜索关键字,添加对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) ->where('status', 'normal') ->first(['id']); // 如果没有找到对应的频道,返回空数组 if (!$channel) { return []; } $channelId = $channel['id']; // 2. 在pr_cms_channel中通过parent_id查询子频道 $childChannels = Db::table('pr_cms_channel') ->where('parent_id', $channelId) ->where('status', 'normal') ->order('weigh DESC') ->get(); return $childChannels ?? []; } catch (Exception $e) { // 发生异常时返回空数组 return []; } } function getContentById($id) { try { // 1. 从pr_cms_archives表中获取基础信息 $archive = Db::table('pr_cms_archives') ->where('id', $id) ->where('status', 'normal') ->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; } } /** * 从pr_cms_block表中获取banner数据 * @return array 轮播图数据数组 */ function getBannerData() { try { // 从pr_cms_block表中查询name=banner的数据 $bannerData = Db::table('pr_cms_block') ->where('name', 'banner') ->where('status', 'normal') ->get(); return $bannerData ?? []; } catch (Exception $e) { // 发生异常时返回空数组 return []; } } /** * 获取flag字段包含recommend的前几篇文章列表 * @param int $maxCount 要获取的文章数量 * @return array 推荐文章列表 */ function getRecommendArticles($maxCount) { try { // 从pr_cms_archives表中查询flag包含recommend的文章 // 先按weigh降序排序,再按createtime降序排序 $articles = Db::table('pr_cms_archives') ->where('status', 'normal') ->where('flag', 'like', '%recommend%') ->order('weigh DESC, createtime DESC') ->limit(0, $maxCount) ->get(); return $articles ?? []; } catch (Exception $e) { // 发生异常时返回空数组 return []; } }