WechatNews.php 4.2 KB

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