Article.php 5.0 KB

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