WechatNews.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. {
  23. use ModelTrait;
  24. /**
  25. * 获取配置分类
  26. * @param array $where
  27. * @return array
  28. */
  29. public static function getAll($where = array())
  30. {
  31. $model = new self;
  32. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  33. if ($where['cid'] !== '') $model = $model->where("CONCAT(',',cid,',') LIKE '%,$where[cid],%'");
  34. if ($where['cid'] == '') {
  35. if (!$where['merchant']) $model = $model->where('mer_id', 0);
  36. if ($where['merchant']) $model = $model->where('mer_id', 'GT', 0);
  37. }
  38. $model = $model->where('status', 1)->where('hide', 0);
  39. return self::page($model, function ($item) {
  40. $item['admin_name'] = '总后台管理员---》' . SystemAdmin::where('id', $item['admin_id'])->value('real_name');
  41. $item['content'] = Db::name('wechatNewsContent')->where('nid', $item['id'])->value('content');
  42. }, $where);
  43. }
  44. /**
  45. * 删除图文
  46. * @param $id
  47. * @return bool
  48. */
  49. public static function del($id)
  50. {
  51. return self::edit(['status' => 0], $id, 'id');
  52. }
  53. /**
  54. * 获取指定字段的值
  55. * @return array
  56. */
  57. public static function getNews()
  58. {
  59. return self::where('status', 1)->where('hide', 0)->order('id desc')->column('id,title');
  60. }
  61. /**
  62. * 给表中的字符串类型追加值
  63. * 删除所有有当前分类的id之后重新添加
  64. * @param $cid
  65. * @param $id
  66. * @return bool
  67. */
  68. public static function saveBatchCid($cid, $id)
  69. {
  70. $res_all = self::where('cid', 'LIKE', "%$cid%")->select();//获取所有有当前分类的图文
  71. foreach ($res_all as $k => $v) {
  72. $cid_arr = explode(',', $v['cid']);
  73. if (in_array($cid, $cid_arr)) {
  74. $key = array_search($cid, $cid_arr);
  75. array_splice($cid_arr, $key, 1);
  76. }
  77. if (empty($cid_arr)) {
  78. $data['cid'] = 0;
  79. self::edit($data, $v['id']);
  80. } else {
  81. $data['cid'] = implode(',', $cid_arr);
  82. self::edit($data, $v['id']);
  83. }
  84. }
  85. $res = self::where('id', 'IN', $id)->select();
  86. foreach ($res as $k => $v) {
  87. if (!in_array($cid, explode(',', $v['cid']))) {
  88. if (!$v['cid']) {
  89. $data['cid'] = $cid;
  90. } else {
  91. $data['cid'] = $v['cid'] . ',' . $cid;
  92. }
  93. self::edit($data, $v['id']);
  94. }
  95. }
  96. return true;
  97. }
  98. public static function setContent($id, $content)
  99. {
  100. $count = Db::name('wechatNewsContent')->where('nid', $id)->count();
  101. $data['nid'] = $id;
  102. $data['content'] = $content;
  103. if ($count) {
  104. $res = Db::name('wechatNewsContent')->where('nid', $id)->setField('content', $content);
  105. if ($res !== false) $res = true;
  106. } else
  107. $res = Db::name('wechatNewsContent')->insert($data);
  108. return $res;
  109. }
  110. public static function merchantPage($where = array())
  111. {
  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('wechatNewsContent')->where('nid', $item['id'])->value('content');
  122. }, $where);
  123. }
  124. }