| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- /**
- * 信息服务
- * @author wesmielr
- */
- namespace app\index\service;
- use think\Db;
- class NewsService
- {
- /**
- * 获取信息列表
- * @param $params 参数
- * @param int $pageSize 条数
- * @param string $field 字段
- * @return \think\Paginator
- * @throws \think\exception\DbException
- */
- public static function getList($params, $pageSize=10, $field=''){
- $field = $field? $field : 'id,title,thumb,keywords,hits,guanggaowei,description,create_time';
- $page = request()->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;
- }
- }
|