PostService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\portal\service;
  12. use app\portal\model\PortalPostModel;
  13. use think\db\Query;
  14. class PostService
  15. {
  16. /**
  17. * 文章查询
  18. * @param $filter
  19. * @return \think\Paginator
  20. * @throws \think\exception\DbException
  21. */
  22. public function adminArticleList($filter)
  23. {
  24. return $this->adminPostList($filter);
  25. }
  26. /**
  27. * 页面文章列表
  28. * @param $filter
  29. * @return \think\Paginator
  30. * @throws \think\exception\DbException
  31. */
  32. public function adminPageList($filter)
  33. {
  34. return $this->adminPostList($filter, true);
  35. }
  36. /**
  37. * 文章查询
  38. * @param $filter
  39. * @param bool $isPage
  40. * @return \think\Paginator
  41. * @throws \think\exception\DbException
  42. */
  43. public function adminPostList($filter, $isPage = false)
  44. {
  45. $field = 'a.*,u.user_login,u.user_nickname,u.user_email';
  46. $portalPostModel = new PortalPostModel();
  47. $articlesQuery = $portalPostModel->alias('a');
  48. $articlesQuery->join('user u', 'a.user_id = u.id');
  49. $category = empty($filter['category']) ? 0 : intval($filter['category']);
  50. if (!empty($category)) {
  51. $articlesQuery->join('portal_category_post b', 'a.id = b.post_id');
  52. $field = 'a.*,b.id AS post_category_id,b.list_order,b.category_id,u.user_login,u.user_nickname,u.user_email';
  53. }
  54. $articles = $articlesQuery->field($field)
  55. ->where('a.create_time', '>=', 0)
  56. ->where('a.delete_time', 0)
  57. ->where(function (Query $query) use ($filter, $isPage) {
  58. $category = empty($filter['category']) ? 0 : intval($filter['category']);
  59. if (!empty($category)) {
  60. $query->where('b.category_id', $category);
  61. }
  62. $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
  63. $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
  64. if (!empty($startTime)) {
  65. $query->where('a.published_time', '>=', $startTime);
  66. }
  67. if (!empty($endTime)) {
  68. $query->where('a.published_time', '<=', $endTime);
  69. }
  70. $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
  71. if (!empty($keyword)) {
  72. $query->where('a.post_title', 'like', "%$keyword%");
  73. }
  74. if ($isPage) {
  75. $query->where('a.post_type', 2);
  76. } else {
  77. $query->where('a.post_type', 1);
  78. }
  79. })
  80. ->order('update_time', 'DESC')
  81. ->paginate(10);
  82. return $articles;
  83. }
  84. /**
  85. * 已发布文章查询
  86. * @param int $postId 文章id
  87. * @param int $categoryId 分类id
  88. * @return array|string|\think\Model|null
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. * @throws \think\exception\DbException
  92. */
  93. public function publishedArticle($postId, $categoryId = 0)
  94. {
  95. $portalPostModel = new PortalPostModel();
  96. $wherePublishedTime = function (Query $query) {
  97. $query->where('post.published_time', '>', 0)
  98. ->where('post.published_time', '<', time());
  99. };
  100. if (empty($categoryId)) {
  101. $where = [
  102. 'post.post_type' => 1,
  103. 'post.post_status' => 1,
  104. 'post.delete_time' => 0,
  105. 'post.id' => $postId
  106. ];
  107. $article = $portalPostModel->alias('post')->field('post.*')
  108. ->where($where)
  109. ->where($wherePublishedTime)
  110. ->find();
  111. } else {
  112. $where = [
  113. 'post.post_type' => 1,
  114. 'post.post_status' => 1,
  115. 'post.delete_time' => 0,
  116. 'relation.category_id' => $categoryId,
  117. 'relation.post_id' => $postId
  118. ];
  119. $article = $portalPostModel->alias('post')->field('post.*')
  120. ->join('portal_category_post relation', 'post.id = relation.post_id')
  121. ->where($where)
  122. ->where($wherePublishedTime)
  123. ->find();
  124. }
  125. return $article;
  126. }
  127. /**
  128. * 上一篇文章
  129. * @param int $postId 文章id
  130. * @param int $categoryId 分类id
  131. * @return array|string|\think\Model|null
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. * @throws \think\exception\DbException
  135. */
  136. public function publishedPrevArticle($postId, $categoryId = 0)
  137. {
  138. $portalPostModel = new PortalPostModel();
  139. $wherePublishedTime = function (Query $query) {
  140. $query->where('post.published_time', '>', 0)
  141. ->where('post.published_time', '<', time());
  142. };
  143. if (empty($categoryId)) {
  144. $where = [
  145. 'post.post_type' => 1,
  146. 'post.post_status' => 1,
  147. 'post.delete_time' => 0,
  148. ];
  149. $article = $portalPostModel
  150. ->alias('post')
  151. ->field('post.*')
  152. ->where($where)
  153. ->where('post.id', '<', $postId)
  154. ->where($wherePublishedTime)
  155. ->order('id', 'DESC')
  156. ->find();
  157. } else {
  158. $where = [
  159. 'post.post_type' => 1,
  160. 'post.post_status' => 1,
  161. 'post.delete_time' => 0,
  162. 'relation.category_id' => $categoryId,
  163. ];
  164. $article = $portalPostModel
  165. ->alias('post')
  166. ->field('post.*')
  167. ->join('portal_category_post relation', 'post.id = relation.post_id')
  168. ->where($where)
  169. ->where('relation.post_id', '<', $postId)
  170. ->where($wherePublishedTime)
  171. ->order('id', 'DESC')
  172. ->find();
  173. }
  174. return $article;
  175. }
  176. /**
  177. * 下一篇文章
  178. * @param int $postId 文章id
  179. * @param int $categoryId 分类id
  180. * @return array|string|\think\Model|null
  181. * @throws \think\db\exception\DataNotFoundException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. * @throws \think\exception\DbException
  184. */
  185. public function publishedNextArticle($postId, $categoryId = 0)
  186. {
  187. $portalPostModel = new PortalPostModel();
  188. $wherePublishedTime = function (Query $query) {
  189. $query->where('post.published_time', '>', 0)
  190. ->where('post.published_time', '<', time());
  191. };
  192. if (empty($categoryId)) {
  193. $where = [
  194. 'post.post_type' => 1,
  195. 'post.post_status' => 1,
  196. 'post.delete_time' => 0,
  197. ];
  198. $article = $portalPostModel->alias('post')->field('post.*')
  199. ->where($where)
  200. ->where('post.id', '>', $postId)
  201. ->where($wherePublishedTime)
  202. ->order('id', 'ASC')
  203. ->find();
  204. } else {
  205. $where = [
  206. 'post.post_type' => 1,
  207. 'post.post_status' => 1,
  208. 'post.delete_time' => 0,
  209. 'relation.category_id' => $categoryId,
  210. ];
  211. $article = $portalPostModel->alias('post')->field('post.*')
  212. ->join('portal_category_post relation', 'post.id = relation.post_id')
  213. ->where($where)
  214. ->where('relation.post_id', '>', $postId)
  215. ->where($wherePublishedTime)
  216. ->order('id', 'ASC')
  217. ->find();
  218. }
  219. return $article;
  220. }
  221. /**
  222. * 页面管理查询
  223. * @param int $pageId 文章id
  224. * @return array|string|\think\Model|null
  225. * @throws \think\db\exception\DataNotFoundException
  226. * @throws \think\db\exception\ModelNotFoundException
  227. * @throws \think\exception\DbException
  228. */
  229. public function publishedPage($pageId)
  230. {
  231. $where = [
  232. 'post_type' => 2,
  233. 'post_status' => 1,
  234. 'delete_time' => 0,
  235. 'id' => $pageId
  236. ];
  237. $wherePublishedTime = function (Query $query) {
  238. $query->where('published_time', '>', 0)
  239. ->where('published_time', '<', time());
  240. };
  241. $portalPostModel = new PortalPostModel();
  242. $page = $portalPostModel
  243. ->where($where)
  244. ->where($wherePublishedTime)
  245. ->find();
  246. return $page;
  247. }
  248. }