NoticeService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. use App\Services\RedisService;
  15. use App\Models\ActionLogModel;
  16. /**
  17. * 通知公告-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class NoticeService
  21. * @package App\Services\Common
  22. */
  23. class NoticeService extends BaseService
  24. {
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * NoticeService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new NoticeModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 列表数据
  49. * @param $params
  50. * @param int $pageSize
  51. * @return array
  52. */
  53. public function getDataList($params, $pageSize = 15)
  54. {
  55. $where = ['a.mark' => 1,'status'=>1];
  56. $status = isset($params['status'])? $params['status'] : 0;
  57. if($status>0){
  58. $where['a.status'] = $status;
  59. }
  60. $list = $this->model->from('notice as a')
  61. ->where($where)
  62. ->where(function ($query) use($params){
  63. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  64. if($keyword){
  65. $query->where('a.title','like',"%{$keyword}%");
  66. }
  67. })
  68. ->select(['a.*'])
  69. ->orderBy('a.id','desc')
  70. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  71. $list = $list? $list->toArray() :[];
  72. if($list){
  73. foreach($list['data'] as &$item){
  74. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  75. }
  76. }
  77. return [
  78. 'pageSize'=> $pageSize,
  79. 'total'=>isset($list['total'])? $list['total'] : 0,
  80. 'list'=> isset($list['data'])? $list['data'] : []
  81. ];
  82. }
  83. /**
  84. * 设置置顶
  85. * @return array
  86. * @since 2020/11/21
  87. * @author laravel开发员
  88. */
  89. public function setIsTop()
  90. {
  91. $data = request()->all();
  92. if (!$data['id']) {
  93. return message('记录ID不能为空', false);
  94. }
  95. if (!$data['is_top']) {
  96. return message('设置置顶不能为空', false);
  97. }
  98. $error = '';
  99. $item = [
  100. 'id' => $data['id'],
  101. 'is_top' => $data['is_top']
  102. ];
  103. $rowId = $this->model->edit($item, $error);
  104. if (!$rowId) {
  105. return message($error, false);
  106. }
  107. return message();
  108. }
  109. /**
  110. * 首页推荐公告
  111. * @param $num
  112. * @return array|mixed
  113. */
  114. public function getRecommandList($num=20)
  115. {
  116. $cacheKey = "caches:notices:{$num}";
  117. $datas = RedisService::get($cacheKey);
  118. if($datas){
  119. return $datas;
  120. }
  121. $datas = $this->model->where(['status'=>1,'mark'=>1])->orderBy('create_time','desc')->limit($num)->get();
  122. $datas = $datas? $datas->toArray() : [];
  123. if($datas){
  124. RedisService::set($cacheKey, $datas, rand(10,20));
  125. }
  126. return $datas;
  127. }
  128. /**
  129. * @param $id
  130. * @return array|string
  131. */
  132. public function getInfo($id)
  133. {
  134. return $this->model->getInfo($id);
  135. }
  136. public function edit()
  137. {
  138. RedisService::clear("caches:index:notices");
  139. return parent::edit(); // TODO: Change the autogenerated stub
  140. }
  141. public function delete()
  142. {
  143. // 设置日志标题
  144. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除通知公告信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  145. ActionLogModel::record();
  146. RedisService::clear("caches:index:notices");
  147. return parent::delete(); // TODO: Change the autogenerated stub
  148. }
  149. public function status()
  150. {
  151. RedisService::clear("caches:index:notices");
  152. return parent::status(); // TODO: Change the autogenerated stub
  153. }
  154. }