Article.php 4.9 KB

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