Article.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\models\article;
  3. use app\models\user\EnterpriseUser as iseUser;
  4. use app\models\store\StoreProduct;
  5. use app\models\article\ArticleReply;
  6. use crmeb\services\SystemConfigService;
  7. use think\facade\Db;
  8. use crmeb\traits\ModelTrait;
  9. use crmeb\basic\BaseModel;
  10. /**
  11. * TODO 文章Model
  12. * Class Article
  13. * @package app\models\article
  14. */
  15. class Article extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'article';
  27. use ModelTrait;
  28. public function profile()
  29. {
  30. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->field('store_name,image,price,id,ot_price');
  31. }
  32. protected function getImageInputAttr($value)
  33. {
  34. return explode(',', $value) ?: [];
  35. }
  36. /**
  37. * TODO 获取一条新闻
  38. * @param int $id
  39. * @return array|null|\think\Model
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. * @throws \think\exception\DbException
  43. */
  44. public static function getArticleOne($id = 0)
  45. {
  46. if (!$id) return [];
  47. $list = self::where('status', 1)->where('hide', 0)->where('id', $id)->order('id desc')->find();
  48. if ($list) {
  49. $list = $list->hidden(['hide', 'status', 'admin_id'])->toArray();
  50. $list["content"] = Db::name('articleContent')->where('nid', $id)->value('content');
  51. $list["content"] = htmlspecialchars_decode($list["content"]);
  52. return $list;
  53. } else return [];
  54. }
  55. /**
  56. * TODO 获取某个分类底下的文章
  57. * @param $cid
  58. * @param $page
  59. * @param $limit
  60. * @param string $field
  61. * @return mixed
  62. */
  63. public static function cidByArticleList($cid, $page, $limit, $field = 'id,title,image_input,visit,add_time,synopsis,url')
  64. {
  65. $model = new self();
  66. // if ($cid) $model->where("`cid` LIKE '$cid,%' OR `cid` LIKE '%,$cid,%' OR `cid` LIKE '%,$cid' OR `cid`=$cid ");
  67. if ((int)$cid) $model = $model->where("CONCAT(',',cid,',') LIKE '%,$cid,%'");
  68. $model = $model->field($field);
  69. $model = $model->where('status', 1);
  70. $model = $model->where('hide', 0);
  71. $model = $model->order('sort DESC,add_time DESC');
  72. if ($page) $model = $model->page($page, $limit);
  73. return $model->select();
  74. }
  75. /**
  76. * TODO 获取首页推文章
  77. * @param string $field
  78. * @return mixed]
  79. */
  80. public static function getArticleListHot($field = 'id,title,image_input,visit,add_time,synopsis,url,mer_id,is_hot,is_top',$type)
  81. {
  82. $model = new self();
  83. $model = $model->field($field);
  84. $model = $model->where('status', 1);
  85. $model = $model->where('is_check', 1);
  86. $model = $model->where('hide', 0);
  87. if(isset($type['type'])&&$type['type']>0){
  88. $model = $model->where('cid',$type['type']);
  89. }elseif (isset($type['type'])&&$type['type']==0) {
  90. $model = $model->where('is_top',1);
  91. }else{
  92. $model = $model->where('is_hot', 1);
  93. }
  94. $model =$model->page($type['page'],$type['rows']);
  95. $model = $model->order('sort DESC,add_time DESC');
  96. $data=[];
  97. foreach($model->select() as $k=>$v){
  98. $mser=iseUser::where('id',$v['mer_id'])->find();
  99. $plays = new ArticleReply();
  100. $play=$plays->counts($v['id']);
  101. $data[]=[
  102. 'id'=>$v['id'],
  103. 'title'=>$v['title'],
  104. 'is_hot'=>$v['is_hot'],
  105. 'image_input'=>$v['image_input'],
  106. 'comment'=>$play,
  107. 'add_time'=>$v['add_time'],
  108. 'mername'=>$mser['name'],
  109. 'is_top'=>$v['is_top'],
  110. ];
  111. }
  112. $count=count($data);
  113. $list=['count'=>$count,'list'=>$data];
  114. return $list;
  115. }
  116. /**
  117. * TODO 获取轮播文章
  118. * @param string $field
  119. * @return mixed
  120. */
  121. public static function getArticleListBanner($field = 'id,title,image_input,visit,add_time,synopsis,url')
  122. {
  123. $model = new self();
  124. $model = $model->field($field);
  125. $model = $model->where('status', 1);
  126. $model = $model->where('hide', 0);
  127. $model = $model->where('is_banner', 1);
  128. $model = $model->order('sort DESC,add_time DESC');
  129. $model = $model->limit(sys_config('news_slides_limit') ?? 3);
  130. return $model->select();
  131. }
  132. public function search($data){
  133. $model = new self();
  134. $res=$model->alias('a')
  135. ->join(['eb_enterprise_user'=>'e'],'a.mer_id=e.id')
  136. ->where('status',1)
  137. ->where('is_check',1)
  138. ->where('synopsis|title','like','%'.$data['search'].'%')
  139. ->page($data['page'],$data['rows'])
  140. ->field('a.id,title,image_input,name,add_time')
  141. ->order('add_time', 'desc')
  142. ->select();
  143. $count=count($res);
  144. // 查询评论数
  145. $list=[];
  146. foreach($res as $v){
  147. $plays = new ArticleReply();
  148. $play=$plays->counts($v['id']);
  149. $list[]=[
  150. 'id'=>$v['id'],
  151. 'title'=>$v['title'],
  152. 'image_input'=>$v['image_input'],
  153. 'comment'=>$play,
  154. 'add_time'=>$v['add_time'],
  155. 'mername'=>$v['name']
  156. ];
  157. }
  158. return $lists=['count'=> $count, 'list'=>$list];
  159. }
  160. }