NoticeService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\NoticeModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 通知公告-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * Class NoticeService
  19. * @package App\Services\Common
  20. */
  21. class NoticeService extends BaseService
  22. {
  23. protected static $instance = null;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * NoticeService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new NoticeModel();
  33. }
  34. /**
  35. * 静态入口
  36. * @return static|null
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = (new static());
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 获取挂单广告列表
  47. * @param $params 参数
  48. * @param int $pageSize 分页大小:默认 15
  49. * @return array
  50. */
  51. public function getDataList($params, $pageSize = 15, $field=[])
  52. {
  53. $where = ['a.mark' => 1];
  54. $status = isset($params['status'])? $params['status'] : 0;
  55. if($status>0){
  56. $where['a.status'] = $status;
  57. }
  58. $list = $this->model->from('notice as a')
  59. ->where($where)
  60. ->where(function($query) use($params){
  61. $title = isset($params['title'])? trim($params['title']) : '';
  62. if($title){
  63. $query->where('a.title','like',"%{$title}%");
  64. }
  65. })
  66. ->select($field? $field : ['a.*'])
  67. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  68. $list = $list? $list->toArray() :[];
  69. if($list){
  70. foreach($list['data'] as &$item){
  71. $item['create_time_text'] = $item['create_time']? datetime($item['create_time']):'';
  72. }
  73. }
  74. return [
  75. 'pageSize'=> $pageSize,
  76. 'total'=>isset($list['total'])? $list['total'] : 0,
  77. 'list'=> isset($list['data'])? $list['data'] : []
  78. ];
  79. }
  80. /**
  81. * 获取最新公告列表
  82. * @param int $limit
  83. * @return mixed
  84. */
  85. public function getNewList($limit=6)
  86. {
  87. return $this->model->from('notice as a')->where(['status'=>1,'mark'=>1])->take($limit)->pluck('title');
  88. }
  89. /**
  90. * 设置置顶
  91. * @return array
  92. * @since 2020/11/21
  93. * @author laravel开发员
  94. */
  95. public function setIsTop()
  96. {
  97. $data = request()->all();
  98. if (!$data['id']) {
  99. return message('记录ID不能为空', false);
  100. }
  101. if (!$data['is_top']) {
  102. return message('设置置顶不能为空', false);
  103. }
  104. $error = '';
  105. $item = [
  106. 'id' => $data['id'],
  107. 'is_top' => $data['is_top']
  108. ];
  109. $rowId = $this->model->edit($item, $error);
  110. if (!$rowId) {
  111. return message($error, false);
  112. }
  113. return message();
  114. }
  115. }