| 1 |
- <?php
/**
* 友链服务
* @author wesmielr
*/
namespace app\index\service;
use think\Db;
class LinkService
{
/**
* 获取友链列表
* @param $params 参数
* @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 getList($params, $num=6, $field=''){
$cacheKey = "cache:links:list:".md5(json_encode($params).$num.$field);
$dataList = RedisService::get($cacheKey);
if ($dataList) {
return $dataList;
}
$field = $field? $field : 'id,name,catname,url,image,target';
$dataList = Db::name('link')->where(['status'=> 1])
->where(function($query) use($params){
$catname = isset($params['catname'])? trim($params['catname']) : '';
if($catname){
$query->where('catname', $catname);
}
})
->field($field)
->order('list_order')
->limit($num)
->select();
$dataList = $dataList ? $dataList->toArray() : [];
if ($dataList) {
RedisService::set($cacheKey, $dataList, 7 * 24 * 3600);
}
return $dataList;
}
}
|