Article.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. public static function getArticleOne($id = 0)
  55. {
  56. if (!$id) return [];
  57. $list = self::where('status', 1)->where('hide', 0)->where('id', $id)->order('id desc')->find();
  58. if ($list) {
  59. $list->store_info = $list->profile ? $list->profile->toArray() : null;
  60. $list = $list->hidden(['hide', 'status', 'admin_id', 'mer_id'])->toArray();
  61. // 修改这里:去除 htmlspecialchars_decode 调用
  62. $list["content"] = Db::name('articleContent')->where('nid', $id)->value('content');
  63. return $list;
  64. } else return [];
  65. }
  66. /**
  67. * TODO 获取某个分类底下的文章
  68. * @param $cid
  69. * @param $page
  70. * @param $limit
  71. * @param string $field
  72. * @return mixed
  73. */
  74. public static function cidByArticleList($cid, $page, $limit, $field = 'id,title,image_input,visit,add_time,synopsis,url')
  75. {
  76. $model = new self();
  77. // if ($cid) $model->where("`cid` LIKE '$cid,%' OR `cid` LIKE '%,$cid,%' OR `cid` LIKE '%,$cid' OR `cid`=$cid ");
  78. if ((int)$cid) $model = $model->where("CONCAT(',',cid,',') LIKE '%,$cid,%'");
  79. $model = $model->field($field);
  80. $model = $model->where('status', 1);
  81. $model = $model->where('hide', 0);
  82. $model = $model->order('sort DESC,add_time DESC');
  83. if ($page) $model = $model->page($page, $limit);
  84. return $model->select();
  85. }
  86. /**
  87. * TODO 获取热门文章
  88. * @param string $field
  89. * @return mixed]
  90. */
  91. public static function getArticleListHot($field = 'id,title,image_input,visit,add_time,synopsis,url')
  92. {
  93. $model = new self();
  94. $model = $model->field($field);
  95. $model = $model->where('status', 1);
  96. $model = $model->where('hide', 0);
  97. $model = $model->where('is_hot', 1);
  98. $model = $model->order('sort DESC,add_time DESC');
  99. return $model->select();
  100. }
  101. /**
  102. * TODO 获取轮播文章
  103. * @param string $field
  104. * @return mixed
  105. */
  106. public static function getArticleListBanner($field = 'id,title,image_input,visit,add_time,synopsis,url')
  107. {
  108. $model = new self();
  109. $model = $model->field($field);
  110. $model = $model->where('status', 1);
  111. $model = $model->where('hide', 0);
  112. $model = $model->where('is_banner', 1);
  113. $model = $model->order('sort DESC,add_time DESC');
  114. $model = $model->limit(sys_config('news_slides_limit') ?? 3);
  115. return $model->select();
  116. }
  117. }