PortalPostService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: pl125 <xskjs888@163.com>
  8. // +----------------------------------------------------------------------
  9. namespace api\portal\service;
  10. use api\portal\model\PortalPostModel;
  11. use think\db\Query;
  12. class PortalPostService
  13. {
  14. //模型关联方法
  15. protected $relationFilter = ['user'];
  16. /**
  17. * 文章列表
  18. * @param $filter
  19. * @param bool $isPage
  20. * @return array|string|\think\Collection
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. */
  25. public function postArticles($filter, $isPage = false)
  26. {
  27. $join = [];
  28. $field = empty($filter['field']) ? 'a.*' : explode(',', $filter['field']);
  29. //转为查询条件
  30. if (is_array($field)) {
  31. foreach ($field as $key => $value) {
  32. $field[$key] = 'a.' . $value;
  33. }
  34. $field = implode(',', $field);
  35. }
  36. $page = empty($filter['page']) ? '' : $filter['page'];
  37. $limit = empty($filter['limit']) ? '' : $filter['limit'];
  38. $order = empty($filter['order']) ? ['-update_time'] : explode(',', $filter['order']);
  39. $category = empty($filter['category_id']) ? 0 : intval($filter['category_id']);
  40. if (!empty($category)) {
  41. array_push($join, ['__PORTAL_CATEGORY_POST__ b', 'a.id = b.post_id']);
  42. $field = $field.',b.id AS post_category_id,b.list_order,b.category_id';
  43. }
  44. $orderArr = order_shift($order);
  45. $portalPostModel = new PortalPostModel();
  46. if (!empty($page)) {
  47. $portalPostModel = $portalPostModel->page($page);
  48. } elseif (!empty($limit)) {
  49. $portalPostModel = $portalPostModel->limit($limit);
  50. } else {
  51. $portalPostModel = $portalPostModel->limit(10);
  52. }
  53. $articles = $portalPostModel
  54. ->alias('a')
  55. ->field($field)
  56. ->join($join)
  57. ->where('a.create_time', '>=', 0)
  58. ->where('a.delete_time', 0)
  59. ->where('a.post_status', 1)
  60. ->where(function (Query $query) use ($filter, $isPage) {
  61. if (!empty($filter['user_id'])) {
  62. $query->where('a.user_id', $filter['user_id']);
  63. }
  64. $category = empty($filter['category_id']) ? 0 : intval($filter['category_id']);
  65. if (!empty($category)) {
  66. $query->where('b.category_id', $category);
  67. }
  68. $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
  69. $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
  70. if (!empty($startTime)) {
  71. $query->where('a.published_time', '>=', $startTime);
  72. }
  73. if (!empty($endTime)) {
  74. $query->where('a.published_time', '<=', $endTime);
  75. }
  76. $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
  77. if (!empty($keyword)) {
  78. $query->where('a.post_title', 'like', "%$keyword%");
  79. }
  80. if ($isPage) {
  81. $query->where('a.post_type', 2);
  82. } else {
  83. $query->where('a.post_type', 1);
  84. }
  85. if (!empty($filter['recommended'])) {
  86. $query->where('a.recommended', 1);
  87. }
  88. if (!empty($filter['ids'])) {
  89. $ids = str_to_arr($filter['ids']);
  90. $query->where('a.id', 'in', $ids);
  91. }
  92. })
  93. ->order($orderArr)
  94. ->select();
  95. return $articles;
  96. }
  97. }