NewsService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. return Db::name('news')
  21. ->field($field)
  22. ->where(function($query) use($params){
  23. $status = isset($params['status'])? $params['status'] : 0;
  24. if($status){
  25. $query->where('status', $status);
  26. }
  27. $ncatid = isset($params['ncatid'])? $params['ncatid'] : 0;
  28. if($ncatid){
  29. $query->where('ncatid', $ncatid);
  30. }
  31. $level = isset($params['level'])? intval($params['level']) : 0;
  32. if($level){
  33. $query->where('level', $level);
  34. }
  35. $kw = isset($params['kw'])? trim($params['kw']) : '';
  36. if($kw){
  37. $query->where('title', 'like', "%{$kw}%");
  38. }
  39. })
  40. ->order('id desc')
  41. ->paginate($pageSize);
  42. }
  43. /**
  44. * 获取热门信息
  45. * @param int $num 记录数
  46. * @param string $field 字段
  47. * @return array|\PDOStatement|string|\think\Collection
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. public static function getHotList($num=6, $field=''){
  53. $field = $field? $field : 'id,title,thumb,keywords,description,create_time';
  54. return Db::name('news')
  55. ->where('status',1)
  56. ->field($field)
  57. ->order('hits desc')
  58. ->limit($num)
  59. ->select();
  60. }
  61. /**
  62. * 获最新发布信息
  63. * @param int $num 记录数
  64. * @param string $field 字段
  65. * @return array|\PDOStatement|string|\think\Collection
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. */
  70. public static function getNewList($num=6, $field=''){
  71. $field = $field? $field : 'id,title,thumb,keywords,description,create_time';
  72. $result = Db::name('news')
  73. ->where('status',1)
  74. ->field($field)
  75. ->order('update_time desc, create_time desc,id desc')
  76. ->limit($num)
  77. ->select();
  78. return $result? $result->toArray() : [];
  79. }
  80. /**
  81. * 获随机发布信息
  82. * @param int $num 记录数
  83. * @param string $field 字段
  84. * @return array|\PDOStatement|string|\think\Collection
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. * @throws \think\exception\DbException
  88. */
  89. public static function getRandList($num=6, $field=''){
  90. $field = $field? $field : 'id,title,thumb,keywords,description,create_time';
  91. $result = Db::name('news')
  92. ->where('status',1)
  93. ->field($field)
  94. ->order(db()->raw('rand()'))
  95. ->limit($num)
  96. ->select();
  97. return $result? $result->toArray() : [];
  98. }
  99. /**
  100. * 获取分类对应的信息列表
  101. * @param $cateId 分类ID:数组则为多个分类
  102. * @param $num 记录数
  103. * @param string $field 字段
  104. * @return array|\PDOStatement|string|\think\Collection
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. * @throws \think\exception\DbException
  108. */
  109. public static function getListByCate($cateId, $num=6, $field=''){
  110. $field = $field? $field : 'id,title,thumb,keywords,description';
  111. return Db::name('news')
  112. ->where(function($query) use($cateId){
  113. if(is_array($cateId)){
  114. $query->whereIn('ncatid', $cateId);
  115. }else{
  116. $query->where('ncatid', $cateId);
  117. }
  118. })
  119. ->where('status', 1)
  120. ->field($field)
  121. ->order('list_order')
  122. ->limit($num)
  123. ->select();
  124. }
  125. /**
  126. * 获取位置对应的信息列表
  127. * @param $level 位置等级
  128. * @param $num 记录数
  129. * @param string $field 字段
  130. * @return array|\PDOStatement|string|\think\Collection
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. * @throws \think\exception\DbException
  134. */
  135. public static function getListByLevel($level, $num=6, $field=''){
  136. $field = $field? $field : 'id,title,thumb,keywords,description';
  137. return Db::name('news')
  138. ->where(function($query) use($level){
  139. if(is_array($level)){
  140. $query->whereIn('level', $level);
  141. }else{
  142. $query->where('level', $level);
  143. }
  144. })
  145. ->where('status', 1)
  146. ->field($field)
  147. ->order('list_order')
  148. ->limit($num)
  149. ->select();
  150. }
  151. }