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