CommonQuery.php 6.5 KB

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