Article.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace app\admin\model\article;
  8. use app\admin\model\store\StoreProduct;
  9. use app\admin\model\system\SystemAdmin;
  10. use crmeb\traits\ModelTrait;
  11. use crmeb\basic\BaseModel;
  12. use think\facade\Db;
  13. /**
  14. * 图文管理 Model
  15. * Class WechatNews
  16. * @package app\admin\model\wechat
  17. */
  18. class Article extends BaseModel {
  19. use ModelTrait;
  20. protected $pk = 'id';
  21. protected $name = 'article';
  22. public function profile()
  23. {
  24. return $this->hasOne(StoreProduct::class,'id','product_id')->field('store_name');
  25. }
  26. /**
  27. * 获取配置分类
  28. * @param array $where
  29. * @return array
  30. */
  31. public static function getAll($where = array()){
  32. $model = new self;
  33. // if($where['status'] !== '') $model = $model->where('status',$where['status']);
  34. // if($where['access'] !== '') $model = $model->where('access',$where['access']);
  35. if($where['title'] !== '') $model = $model->where('title','LIKE',"%$where[title]%");
  36. if($where['cid'] !== '')
  37. $model = $model->where('cid','in',$where['cid']);
  38. else
  39. if($where['merchant'])
  40. $model = $model->where('mer_id','>',0);
  41. else
  42. $model = $model->where('mer_id',0);
  43. $model = $model->where('status',1)->where('hide',0)->order('id desc');
  44. return self::page($model,function($item){
  45. if(!$item['mer_id']) $item['admin_name'] = '总后台管理员---》'.SystemAdmin::where('id',$item['admin_id'])->value('real_name');
  46. else $item['admin_name'] = Merchant::where('id',$item['mer_id'])->value('mer_name').'---》'.MerchantAdmin::where('id',$item['admin_id'])->value('real_name');
  47. $item['content'] = Db::name('ArticleContent')->where('nid',$item['id'])->value('content');
  48. $item['catename'] = Db::name('ArticleCategory')->where('id',$item['cid'])->value('title');
  49. $item['store_name'] = $item->profile->store_name ?? '';
  50. },$where);
  51. }
  52. /**
  53. * 删除图文
  54. * @param $id
  55. * @return bool
  56. */
  57. public static function del($id){
  58. return self::edit(['status'=>0],$id,'id');
  59. }
  60. /**
  61. * 获取指定字段的值
  62. * @return array
  63. */
  64. public static function getNews()
  65. {
  66. return self::where('status',1)->where('hide',0)->order('id desc')->column('title','id');
  67. }
  68. /**
  69. * 给表中的字符串类型追加值
  70. * 删除所有有当前分类的id之后重新添加
  71. * @param $cid
  72. * @param $id
  73. * @return bool
  74. */
  75. public static function saveBatchCid($cid,$id){
  76. $res_all = self::where('cid','LIKE',"%$cid%")->select();//获取所有有当前分类的图文
  77. foreach ($res_all as $k=>$v){
  78. $cid_arr = explode(',',$v['cid']);
  79. if(in_array($cid,$cid_arr)){
  80. $key = array_search($cid, $cid_arr);
  81. array_splice($cid_arr, $key, 1);
  82. }
  83. if(empty($cid_arr)) {
  84. $data['cid'] = 0;
  85. self::edit($data,$v['id']);
  86. }else{
  87. $data['cid'] = implode(',',$cid_arr);
  88. self::edit($data,$v['id']);
  89. }
  90. }
  91. $res = self::where('id','IN',$id)->select();
  92. foreach ($res as $k=>$v){
  93. if(!in_array($cid,explode(',',$v['cid']))){
  94. if(!$v['cid']){
  95. $data['cid'] = $cid;
  96. }else{
  97. $data['cid'] = $v['cid'].','.$cid;
  98. }
  99. self::edit($data,$v['id']);
  100. }
  101. }
  102. return true;
  103. }
  104. public static function setContent($id,$content){
  105. $count = Db::name('ArticleContent')->where('nid',$id)->count();
  106. $data['nid'] = $id;
  107. $data['content'] = $content;
  108. if($count){
  109. $contentSql = Db::name('ArticleContent')->where('nid',$id)->value('content');
  110. if($contentSql == $content) $res = true;
  111. else $res = Db::name('ArticleContent')->where('nid',$id)->update(['content'=>$content]);
  112. if($res !== false) $res = true;
  113. }else{
  114. $res = Db::name('ArticleContent')->insert($data);
  115. }
  116. return $res;
  117. }
  118. public static function merchantPage($where = array()){
  119. $model = new self;
  120. if($where['title'] !== '') $model = $model->where('title','LIKE',"%$where[title]%");
  121. if($where['cid'] !== '') $model = $model->where('cid','LIKE',"%$where[cid]%");
  122. $model = $model
  123. ->where('status',1)
  124. ->where('hide',0)
  125. ->where('admin_id',$where['admin_id'])
  126. ->where('mer_id',$where['mer_id']);
  127. return self::page($model,function($item){
  128. $item['content'] = Db::name('ArticleContent')->where('nid',$item['id'])->value('content');
  129. },$where);
  130. }
  131. /**
  132. * 获取指定文章列表 图文管理使用
  133. * @param string $id
  134. * @param string $field
  135. * @return false|\PDOStatement|string|\think\Collection
  136. */
  137. public static function getArticleList($id = '',$field = 'title,author,image_input,synopsis,id'){
  138. $list = self::where('id','IN',$id)->field($field)->select();
  139. foreach ($list as $k=>$v){
  140. $list[$k]['content'] = Db::name('ArticleContent')->where('nid',$v['id'])->value('content');
  141. }
  142. return $list;
  143. }
  144. }