PostsLogic.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * PostLogic.php UTF-8
  4. * 文章逻辑
  5. *
  6. * @date : 2017/11/23 21:22
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huomp\logic\posts;
  13. use huo\model\common\CommonModel;
  14. use huo\model\posts\PostsModel;
  15. use huolib\constant\CommonConst;
  16. use huolib\tool\StrUtils;
  17. class PostsLogic extends CommonModel {
  18. protected $base_field
  19. = [
  20. 'id' => 'id',
  21. 'post_title' => 'title',
  22. 'post_excerpt' => 'excerpt',
  23. 'post_content' => 'content',
  24. ];
  25. /**
  26. * 获取问答列表
  27. *
  28. * @param array $where
  29. * @param string $page
  30. * @param string $order
  31. *
  32. * @return array|int
  33. */
  34. public function getList($where = [], $page = '1,10', $order = '') {
  35. $_map = [];
  36. if (!empty($where['post_type'])) {
  37. $_map['post_type'] = $where['post_type'];
  38. }
  39. $_posts_model = new PostsModel();
  40. $_count = $_posts_model->where($_map)->count();
  41. if (empty($_count)) {
  42. return [
  43. 'count' => 0,
  44. 'list' => []
  45. ];
  46. }
  47. $_field = $this->base_field;
  48. $_order = $this->orderFilter($order);
  49. $_datas = $_posts_model->field($_field)->where($_map)->page($page)->order($_order)->select();
  50. if (is_object($_datas)) {
  51. $_datas = $_datas->toArray();
  52. }
  53. $_rdata['count'] = $_count;
  54. $_rdata['list'] = $_datas;
  55. return $_rdata;
  56. }
  57. /**
  58. * 后台猎人平台列表
  59. *
  60. * @param array $where
  61. * @param string $page
  62. * @param string $order
  63. *
  64. * @return array
  65. */
  66. public function getHunterList($where = [], $page = '1,10', $order = '-published_time') {
  67. $_map = [];
  68. if (!empty($param['start_time']) && !empty($param['start_time'])) {
  69. $_map['published_time'] = ['between', [strtotime($param['start_time']),
  70. CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])]];
  71. } else if (!empty($param['start_time'])) {
  72. $_map['published_time'] = ['gt', strtotime($param['start_time'])];
  73. } else if (!empty($param['end_time'])) {
  74. $_map['published_time'] = ['lt', CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])];
  75. }
  76. if (!empty($where['post_type'])) {
  77. $_map['post_type'] = $where['post_type'];
  78. }
  79. if (!empty($where['keyword'])) {
  80. $_map['post_title'] = ['like', $where['keyword'].'%'];
  81. }
  82. $_map['delete_time'] = 0;
  83. $_posts_model = new PostsModel();
  84. $_count = $_posts_model->useGlobalScope(false)->where($_map)->count();
  85. if (empty($_count)) {
  86. return [
  87. 'count' => 0,
  88. 'list' => []
  89. ];
  90. }
  91. $_order = $this->orderFilter($order);
  92. $_datas = $_posts_model->useGlobalScope(false)
  93. ->with('user')
  94. ->where($_map)
  95. ->page($page)
  96. ->order($_order)
  97. ->select();
  98. if (is_object($_datas)) {
  99. $_datas = $_datas->toArray();
  100. }
  101. $_rdata['count'] = $_count;
  102. $_rdata['list'] = $_datas;
  103. return $_rdata;
  104. }
  105. /**
  106. * 获取列表用于生成静态页面
  107. *
  108. * @param string $post_type
  109. * @param string $order
  110. *
  111. * @return array
  112. */
  113. public function getHtmlList($post_type, $order = '-published_time') {
  114. $_map = [];
  115. $_map['post_type'] = $post_type;
  116. $_posts_model = new PostsModel();
  117. $_count = $_posts_model->where($_map)->count();
  118. if (empty($_count)) {
  119. return [
  120. 'count' => 0,
  121. 'list' => []
  122. ];
  123. }
  124. $_field = 'id,is_top,post_like,url,post_title,post_keywords,post_source,post_excerpt,more,update_time';
  125. $_order = $this->orderFilter($order);
  126. $_datas = $_posts_model->field($_field)->where($_map)->order($_order)->select();
  127. if (is_object($_datas)) {
  128. $_datas = $_datas->toArray();
  129. }
  130. if (empty($_datas)) {
  131. return [
  132. 'count' => 0,
  133. 'list' => []
  134. ];
  135. }
  136. foreach ($_datas as $_k => $_v) {
  137. $_datas[$_k]['id'] = $_v['id'];
  138. $_datas[$_k]['title'] = $_v['post_title'];
  139. $_datas[$_k]['img'] = empty($_v['more']['thumbnail']) ? '' : $_v['more']['thumbnail'];
  140. $_datas[$_k]['down_cnt'] = StrUtils::formatNumber($_v['post_like'] / 1000);
  141. $_datas[$_k]['size'] = $_v['post_source'];
  142. $_datas[$_k]['post_excerpt'] = $_v['post_excerpt'];
  143. $_datas[$_k]['url'] = $_v['url'];
  144. $_datas[$_k]['update_time'] = $_v['update_time'];
  145. $_datas[$_k]['tags'] = empty($_v['post_keywords']) ? [] : explode(',', $_v['post_keywords']);
  146. }
  147. $_rdata['count'] = $_count;
  148. $_rdata['list'] = $_datas;
  149. return $_rdata;
  150. }
  151. /**
  152. * 获取所有可显示的id
  153. *
  154. * @param array $post_type
  155. *
  156. * @return array
  157. */
  158. public function getAllIds($post_type = []) {
  159. $_map = [
  160. 'post_type' => ['in', $post_type],
  161. ];
  162. $_ids = (new PostsModel())->where($_map)->column('id');
  163. return $_ids;
  164. }
  165. }