Article.php 3.5 KB

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