NewsService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 $cateId 分类ID:数组则为多个分类
  83. * @param $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 getListByCate($cateId, $num=6, $field=''){
  91. $field = $field? $field : 'id,title,thumb,keywords,description';
  92. return Db::name('news')
  93. ->where(function($query) use($cateId){
  94. if(is_array($cateId)){
  95. $query->whereIn('ncatid', $cateId);
  96. }else{
  97. $query->where('ncatid', $cateId);
  98. }
  99. })
  100. ->where('status', 1)
  101. ->field($field)
  102. ->order('list_order')
  103. ->limit($num)
  104. ->select();
  105. }
  106. /**
  107. * 获取位置对应的信息列表
  108. * @param $level 位置等级
  109. * @param $num 记录数
  110. * @param string $field 字段
  111. * @return array|\PDOStatement|string|\think\Collection
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. * @throws \think\exception\DbException
  115. */
  116. public static function getListByLevel($level, $num=6, $field=''){
  117. $field = $field? $field : 'id,title,thumb,keywords,description';
  118. return Db::name('news')
  119. ->where(function($query) use($level){
  120. if(is_array($level)){
  121. $query->whereIn('level', $level);
  122. }else{
  123. $query->where('level', $level);
  124. }
  125. })
  126. ->where('status', 1)
  127. ->field($field)
  128. ->order('list_order')
  129. ->limit($num)
  130. ->select();
  131. }
  132. }