NewsService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 $cateId 分类ID:数组则为多个分类
  64. * @param $num 记录数
  65. * @param string $field 字段
  66. * @return array|\PDOStatement|string|\think\Collection
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. * @throws \think\exception\DbException
  70. */
  71. public static function getListByCate($cateId, $num=6, $field=''){
  72. $field = $field? $field : 'id,title,thumb,keywords,description';
  73. return Db::name('news')
  74. ->where(function($query) use($cateId){
  75. if(is_array($cateId)){
  76. $query->whereIn('ncatid', $cateId);
  77. }else{
  78. $query->where('ncatid', $cateId);
  79. }
  80. })
  81. ->where('status', 1)
  82. ->field($field)
  83. ->order('list_order')
  84. ->limit($num)
  85. ->select();
  86. }
  87. /**
  88. * 获取位置对应的信息列表
  89. * @param $level 位置等级
  90. * @param $num 记录数
  91. * @param string $field 字段
  92. * @return array|\PDOStatement|string|\think\Collection
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. * @throws \think\exception\DbException
  96. */
  97. public static function getListByLevel($level, $num=6, $field=''){
  98. $field = $field? $field : 'id,title,thumb,keywords,description';
  99. return Db::name('news')
  100. ->where(function($query) use($level){
  101. if(is_array($level)){
  102. $query->whereIn('level', $level);
  103. }else{
  104. $query->where('level', $level);
  105. }
  106. })
  107. ->where('status', 1)
  108. ->field($field)
  109. ->order('list_order')
  110. ->limit($num)
  111. ->select();
  112. }
  113. }