// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\NoticeModel; use App\Services\BaseService; use App\Services\RedisService; /** * 通知公告-服务类 * @author laravel开发员 * @since 2020/11/11 * Class NoticeService * @package App\Services\Api */ class NoticeService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * NoticeService constructor. */ public function __construct() { $this->model = new NoticeModel(); } /** * 静态入口 */ public static function make() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } /** * 列表 * @param int $num * @return array|mixed */ public function getDataList($num = 9) { $cacheKey = "caches:index:notices"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = $this->model->where(['status'=>1,'mark'=>1]) ->select(['id','title','create_time','content','status']) ->limit($num) ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, rand(300, 600)); } return $datas; } /** * 获取详情 * @param $id * @return array|mixed */ public function getInfo($id) { $cacheKey = "caches:notices:info_{$id}"; $info = RedisService::get($cacheKey); if($info){ return $info; } $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1]) ->select(['id','title','author','content','create_time']) ->first(); $info = $info? $info->toArray() : []; if($info){ $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d') : ''; $info['content'] = get_format_content($info['content']); RedisService::set($cacheKey, $info, rand(5,10)); } return $info; } }