Article.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\article;
  12. use app\admin\model\system\RecommendRelation;
  13. use app\admin\model\system\SystemAdmin;
  14. use traits\ModelTrait;
  15. use basic\ModelBasic;
  16. use think\Db;
  17. /**
  18. * 图文管理 Model
  19. * Class WechatNews
  20. * @package app\admin\model\wechat
  21. */
  22. class Article extends ModelBasic {
  23. use ModelTrait;
  24. public function profile()
  25. {
  26. return $this->hasOne('ArticleContent','nid','id')->field('content');
  27. }
  28. public static function getAddTimeAttr($value){
  29. return $value ? date('Y-m-d H:i:s',$value) : '';
  30. }
  31. public static function getLabelAttr($value){
  32. return is_string($value) ? json_decode($value,true) : $value;
  33. }
  34. public static function PreWhere($alias=''){
  35. $alias=$alias ? $alias.'.' : '';
  36. return self::where(["{$alias}is_show"=>1]);
  37. }
  38. public static function setWhere($where,$alias='',$model=null){
  39. if($model===null) $model=new self();
  40. if($alias){
  41. $model->alias($alias);
  42. $alias.='.';
  43. }
  44. if($where['store_name']) $model->where("{$alias}title",'LIKE',"%$where[store_name]%");
  45. if($where['cid']) $model->where("{$alias}cid",$where['cid']);
  46. if($where['order'])
  47. $model->order($alias.self::setOrder($where['order']));
  48. else
  49. $model->order($alias.'sort desc');
  50. if($where['is_show']!=='') $model->where("{$alias}is_show",$where['is_show']);
  51. $model->where("{$alias}hide",0);
  52. return $model;
  53. }
  54. public static function getArticleLayList($where){
  55. $data=self::setWhere($where,'A')->join('__ARTICLE_CONTENT__ B','A.id=B.nid')->field('A.*,B.content')->page((int)$where['page'],(int)$where['limit'])->select();
  56. foreach ($data as &$item){
  57. $item['cate_name']=ArticleCategory::where('id',$item['cid'])->where('is_del',0)->value('title');
  58. }
  59. $count=self::setWhere($where)->count();
  60. return compact('data','count');
  61. }
  62. /**
  63. * 获取配置分类
  64. * @param array $where
  65. * @return array
  66. */
  67. public static function getAll($where = array()){
  68. $model = new self;
  69. if($where['title'] !== '') $model = $model->where('title','LIKE',"%$where[title]%");
  70. if($where['cid'] !== '') $model = $model->where("CONCAT(',',cid,',') LIKE '%,$where[cid],%'");
  71. if($where['cid'] == ''){
  72. if(!$where['merchant']) $model = $model->where('mer_id',0);
  73. if($where['merchant']) $model = $model->where('mer_id','GT',0);
  74. }
  75. $model = $model->where('status',1)->where('hide',0);
  76. return self::page($model,function($item){
  77. $item['admin_name'] = '后台管理员---》'.SystemAdmin::where('id',$item['admin_id'])->value('real_name');
  78. $item['content'] = Db::name('ArticleContent')->where('nid',$item['id'])->value('content');
  79. $item['catename'] = Db::name('ArticleCategory')->where('id',$item['cid'])->value('title');
  80. },$where);
  81. }
  82. /**
  83. * 删除图文
  84. * @param $id
  85. * @return bool
  86. */
  87. public static function del($id){
  88. return self::edit(['status'=>0],$id,'id');
  89. }
  90. /**
  91. * 获取指定字段的值
  92. * @return array
  93. */
  94. public static function getNews()
  95. {
  96. return self::where('status',1)->where('hide',0)->order('id desc')->column('id,title');
  97. }
  98. /**
  99. * 给表中的字符串类型追加值
  100. * 删除所有有当前分类的id之后重新添加
  101. * @param $cid
  102. * @param $id
  103. * @return bool
  104. */
  105. public static function saveBatchCid($cid,$id){
  106. $res_all = self::where('cid','LIKE',"%$cid%")->select();//获取所有有当前分类的图文
  107. foreach ($res_all as $k=>$v){
  108. $cid_arr = explode(',',$v['cid']);
  109. if(in_array($cid,$cid_arr)){
  110. $key = array_search($cid, $cid_arr);
  111. array_splice($cid_arr, $key, 1);
  112. }
  113. if(empty($cid_arr)) {
  114. $data['cid'] = 0;
  115. self::edit($data,$v['id']);
  116. }else{
  117. $data['cid'] = implode(',',$cid_arr);
  118. self::edit($data,$v['id']);
  119. }
  120. }
  121. $res = self::where('id','IN',$id)->select();
  122. foreach ($res as $k=>$v){
  123. if(!in_array($cid,explode(',',$v['cid']))){
  124. if(!$v['cid']){
  125. $data['cid'] = $cid;
  126. }else{
  127. $data['cid'] = $v['cid'].','.$cid;
  128. }
  129. self::edit($data,$v['id']);
  130. }
  131. }
  132. return true;
  133. }
  134. public static function setContent($id,$content){
  135. $count = Db::name('ArticleContent')->where('nid',$id)->count();
  136. $data['nid'] = $id;
  137. $data['content'] = $content;
  138. if($count){
  139. $res = Db::name('ArticleContent')->where('nid',$id)->setField('content',$content);
  140. if($res !== false) $res = true;
  141. }
  142. else
  143. $res = Db::name('ArticleContent')->insert($data);
  144. return $res;
  145. }
  146. public static function merchantPage($where = array()){
  147. $model = new self;
  148. if($where['title'] !== '') $model = $model->where('title','LIKE',"%$where[title]%");
  149. if($where['cid'] !== '') $model = $model->where('cid','LIKE',"%$where[cid]%");
  150. $model = $model
  151. ->where('status',1)
  152. ->where('hide',0)
  153. ->where('admin_id',$where['admin_id']);
  154. return self::page($model,function($item){
  155. $item['content'] = Db::name('ArticleContent')->where('nid',$item['id'])->value('content');
  156. },$where);
  157. }
  158. /**
  159. * 获取指定文章列表 图文管理使用
  160. * @param string $id
  161. * @param string $field
  162. * @return false|\PDOStatement|string|\think\Collection
  163. */
  164. public static function getArticleList($id = '',$field = 'title,author,image_input,synopsis,id'){
  165. $list = self::where('id','IN',$id)->field($field)->select();
  166. foreach ($list as $k=>$v){
  167. $list[$k]['content'] = Db::name('ArticleContent')->where('nid',$v['id'])->value('content');
  168. }
  169. return $list;
  170. }
  171. /**
  172. * 获取活动咨询列表
  173. * @param array $where
  174. * @return array
  175. */
  176. public static function getConsultList($where = array()){
  177. $model = new self;
  178. if(isset($where['title']) && $where['title'] !== '') $model = $model->where('title','LIKE',"%$where[title]%");
  179. if(isset($where['consult_type']) && $where['consult_type']) $model = $model->where('consult_type',$where['consult_type']);
  180. $model = $model->where('is_consult',1)->where('hide',0);
  181. return self::page($model,$where);
  182. }
  183. }