// +---------------------------------------------------------------------- namespace App\Services; use App\Models\AdModel; /** * 广告管理-服务类 * @author wesmiler * @since 2020/11/11 * Class AdService * @package App\Services */ class AdService extends BaseService { /** * 构造函数 * @author wesmiler * @since 2020/11/11 * AdService constructor. */ public function __construct() { $this->model = new AdModel(); } /** * 添加或编辑 * @return array * @since 2020/11/11 * @author wesmiler */ public function edit() { $data = request()->all(); // 图片处理 $cover = trim($data['cover']); if (strpos($cover, "temp")) { $data['cover'] = save_image($cover, 'ad'); } else { $data['cover'] = str_replace(IMG_URL, "", $data['cover']); } // 开始时间 if (isset($data['start_time']) && $data['start_time']) { $data['start_time'] = strtotime($data['start_time']); } // 结束时间 if (isset($data['end_time']) && $data['end_time']) { $data['end_time'] = strtotime($data['end_time']); } return parent::edit($data); // TODO: Change the autogenerated stub } /** * 获取广告轮播列表数据 * @param $sortId 广告位ID * @return array|mixed */ public function geListBySort($sortId, $num=6){ $showNum = ConfigService::make()->getConfigByCode("index_ad_{$sortId}_num"); $num = $showNum? $showNum : $num; $cacheKey = "caches:index:adverts:list_sort_{$sortId}_{$num}"; $dataList = RedisService::get($cacheKey); if($dataList){ return $dataList; } $dataList = $this->model::where(['ad_sort_id'=> $sortId,'mark'=> 1,'status'=> 1]) ->where(function($query){ $query->where('start_time',0)->orWhere('end_time', 0)->orWhere(function($query){ $query->where('start_time','>=', time())->where('end_time','<=', time()); }); }) ->select(['id','title','cover','type','description','url','view_num','width','height','status','sort']) ->orderBy('sort','asc') ->orderBy('create_time','desc') ->limit($num) ->get() ->each(function($item, $k){ $item['cover'] = $item['cover']? get_image_url($item['cover']) : ''; }); $dataList = $dataList? $dataList->toArray() :[]; if($dataList){ RedisService::set($cacheKey, $dataList, rand(10, 30)); } return $dataList; } /** * 获取广告位对应单广告数据 * @param $sortId 广告位ID * @return array|mixed */ public function geDataBySort($sortId){ $cacheKey = "caches:index:adverts:data_sort_{$sortId}"; $data = RedisService::get($cacheKey); if($data){ return $data; } $data = $this->model::where(['ad_sort_id'=> $sortId,'mark'=> 1,'status'=> 1]) ->where(function($query){ $query->where('start_time',0)->orWhere('end_time', 0)->orWhere(function($query){ $query->where('start_time','>=', time())->where('end_time','<=', time()); }); }) ->select(['id','title','cover','type','description','url','view_num','width','height','status','sort']) ->orderBy('sort','asc') ->orderBy('create_time','desc') ->first(); $data = $data? $data->toArray() : []; if($data){ $data['cover'] = $data['cover']? get_image_url($data['cover']) : ''; RedisService::set($cacheKey, $data, rand(10, 30)); } return $data; } }