Article.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\models\article;
  3. use crmeb\services\SystemConfigService;
  4. use think\facade\Db;
  5. use crmeb\traits\ModelTrait;
  6. use crmeb\basic\BaseModel;
  7. /**
  8. * TODO 文章Model
  9. * Class Article
  10. * @package app\models\article
  11. */
  12. class Article extends BaseModel
  13. {
  14. /**
  15. * 数据表主键
  16. * @var string
  17. */
  18. protected $pk = 'id';
  19. /**
  20. * 模型名称
  21. * @var string
  22. */
  23. protected $name = 'article';
  24. use ModelTrait;
  25. protected function getImageInputAttr($value)
  26. {
  27. return explode(',',$value)?:[];
  28. }
  29. /**
  30. * TODO 获取一条新闻
  31. * @param int $id
  32. * @return array|null|\think\Model
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. * @throws \think\exception\DbException
  36. */
  37. public static function getArticleOne($id = 0){
  38. if(!$id) return [];
  39. $list = self::where('status',1)->where('hide',0)->where('id',$id)->order('id desc')->find();
  40. if($list){
  41. $list = $list->hidden(['hide','status','admin_id','mer_id'])->toArray();
  42. $list["content"] = Db::name('articleContent')->where('nid',$id)->value('content');
  43. return $list;
  44. }
  45. else return [];
  46. }
  47. /**
  48. * TODO 获取某个分类底下的文章
  49. * @param $cid
  50. * @param $page
  51. * @param $limit
  52. * @param string $field
  53. * @return mixed
  54. */
  55. public static function cidByArticleList($cid, $page, $limit, $field = 'id,title,image_input,visit,add_time,synopsis,url')
  56. {
  57. $model=new self();
  58. // if ($cid) $model->where("`cid` LIKE '$cid,%' OR `cid` LIKE '%,$cid,%' OR `cid` LIKE '%,$cid' OR `cid`=$cid ");
  59. if ((int)$cid) $model = $model->where("CONCAT(',',cid,',') LIKE '%,$cid,%'");
  60. $model = $model->field($field);
  61. $model = $model->where('status', 1);
  62. $model = $model->where('hide', 0);
  63. $model = $model->order('sort DESC,add_time DESC');
  64. if($page) $model = $model->page($page, $limit);
  65. return $model->select();
  66. }
  67. /**
  68. * TODO 获取热门文章
  69. * @param string $field
  70. * @return mixed]
  71. */
  72. public static function getArticleListHot($field = 'id,title,image_input,visit,add_time,synopsis,url'){
  73. $model = new self();
  74. $model = $model->field($field);
  75. $model = $model->where('status', 1);
  76. $model = $model->where('hide', 0);
  77. $model = $model->where('is_hot', 1);
  78. $model = $model->order('sort DESC,add_time DESC');
  79. return $model->select();
  80. }
  81. /**
  82. * TODO 获取轮播文章
  83. * @param string $field
  84. * @return mixed
  85. */
  86. public static function getArticleListBanner($field = 'id,title,image_input,visit,add_time,synopsis,url'){
  87. $model = new self();
  88. $model = $model->field($field);
  89. $model = $model->where('status', 1);
  90. $model = $model->where('hide', 0);
  91. $model = $model->where('is_banner', 1);
  92. $model = $model->order('sort DESC,add_time DESC');
  93. $model = $model->limit(SystemConfigService::get('news_slides_limit') ?? 3);
  94. return $model->select();
  95. }
  96. }