CommonQuery.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 loadListByChannelIdInNameAndPage($maxCount, $channelIds, $page, $searchKeyword = '') {
  74. try {
  75. // 查询符合条件的文章总数量
  76. $totalCountQuery = Db::table('pr_cms_archives')
  77. ->in('channel_id', $channelIds)
  78. ->where('status', 'normal');
  79. // 如果提供了搜索关键字,添加对title字段的模糊搜索
  80. if (!empty($searchKeyword)) {
  81. $totalCountQuery->where('title', 'like', '%' . $searchKeyword . '%');
  82. }
  83. $totalCount = $totalCountQuery->count();
  84. // 计算总页数
  85. $totalPages = $totalCount > 0 ? ceil($totalCount / $maxCount) : 0;
  86. // 查询当前页的文章列表
  87. $articlesQuery = Db::table('pr_cms_archives')
  88. ->in('channel_id', $channelIds)
  89. ->where('status', 'normal');
  90. // 如果提供了搜索关键字,添加对title字段的模糊搜索
  91. if (!empty($searchKeyword)) {
  92. $articlesQuery->where('title', 'like', '%' . $searchKeyword . '%');
  93. }
  94. $articles = $articlesQuery
  95. ->order('createtime DESC')
  96. ->limit(($page - 1) * $maxCount, $maxCount)
  97. ->get();
  98. // 返回包含文章列表和总页数的数组
  99. return [
  100. 'list' => $articles ?? [],
  101. 'totalPages' => $totalPages
  102. ];
  103. } catch (Exception $e) {
  104. // 发生异常时返回包含空列表和0总页数的数组
  105. return ['list' => [], 'totalPages' => 0];
  106. }
  107. }
  108. function loadListPage($maxCount, $page, $searchKeyword = '') {
  109. try {
  110. // 2. 查询符合条件的文章总数量
  111. $totalCountQuery = Db::table('pr_cms_archives')
  112. ->where('status', 'normal');
  113. // 如果提供了搜索关键字,添加对title字段的模糊搜索
  114. if (!empty($searchKeyword)) {
  115. $totalCountQuery->where('title', 'like', '%' . $searchKeyword . '%');
  116. }
  117. $totalCount = $totalCountQuery->count();
  118. // 计算总页数
  119. $totalPages = $totalCount > 0 ? ceil($totalCount / $maxCount) : 0;
  120. // 3. 查询当前页的文章列表
  121. $articlesQuery = Db::table('pr_cms_archives')
  122. ->where('status', 'normal');
  123. // 如果提供了搜索关键字,添加对title字段的模糊搜索
  124. if (!empty($searchKeyword)) {
  125. $articlesQuery->where('title', 'like', '%' . $searchKeyword . '%');
  126. }
  127. $articles = $articlesQuery
  128. ->order('createtime DESC')
  129. ->limit(($page - 1) * $maxCount, $maxCount)
  130. ->get();
  131. // 返回包含文章列表和总页数的数组
  132. return [
  133. 'list' => $articles ?? [],
  134. 'totalPages' => $totalPages
  135. ];
  136. } catch (Exception $e) {
  137. // 发生异常时返回包含空列表和0总页数的数组
  138. return ['list' => [], 'totalPages' => 0];
  139. }
  140. }
  141. function loadChildChannelByChannelName($name) {
  142. try {
  143. // 1. 从pr_cms_channel表中通过name查询channel_id
  144. $channel = Db::table('pr_cms_channel')
  145. ->where('name', $name)
  146. ->where('status', 'normal')
  147. ->first(['id']);
  148. // 如果没有找到对应的频道,返回空数组
  149. if (!$channel) {
  150. return [];
  151. }
  152. $channelId = $channel['id'];
  153. // 2. 在pr_cms_channel中通过parent_id查询子频道
  154. $childChannels = Db::table('pr_cms_channel')
  155. ->where('parent_id', $channelId)
  156. ->where('status', 'normal')
  157. ->order('weigh DESC')
  158. ->get();
  159. return $childChannels ?? [];
  160. } catch (Exception $e) {
  161. // 发生异常时返回空数组
  162. return [];
  163. }
  164. }
  165. function getContentById($id) {
  166. try {
  167. // 1. 从pr_cms_archives表中获取基础信息
  168. $archive = Db::table('pr_cms_archives')
  169. ->where('id', $id)
  170. ->where('status', 'normal')
  171. ->first();
  172. // 如果没有找到对应的归档信息,返回null
  173. if (!$archive) {
  174. print_r(1);
  175. return null;
  176. }
  177. // 2. 通过model_id从pr_cms_model表中获取table字段
  178. $model = Db::table('pr_cms_model')
  179. ->where('id', $archive['model_id'])
  180. ->first(['table']);
  181. // 如果没有找到对应的模型信息,返回归档信息
  182. if (!$model || empty($model['table'])) {
  183. print_r(2);
  184. return $archive;
  185. }
  186. // 3. 通过table指定的表通过id查出content
  187. $contentTable = "pr_".$model['table'];
  188. $content = Db::table($contentTable)
  189. ->where('id', $id)
  190. ->first(['content']);
  191. // 4. 合并返回结果
  192. if ($content && isset($content['content'])) {
  193. $archive['content'] = $content['content'];
  194. }
  195. return $archive;
  196. } catch (Exception $e) {
  197. // 发生异常时返回null
  198. print_r(3);
  199. print_r($e);
  200. return null;
  201. }
  202. }
  203. /**
  204. * 从pr_cms_block表中获取banner数据
  205. * @return array 轮播图数据数组
  206. */
  207. function getBannerData() {
  208. try {
  209. // 从pr_cms_block表中查询name=banner的数据
  210. $bannerData = Db::table('pr_cms_block')
  211. ->where('name', 'banner')
  212. ->where('status', 'normal')
  213. ->get();
  214. return $bannerData ?? [];
  215. } catch (Exception $e) {
  216. // 发生异常时返回空数组
  217. return [];
  218. }
  219. }
  220. /**
  221. * 获取flag字段包含recommend的前几篇文章列表
  222. * @param int $maxCount 要获取的文章数量
  223. * @return array 推荐文章列表
  224. */
  225. function getRecommendArticles($maxCount) {
  226. try {
  227. // 从pr_cms_archives表中查询flag包含recommend的文章
  228. // 先按weigh降序排序,再按createtime降序排序
  229. $articles = Db::table('pr_cms_archives')
  230. ->where('status', 'normal')
  231. ->where('flag', 'like', '%recommend%')
  232. ->order('weigh DESC, createtime DESC')
  233. ->limit(0, $maxCount)
  234. ->get();
  235. return $articles ?? [];
  236. } catch (Exception $e) {
  237. // 发生异常时返回空数组
  238. return [];
  239. }
  240. }