Article.php 5.1 KB

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