ArticleService.php 7.0 KB

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