// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\ActionLogModel; use App\Models\AdModel; use App\Services\BaseService; use App\Services\RedisService; /** * 广告管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class AdService * @package App\Services\Common */ class AdService extends BaseService { /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * AdService constructor. */ public function __construct() { $this->model = new AdModel(); } /** * 添加或编辑 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { $data = request()->all(); // 图片处理 $cover = trim($data['cover']); if (strpos($cover, "temp")) { $data['cover'] = str_replace(IMG_URL, "", $data['cover']); } ActionLogModel::setTitle("添加或编辑轮播广告"); ActionLogModel::record(); return parent::edit($data); // TODO: Change the autogenerated stub } /** * 按未知获取广告 * @param $position 广告位 * @param $locale 语言:zh-中文,en-英文 * @param int $limit * @return array|mixed */ public function getListByType($position, $locale, $limit=6) { $cahceKey = "caches:adverts:{$position}_{$locale}_{$limit}"; $datas = RedisService::get($cahceKey); if($datas){ return $datas; } $datas = $this->model->where(['position'=> $position,'locale'=> $locale,'status'=>1,'mark'=>1]) ->select(['id','title','cover','locale','sort','url','status']) ->limit($limit? $limit : 6) ->get(); $datas = $datas? $datas->toArray() : []; if($datas){ foreach ($datas as &$item){ $item['cover'] = $item['cover']? get_image_url($item['cover']) : ''; } RedisService::set($cahceKey, $datas, rand(3600, 7200)); } return $datas; } }