NewsService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * 信息服务
  4. * @author wesmielr
  5. */
  6. namespace app\index\service;
  7. use think\Db;
  8. class NewsService
  9. {
  10. /**
  11. * 获取信息列表
  12. * @param $params 参数
  13. * @param int $pageSize 条数
  14. * @param string $field 字段
  15. * @return \think\Paginator
  16. * @throws \think\exception\DbException
  17. */
  18. public static function getList($params, $pageSize=10, $field=''){
  19. $field = $field? $field : 'id,title,thumb,keywords,hits,guanggaowei,description,create_time';
  20. $page = request()->get('page', 1);
  21. $cacheKey = "cache:news:list:p".$page.'_'.md5(json_encode($params).$field.$pageSize);
  22. $dataList = RedisService::get($cacheKey);
  23. if($dataList){
  24. return $dataList;
  25. }
  26. $dataList = Db::name('news')
  27. ->field($field)
  28. ->where(function($query) use($params){
  29. $status = isset($params['status'])? $params['status'] : 0;
  30. if($status){
  31. $query->where('status', $status);
  32. }
  33. $ncatid = isset($params['ncatid'])? $params['ncatid'] : 0;
  34. if($ncatid){
  35. $query->where('ncatid', $ncatid);
  36. }
  37. $level = isset($params['level'])? intval($params['level']) : 0;
  38. if($level){
  39. $query->where('level', $level);
  40. }
  41. $kw = isset($params['kw'])? trim($params['kw']) : '';
  42. if($kw){
  43. $query->where('title', 'like', "%{$kw}%");
  44. }
  45. })
  46. ->order('id desc')
  47. ->paginate($pageSize);
  48. if ($dataList){
  49. RedisService::set($cacheKey, $dataList, 3 * 3600);
  50. }
  51. return $dataList;
  52. }
  53. /**
  54. * 获取热门信息
  55. * @param int $num 记录数
  56. * @param string $field 字段
  57. * @return array|\PDOStatement|string|\think\Collection
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. * @throws \think\exception\DbException
  61. */
  62. public static function getHotList($num=6, $field=''){
  63. $field = $field? $field : 'id,title,thumb,keywords,description,create_time';
  64. $cacheKey = "cache:news:hotlist_".$num;
  65. $dataList = RedisService::get($cacheKey);
  66. if($dataList){
  67. return $dataList;
  68. }
  69. $dataList = Db::name('news')
  70. ->where('status',1)
  71. ->field($field)
  72. ->order('hits desc')
  73. ->limit($num)
  74. ->select();
  75. $dataList = $dataList? $dataList->toArray() : [];
  76. if ($dataList){
  77. RedisService::set($cacheKey, $dataList, 3 * 3600);
  78. }
  79. return $dataList;
  80. }
  81. /**
  82. * 获最新发布信息
  83. * @param int $num 记录数
  84. * @param string $field 字段
  85. * @return array|\PDOStatement|string|\think\Collection
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. public static function getNewList($num=6, $field=''){
  91. $field = $field? $field : 'id,title,thumb,keywords,description,create_time';
  92. $cacheKey = "cache:news:newlist_".$num;
  93. $dataList = RedisService::get($cacheKey);
  94. if($dataList){
  95. return $dataList;
  96. }
  97. $dataList = Db::name('news')
  98. ->where('status',1)
  99. ->field($field)
  100. ->order('update_time desc, create_time desc,id desc')
  101. ->limit($num)
  102. ->select();
  103. $dataList = $dataList? $dataList->toArray() : [];
  104. if ($dataList){
  105. RedisService::set($cacheKey, $dataList, 7*24 * 3600);
  106. }
  107. return $dataList;
  108. }
  109. /**
  110. * 获随机发布信息
  111. * @param int $num 记录数
  112. * @param string $field 字段
  113. * @return array|\PDOStatement|string|\think\Collection
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. * @throws \think\exception\DbException
  117. */
  118. public static function getRandList($num=6, $field=''){
  119. $field = $field? $field : 'id,title,thumb,keywords,description,create_time';
  120. $result = Db::name('news')
  121. ->where('status',1)
  122. ->field($field)
  123. ->order(db()->raw('rand()'))
  124. ->limit($num)
  125. ->select();
  126. return $result? $result->toArray() : [];
  127. }
  128. /**
  129. * 获取分类对应的信息列表
  130. * @param $cateId 分类ID:数组则为多个分类
  131. * @param $num 记录数
  132. * @param string $field 字段
  133. * @return array|\PDOStatement|string|\think\Collection
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\ModelNotFoundException
  136. * @throws \think\exception\DbException
  137. */
  138. public static function getListByCate($cateId, $num=6, $field=''){
  139. $cacheKey = "cache:news:bycate_".$cateId.'_'.$num;
  140. $dataList = RedisService::get($cacheKey);
  141. if($dataList){
  142. return $dataList;
  143. }
  144. $field = $field? $field : 'id,title,thumb,keywords,description';
  145. $dataList = Db::name('news')
  146. ->where(function($query) use($cateId){
  147. if(is_array($cateId)){
  148. $query->whereIn('ncatid', $cateId);
  149. }else{
  150. $query->where('ncatid', $cateId);
  151. }
  152. })
  153. ->where('status', 1)
  154. ->field($field)
  155. ->order('list_order')
  156. ->limit($num)
  157. ->select();
  158. $dataList = $dataList? $dataList->toArray() : [];
  159. if ($dataList){
  160. RedisService::set($cacheKey, $dataList, 7*24 * 3600);
  161. }
  162. return $dataList;
  163. }
  164. /**
  165. * 获取位置对应的信息列表
  166. * @param $level 位置等级
  167. * @param $num 记录数
  168. * @param string $field 字段
  169. * @return array|\PDOStatement|string|\think\Collection
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\ModelNotFoundException
  172. * @throws \think\exception\DbException
  173. */
  174. public static function getListByLevel($level, $num=6, $field=''){
  175. $field = $field? $field : 'id,title,thumb,keywords,description';
  176. $cacheKey = "cache:news:listbylevel_".$level.'_'.$num;
  177. $dataList = RedisService::get($cacheKey);
  178. if($dataList){
  179. return $dataList;
  180. }
  181. $dataList = Db::name('news')
  182. ->where(function($query) use($level){
  183. if(is_array($level)){
  184. $query->whereIn('level', $level);
  185. }else{
  186. $query->where('level', $level);
  187. }
  188. })
  189. ->where('status', 1)
  190. ->field($field)
  191. ->order('list_order')
  192. ->limit($num)
  193. ->select();
  194. $dataList = $dataList? $dataList->toArray() : [];
  195. if ($dataList){
  196. RedisService::set($cacheKey, $dataList, 7*24 * 3600);
  197. }
  198. return $dataList;
  199. }
  200. /**
  201. * 获取资讯阅读排行榜
  202. * @param int $num 数量
  203. * @param string $field 返回字段
  204. * @return array|bool|\PDOStatement|string|\think\Collection
  205. * @throws \think\db\exception\DataNotFoundException
  206. * @throws \think\db\exception\ModelNotFoundException
  207. * @throws \think\exception\DbException
  208. */
  209. public static function getRankList($num=10, $field=''){
  210. $field = $field? $field : 'id,title,thumb,hits';
  211. $cacheKey = "cache:news:ranks_".$num;
  212. $dataList = RedisService::get($cacheKey);
  213. if($dataList){
  214. return $dataList;
  215. }
  216. $dataList = Db::name('news')
  217. ->where('status', 1)
  218. ->field($field)
  219. ->order('hits desc')
  220. ->limit($num)
  221. ->select();
  222. $dataList = $dataList? $dataList->toArray() : [];
  223. if ($dataList){
  224. foreach ($dataList as &$item){
  225. $item['hits'] = intval($item['hits']) + 100;
  226. }
  227. RedisService::set($cacheKey, $dataList, 3600);
  228. }
  229. return $dataList;
  230. }
  231. }