WechatNews.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\wechat;
  12. use app\admin\model\system\SystemAdmin;
  13. use traits\ModelTrait;
  14. use basic\ModelBasic;
  15. use think\Db;
  16. /**
  17. * 图文管理 Model
  18. * Class WechatNews
  19. * @package app\admin\model\wechat
  20. */
  21. class WechatNews extends ModelBasic {
  22. use ModelTrait;
  23. /**
  24. * 获取配置分类
  25. * @param array $where
  26. * @return array
  27. */
  28. public static function getAll($where = array()){
  29. $model = new self;
  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. $item['admin_name'] = '总后台管理员---》'.SystemAdmin::where('id',$item['admin_id'])->value('real_name');
  39. $item['content'] = Db::name('wechatNewsContent')->where('nid',$item['id'])->value('content');
  40. },$where);
  41. }
  42. /**
  43. * 删除图文
  44. * @param $id
  45. * @return bool
  46. */
  47. public static function del($id){
  48. return self::edit(['status'=>0],$id,'id');
  49. }
  50. /**
  51. * 获取指定字段的值
  52. * @return array
  53. */
  54. public static function getNews()
  55. {
  56. return self::where('status',1)->where('hide',0)->order('id desc')->column('id,title');
  57. }
  58. /**
  59. * 给表中的字符串类型追加值
  60. * 删除所有有当前分类的id之后重新添加
  61. * @param $cid
  62. * @param $id
  63. * @return bool
  64. */
  65. public static function saveBatchCid($cid,$id){
  66. $res_all = self::where('cid','LIKE',"%$cid%")->select();//获取所有有当前分类的图文
  67. foreach ($res_all as $k=>$v){
  68. $cid_arr = explode(',',$v['cid']);
  69. if(in_array($cid,$cid_arr)){
  70. $key = array_search($cid, $cid_arr);
  71. array_splice($cid_arr, $key, 1);
  72. }
  73. if(empty($cid_arr)) {
  74. $data['cid'] = 0;
  75. self::edit($data,$v['id']);
  76. }else{
  77. $data['cid'] = implode(',',$cid_arr);
  78. self::edit($data,$v['id']);
  79. }
  80. }
  81. $res = self::where('id','IN',$id)->select();
  82. foreach ($res as $k=>$v){
  83. if(!in_array($cid,explode(',',$v['cid']))){
  84. if(!$v['cid']){
  85. $data['cid'] = $cid;
  86. }else{
  87. $data['cid'] = $v['cid'].','.$cid;
  88. }
  89. self::edit($data,$v['id']);
  90. }
  91. }
  92. return true;
  93. }
  94. public static function setContent($id,$content){
  95. $count = Db::name('wechatNewsContent')->where('nid',$id)->count();
  96. $data['nid'] = $id;
  97. $data['content'] = $content;
  98. if($count){
  99. $res = Db::name('wechatNewsContent')->where('nid',$id)->setField('content',$content);
  100. if($res !== false) $res = true;
  101. }
  102. else
  103. $res = Db::name('wechatNewsContent')->insert($data);
  104. return $res;
  105. }
  106. public static function merchantPage($where = array()){
  107. $model = new self;
  108. if($where['title'] !== '') $model = $model->where('title','LIKE',"%$where[title]%");
  109. if($where['cid'] !== '') $model = $model->where('cid','LIKE',"%$where[cid]%");
  110. $model = $model
  111. ->where('status',1)
  112. ->where('hide',0)
  113. ->where('admin_id',$where['admin_id'])
  114. ->where('mer_id',$where['mer_id']);
  115. return self::page($model,function($item){
  116. $item['content'] = Db::name('wechatNewsContent')->where('nid',$item['id'])->value('content');
  117. },$where);
  118. }
  119. }