| 1 |
- <?php
/**
* 广告
* @author wesmielr
*/
namespace app\index\service;
use think\Db;
class AdvertService
{
/**
* 获取对应位置轮播列表
* @param $slideId 广告位ID
* @param int $num 获取数量
* @param string $field 字段
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function getListBySlide($slideId, $num=6, $field=''){
$cacheKey = "cache:advert:byslide_".$slideId.'_'.$num;
$dataList = RedisService::get($cacheKey);
if ($dataList) {
return $dataList;
}
$field = $field? $field : 'id,title,image,url,target';
$dataList = Db::name('slide_item')->where(['status'=> 1])
->where(function($query) use ($slideId){
if(is_array($slideId)){
$query->whereIn('slide_id', $slideId);
}else{
$query->where('slide_id', $slideId);
}
})
->field($field)
->order('list_order')
->limit($num)
->select();
$dataList = $dataList ? $dataList->toArray() : [];
if ($dataList) {
RedisService::set($cacheKey, $dataList, 7 * 24 * 3600);
}
return $dataList;
}
}
|