ArticleService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Services\Content;
  3. use App\Repositories\ArticleRepository;
  4. use App\Repositories\ArticlePropertyReoository;
  5. use Illuminate\Support\Facades\Cache;
  6. class ArticleService
  7. {
  8. /**
  9. * @var ArticleRepository
  10. */
  11. protected $articleRepository;
  12. protected $articlePropertyReoository;
  13. /**
  14. * ArticleAuthService constructor.
  15. * @param $articleRepository
  16. */
  17. public function __construct(ArticleRepository $articleRepository, ArticlePropertyReoository $articlePropertyReoository)
  18. {
  19. $this->articleRepository = $articleRepository;
  20. $this->articlePropertyReoository = $articlePropertyReoository;
  21. }
  22. public function list($key = '', $id = '', $page = '')
  23. {
  24. $where = array();
  25. $map = array();
  26. $where[] = array('is_display','=','1');
  27. $map[] = array('is_display','=','1');
  28. if ($key) {
  29. $where[] = array('title','like','%'.$key.'%');
  30. $map[] = array('title','like','%'.$key.'%');
  31. }
  32. if ($id) {
  33. $where[] = array('type_id','=',$id);
  34. $list=$this->articleRepository->getArticles($where, $page);
  35. } else {
  36. $or_map = $map;
  37. $or_map[] = array('type_id','=','1');
  38. // $map[] = array('parent_id','=','1');
  39. $list=$this->articleRepository->getAllArticles($map, $or_map, $page);
  40. }
  41. if ($list->toArray()) {
  42. foreach ($list as $k => $v) {
  43. $list[$k]->origin_title = $v->title;
  44. $style= '';
  45. if ($v->tit_color) {
  46. $style .='color:'.$v->tit_color.';';
  47. }
  48. if ($v->tit_b=='1') {
  49. $style .='font-weight:bold;';
  50. }
  51. if ($style) {
  52. $list[$k]->title = '<span style='.$style.'>'.$v->title.'</span>';
  53. }
  54. }
  55. }
  56. return $list;
  57. }
  58. //获取指定属性的新闻资讯
  59. public function getPropertyArticles($property_id, $limit = '5')
  60. {
  61. return $this->articleRepository->getPropertyArticles($property_id, $limit);
  62. }
  63. //获取指定分类的新闻资讯
  64. public function getArticlesByType($type_id, $limit = '5')
  65. {
  66. $articles = $this->articleRepository->getArticlesByType($type_id, $limit);
  67. foreach ($articles as $key => $val){
  68. $articles[$key]['url'] = route('news.show', ['id'=>$val->id]);
  69. }
  70. return $articles;
  71. }
  72. //获取前台显示的新闻信息
  73. public function getArticleInfo($id,$fairjob = 0)
  74. {
  75. $page_new = get_subsite_id() == 0 ? 'news.show' : 'jkq.news.show';
  76. $where = array(
  77. 'id' => $id,
  78. 'is_display'=>'1',
  79. );
  80. $article_info = $this->articleRepository->firstWhere($where);
  81. if (empty($article_info)){
  82. return $article_info;
  83. }
  84. if ($article_info) {
  85. $property = $this->articlePropertyReoository->getProperty(['id'=>$article_info->property_id]);
  86. $article_info->property = '';
  87. if ($property) {
  88. $article_info->property = $property->category_name;
  89. }
  90. }
  91. //获取相同分类下的所有新闻列表
  92. $lists = $this->list('', $article_info->type_id);
  93. $pre_info = array();
  94. $nex_info = array();
  95. $article_info->prev = 0;
  96. $article_info->next = 0;
  97. $info_key = -1;
  98. if ($lists->toArray()) {
  99. foreach ($lists as $k => $v) {
  100. if ($v->id==$id) {
  101. $info_key = $k;
  102. }
  103. }
  104. }
  105. $prev = $info_key>-1 && !empty($lists[$info_key-1]) ? $lists[$info_key-1] : 0;
  106. if ($prev) {
  107. $article_info->prev = 1;
  108. $article_info->prev_title = $prev->origin_title;
  109. if($fairjob){
  110. $article_info->prev_url = route('jobfair.new.show', ['id'=>$prev->id]);
  111. }else{
  112. $article_info->prev_url = route($page_new, ['id'=>$prev->id]);
  113. }
  114. }
  115. $next = $info_key>-1 && !empty($lists[$info_key+1]) ? $lists[$info_key+1] : 0;
  116. if ($next) {
  117. $article_info->next = 1;
  118. $article_info->next_title = $next->origin_title;
  119. if($fairjob){
  120. $article_info->next_url = route('jobfair.new.show', ['id'=>$next->id]);
  121. }else{
  122. $article_info->next_url = route($page_new, ['id'=>$next->id]);
  123. }
  124. }
  125. return $article_info;
  126. }
  127. public function incrementData($where, $num, $filed)
  128. {
  129. return $this->articleRepository->incrementData($where, $num, $filed);
  130. }
  131. public function getArticleCache($params, $type = 'home')
  132. {
  133. $lists = Cache::get('article_index_list_'.$type.'_'.get_subsite_id());
  134. if ($lists === null) {
  135. //获取指定分类的资讯信息
  136. $where = array(
  137. array('is_display','=',1)
  138. );
  139. $whereIn = array();
  140. if (array_has($params, 'type_id')) {
  141. if (is_array($params['type_id'])) {
  142. $whereIn['type_id'] = $params['type_id'];
  143. } else {
  144. $where[] = array('type_id', '=',$params['type_id']);
  145. }
  146. }
  147. $order = array(
  148. 'list_order' => 'desc',
  149. 'created_at' => 'desc'
  150. );
  151. $rst = $this->articleRepository->filterArticles($where, $whereIn, $order);
  152. $lists = array();
  153. if ($rst->isNotEmpty()) {
  154. foreach ($rst as $k => $v) {
  155. if (array_has($params, 'titlelen')) {
  156. $dot = '...';
  157. if (array_has($params, 'dot')) {
  158. $dot = $params['dot'];
  159. }
  160. }
  161. $rst[$k]->dot_title = cut_str($v->title, $params['titlelen'], 0, $dot);
  162. $lists[$v->type_id][] = $rst[$k];
  163. }
  164. }
  165. if ($lists && array_has($params, 'limit')) {
  166. if (array_has($params, 'type_id')) {
  167. if (is_array($params['type_id'])) {
  168. foreach ($params['type_id'] as $key => $val) {
  169. if (array_has($lists, $val)) {
  170. $lists[$val] = array_slice($lists[$val], 0, $params['limit'], true);
  171. } else {
  172. $lists[$val] = array();
  173. }
  174. }
  175. } else {
  176. $lists = array_slice($lists, 0, $params['limit'], true);
  177. }
  178. }
  179. }
  180. Cache::put('article_index_list_'.$type.'_'.get_subsite_id(), $lists, '1800');
  181. }
  182. return $lists;
  183. }
  184. }