| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\NoticeModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 通知公告-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class NoticeService
- * @package App\Services\Common
- */
- class NoticeService extends BaseService
- {
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * NoticeService constructor.
- */
- public function __construct()
- {
- $this->model = new NoticeModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 列表数据
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 10)
- {
- $where = ['a.mark' => 1,'status'=>1];
- $status = isset($params['status'])? $params['status'] : 0;
- if($status>0){
- $where['a.status'] = $status;
- }
- $list = $this->model->from('notice as a')
- ->where($where)
- ->where(function ($query) use($params){
- $keyword = isset($params['keyword'])? $params['keyword'] : '';
- if($keyword){
- $query->where('a.title','like',"%{$keyword}%");
- }
- })
- ->select(['a.*'])
- ->orderBy('a.id','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- foreach($list['data'] as &$item){
- $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
- }
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- /**
- * 设置置顶
- * @return array
- * @since 2020/11/21
- * @author laravel开发员
- */
- public function setIsTop()
- {
- $data = request()->all();
- if (!$data['id']) {
- return message('记录ID不能为空', false);
- }
- if (!$data['is_top']) {
- return message('设置置顶不能为空', false);
- }
- $error = '';
- $item = [
- 'id' => $data['id'],
- 'is_top' => $data['is_top']
- ];
- $rowId = $this->model->edit($item, $error);
- if (!$rowId) {
- return message($error, false);
- }
- return message();
- }
- /**
- * 首页推荐公告
- * @param $num
- * @return array|mixed
- */
- public function getRecommandList($num=20)
- {
- $cacheKey = "caches:notices:{$num}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $datas = $this->model->where(['status'=>1,'mark'=>1])->orderBy('type','desc')->orderBy('create_time','desc')->limit($num)->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- RedisService::set($cacheKey, $datas, rand(10,20));
- }
- return $datas;
- }
- /**
- * @param $id
- * @return array|string
- */
- public function getInfo($id)
- {
- return $this->model->getInfo($id);
- }
- public function edit()
- {
- RedisService::clear("caches:index:notices");
- return parent::edit(); // TODO: Change the autogenerated stub
- }
- public function delete()
- {
- RedisService::clear("caches:index:notices");
- return parent::delete(); // TODO: Change the autogenerated stub
- }
- public function status()
- {
- RedisService::clear("caches:index:notices");
- return parent::status(); // TODO: Change the autogenerated stub
- }
- /**
- * 生成公告
- * @param $title
- * @param string $content
- */
- public function saveNotice($title, $content='')
- {
- $data = [
- 'title'=> $title,
- 'content'=> $content?$content:$title,
- 'type'=> 1,
- 'create_time'=>time(),
- 'update_time'=>time(),
- 'status'=>1,
- 'mark'=>1
- ];
- NoticeModel::insert($data);
- if(NoticeModel::where(['type'=>2])->count('id')>=10){
- NoticeModel::where(['type'=>2])->where('create_time','<=', time() - 600)->delete();
- }
- // 清除缓存
- RedisService::keyDel("caches:notices*");
- RedisService::keyDel("caches:config:app*");
- return true;
- }
- }
|