NoticeServer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/11/8
  6. * Time: 10:03
  7. */
  8. namespace App\Services\Content;
  9. use App\Repositories\NoticeRepository;
  10. class NoticeServer
  11. {
  12. protected $noticeRepository;
  13. /**
  14. * NoticeServer constructor.
  15. * @param $noticeRepository
  16. */
  17. public function __construct(NoticeRepository $noticeRepository)
  18. {
  19. $this->noticeRepository = $noticeRepository;
  20. }
  21. public function getLists($where, $page_num = '')
  22. {
  23. $lists = $this->noticeRepository->getNotices($where, $page_num);
  24. if ($lists->toArray()) {
  25. foreach ($lists as $k => $v) {
  26. $lists[$k]->origin_title = $v->title;
  27. $style_font = '';
  28. $style_color ='';
  29. if ($v->tit_b == 1) {
  30. $style_font = "font-weight:bold;";
  31. }
  32. if ($v->tit_color) {
  33. $style_color = 'color:'.$v->tit_color.';';
  34. }
  35. if ($style_font || $style_color) {
  36. $lists[$k]->title = "<span style=".$style_color.$style_font.">".$v->title."</span>";
  37. }
  38. if ($v->is_url) {
  39. $lists[$k]->url = $v->is_url;
  40. } else {
  41. $lists[$k]->url = route('notice.show', array('id'=>$v->id));
  42. }
  43. }
  44. }
  45. return $lists;
  46. }
  47. public function getNotice($where)
  48. {
  49. $notice_info = $this->noticeRepository->getNotice($where);
  50. if (!$notice_info) {
  51. return null;
  52. }
  53. $notice_info->prev = 0;
  54. $notice_info->next = 0;
  55. $map[] = array('is_display','=','1');
  56. //$map[] = array('type_id','=',$notice_info->type_id);
  57. $lists = $this->getLists($map);
  58. if ($lists->isNotEmpty()) {
  59. $info_key = -1;
  60. foreach ($lists as $k => $v) {
  61. if ($v->id==$notice_info->id) {
  62. $info_key = $k;
  63. }
  64. }
  65. $pre_info = array();
  66. $nex_info = array();
  67. if ($info_key>-1 && !empty($lists[$info_key-1])) {
  68. $prev = $lists[$info_key-1];
  69. if ($prev) {
  70. $notice_info->prev = 1;
  71. $notice_info->prev_title = $prev->origin_title;
  72. $notice_info->prev_url = route('notice.show', ['id'=>$prev->id]);
  73. }
  74. }
  75. if ($info_key>-1 && !empty($lists[$info_key+1])) {
  76. $next = $lists[$info_key+1];
  77. if ($next) {
  78. $notice_info->next = 1;
  79. $notice_info->next_title = $next->origin_title;
  80. $notice_info->next_url = route('notice.show', ['id'=>$next->id]);
  81. }
  82. }
  83. }
  84. return $notice_info;
  85. }
  86. public function incrementData($where, $num, $filed)
  87. {
  88. return $this->noticeRepository->incrementData($where, $num, $filed);
  89. }
  90. public function getNotices($params)
  91. {
  92. $where = array(
  93. array('is_display','=',1)
  94. );
  95. if (array_has($params, 'type_id')) {
  96. $where[] = array('type_id','=',$params['type_id']);
  97. }
  98. $limit = '';
  99. if (array_has($params, 'size')) {
  100. $limit = $params['size'];
  101. }
  102. $order = array('sort'=>'desc','created_at'=>'desc');
  103. $rst = $this->noticeRepository->getLimitNotices($where, $order, $limit);
  104. if ($rst->isNotEmpty() && array_has($params, 'titlelen')) {
  105. $dot = '...';
  106. if (array_has($params, 'dot')) {
  107. $dot = $params['dot'];
  108. }
  109. foreach ($rst as $k => $v) {
  110. $rst[$k]->dot_title = cut_str($v->title, $params['titlelen'], 0, $dot);
  111. }
  112. }
  113. return $rst;
  114. }
  115. }