WechatNews.php 4.4 KB

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