get('page', 1); $cacheKey = "cache:news:list:p".$page.'_'.md5(json_encode($params).$field.$pageSize); $dataList = RedisService::get($cacheKey); if($dataList){ return $dataList; } $dataList = Db::name('news') ->field($field) ->where(function($query) use($params){ $status = isset($params['status'])? $params['status'] : 0; if($status){ $query->where('status', $status); } $ncatid = isset($params['ncatid'])? $params['ncatid'] : 0; if($ncatid){ $query->where('ncatid', $ncatid); } $level = isset($params['level'])? intval($params['level']) : 0; if($level){ $query->where('level', $level); } $kw = isset($params['kw'])? trim($params['kw']) : ''; if($kw){ $query->where('title', 'like', "%{$kw}%"); } }) ->order('id desc') ->paginate($pageSize); if ($dataList){ RedisService::set($cacheKey, $dataList, 3 * 3600); } return $dataList; } /** * 获取热门信息 * @param int $num 记录数 * @param string $field 字段 * @return array|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getHotList($num=6, $field=''){ $field = $field? $field : 'id,title,thumb,keywords,description,create_time'; $cacheKey = "cache:news:hotlist_".$num; $dataList = RedisService::get($cacheKey); if($dataList){ return $dataList; } $dataList = Db::name('news') ->where('status',1) ->field($field) ->order('hits desc') ->limit($num) ->select(); $dataList = $dataList? $dataList->toArray() : []; if ($dataList){ RedisService::set($cacheKey, $dataList, 3 * 3600); } return $dataList; } /** * 获最新发布信息 * @param int $num 记录数 * @param string $field 字段 * @return array|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getNewList($num=6, $field=''){ $field = $field? $field : 'id,title,thumb,keywords,description,create_time'; $cacheKey = "cache:news:newlist_".$num; $dataList = RedisService::get($cacheKey); if($dataList){ return $dataList; } $dataList = Db::name('news') ->where('status',1) ->field($field) ->order('update_time desc, create_time desc,id desc') ->limit($num) ->select(); $dataList = $dataList? $dataList->toArray() : []; if ($dataList){ RedisService::set($cacheKey, $dataList, 7*24 * 3600); } return $dataList; } /** * 获随机发布信息 * @param int $num 记录数 * @param string $field 字段 * @return array|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getRandList($num=6, $field=''){ $field = $field? $field : 'id,title,thumb,keywords,description,create_time'; $result = Db::name('news') ->where('status',1) ->field($field) ->order(db()->raw('rand()')) ->limit($num) ->select(); return $result? $result->toArray() : []; } /** * 获取分类对应的信息列表 * @param $cateId 分类ID:数组则为多个分类 * @param $num 记录数 * @param string $field 字段 * @return array|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getListByCate($cateId, $num=6, $field=''){ $cacheKey = "cache:news:bycate_".$cateId.'_'.$num; $dataList = RedisService::get($cacheKey); if($dataList){ return $dataList; } $field = $field? $field : 'id,title,thumb,keywords,description'; $dataList = Db::name('news') ->where(function($query) use($cateId){ if(is_array($cateId)){ $query->whereIn('ncatid', $cateId); }else{ $query->where('ncatid', $cateId); } }) ->where('status', 1) ->field($field) ->order('list_order') ->limit($num) ->select(); $dataList = $dataList? $dataList->toArray() : []; if ($dataList){ RedisService::set($cacheKey, $dataList, 7*24 * 3600); } return $dataList; } /** * 获取位置对应的信息列表 * @param $level 位置等级 * @param $num 记录数 * @param string $field 字段 * @return array|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getListByLevel($level, $num=6, $field=''){ $field = $field? $field : 'id,title,thumb,keywords,description'; $cacheKey = "cache:news:listbylevel_".$level.'_'.$num; $dataList = RedisService::get($cacheKey); if($dataList){ return $dataList; } $dataList = Db::name('news') ->where(function($query) use($level){ if(is_array($level)){ $query->whereIn('level', $level); }else{ $query->where('level', $level); } }) ->where('status', 1) ->field($field) ->order('list_order') ->limit($num) ->select(); $dataList = $dataList? $dataList->toArray() : []; if ($dataList){ RedisService::set($cacheKey, $dataList, 7*24 * 3600); } return $dataList; } /** * 获取资讯阅读排行榜 * @param int $num 数量 * @param string $field 返回字段 * @return array|bool|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getRankList($num=10, $field=''){ $field = $field? $field : 'id,title,thumb,hits'; $cacheKey = "cache:news:ranks_".$num; $dataList = RedisService::get($cacheKey); if($dataList){ return $dataList; } $dataList = Db::name('news') ->where('status', 1) ->field($field) ->order('hits desc') ->limit($num) ->select(); $dataList = $dataList? $dataList->toArray() : []; if ($dataList){ foreach ($dataList as &$item){ $item['hits'] = intval($item['hits']) + 100; } RedisService::set($cacheKey, $dataList, 3600); } return $dataList; } }