CommonQuery.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. require 'Db.class.php';
  3. function loadListByChannelName($maxCount, $name) {
  4. try {
  5. // 1. 从pr_cms_channel表中通过name查询channel_id
  6. $channel = Db::table('pr_cms_channel')
  7. ->where('name', $name)
  8. ->first(['id']);
  9. // 如果没有找到对应的频道,返回空数组
  10. if (!$channel) {
  11. return [];
  12. }
  13. $channelId = $channel['id'];
  14. // 2. 在pr_cms_archives中通过channel_id按createtime倒序查询指定数量的最新文章
  15. $articles = Db::table('pr_cms_archives')
  16. ->where('channel_id', $channelId)
  17. ->order('createtime DESC')
  18. ->limit(0, $maxCount)
  19. ->get();
  20. return $articles ?? [];
  21. } catch (Exception $e) {
  22. // 发生异常时返回空数组
  23. return [];
  24. }
  25. }
  26. function loadListByChannelNameAndPage($maxCount, $name, $page, $searchKeyword = '') {
  27. try {
  28. // 1. 从pr_cms_channel表中通过name查询channel_id
  29. $channel = Db::table('pr_cms_channel')
  30. ->where('name', $name)
  31. ->first(['id']);
  32. // 如果没有找到对应的频道,返回包含空列表和0总页数的数组
  33. if (!$channel) {
  34. return ['list' => [], 'totalPages' => 0];
  35. }
  36. $channelId = $channel['id'];
  37. // 2. 查询符合条件的文章总数量
  38. $totalCountQuery = Db::table('pr_cms_archives')
  39. ->where('channel_id', $channelId);
  40. // 如果提供了搜索关键字,添加对title字段的模糊搜索
  41. if (!empty($searchKeyword)) {
  42. $totalCountQuery->where('title', 'like', '%' . $searchKeyword . '%');
  43. }
  44. $totalCount = $totalCountQuery->count();
  45. // 计算总页数
  46. $totalPages = $totalCount > 0 ? ceil($totalCount / $maxCount) : 0;
  47. // 3. 查询当前页的文章列表
  48. $articlesQuery = Db::table('pr_cms_archives')
  49. ->where('channel_id', $channelId);
  50. // 如果提供了搜索关键字,添加对title字段的模糊搜索
  51. if (!empty($searchKeyword)) {
  52. $articlesQuery->where('title', 'like', '%' . $searchKeyword . '%');
  53. }
  54. $articles = $articlesQuery
  55. ->order('createtime DESC')
  56. ->limit(($page - 1) * $maxCount, $maxCount)
  57. ->get();
  58. // 返回包含文章列表和总页数的数组
  59. return [
  60. 'list' => $articles ?? [],
  61. 'totalPages' => $totalPages
  62. ];
  63. } catch (Exception $e) {
  64. // 发生异常时返回包含空列表和0总页数的数组
  65. return ['list' => [], 'totalPages' => 0];
  66. }
  67. }
  68. function loadChildChannelByChannelName($name) {
  69. try {
  70. // 1. 从pr_cms_channel表中通过name查询channel_id
  71. $channel = Db::table('pr_cms_channel')
  72. ->where('name', $name)
  73. ->first(['id']);
  74. // 如果没有找到对应的频道,返回空数组
  75. if (!$channel) {
  76. return [];
  77. }
  78. $channelId = $channel['id'];
  79. // 2. 在pr_cms_channel中通过parent_id查询子频道
  80. $childChannels = Db::table('pr_cms_channel')
  81. ->where('parent_id', $channelId)
  82. ->get();
  83. return $childChannels ?? [];
  84. } catch (Exception $e) {
  85. // 发生异常时返回空数组
  86. return [];
  87. }
  88. }
  89. function getContentById($id) {
  90. try {
  91. // 1. 从pr_cms_archives表中获取基础信息
  92. $archive = Db::table('pr_cms_archives')
  93. ->where('id', $id)
  94. ->first();
  95. // 如果没有找到对应的归档信息,返回null
  96. if (!$archive) {
  97. print_r(1);
  98. return null;
  99. }
  100. // 2. 通过model_id从pr_cms_model表中获取table字段
  101. $model = Db::table('pr_cms_model')
  102. ->where('id', $archive['model_id'])
  103. ->first(['table']);
  104. // 如果没有找到对应的模型信息,返回归档信息
  105. if (!$model || empty($model['table'])) {
  106. print_r(2);
  107. return $archive;
  108. }
  109. // 3. 通过table指定的表通过id查出content
  110. $contentTable = "pr_".$model['table'];
  111. $content = Db::table($contentTable)
  112. ->where('id', $id)
  113. ->first(['content']);
  114. // 4. 合并返回结果
  115. if ($content && isset($content['content'])) {
  116. $archive['content'] = $content['content'];
  117. }
  118. return $archive;
  119. } catch (Exception $e) {
  120. // 发生异常时返回null
  121. print_r(3);
  122. print_r($e);
  123. return null;
  124. }
  125. }