// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\AdModel; use App\Services\BaseService; use App\Services\RedisService; /** * 广告管理-服务类 * Class AdService * @package App\Services\Api */ class AdService extends BaseService { /** * 构造函数 * AdService constructor. */ public function __construct() { $this->model = new AdModel(); } /** * 静态入口 * @return AdService|null */ public static function make() { return parent::make(); // TODO: Change the autogenerated stub } /** * 获取广告列表 * @param int $position 广告位置:1-首页 * @param int $listRows 返回数量 * @return array|false|mixed */ public function getList($position=1, $listRows = 6) { $cacheKey = "caches:adverts:index_{$position}_{$listRows}"; if($list = RedisService::get($cacheKey)){ return $list; } $list = $this->model->where(['position'=> $position,'mark'=>1,'status'=>1]) ->select(['id','title','cover as image','type']) ->orderBy('sort','desc') ->limit($listRows) ->get() ->each(function($item, $k){ $item['image'] = $item['image']? get_image_url($item['image']) : ''; }); $list = $list? $list->toArray() : []; if($list){ RedisService::set($cacheKey, $list, rand(10, 30)); } return $list; } }