WechatNews.php 4.3 KB

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