Article.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\admin\model\article;
  3. use app\admin\model\store\StoreProduct;
  4. use app\admin\model\system\SystemAdmin;
  5. use app\models\article\ArticleContent;
  6. use crmeb\traits\ModelTrait;
  7. use crmeb\basic\BaseModel;
  8. /**
  9. * 图文管理 Model
  10. * Class WechatNews
  11. * @package app\admin\model\wechat
  12. */
  13. class Article extends BaseModel
  14. {
  15. use ModelTrait;
  16. protected $pk = 'id';
  17. protected $name = 'article';
  18. public function profile()
  19. {
  20. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->field('store_name');
  21. }
  22. /**
  23. * 获取配置分类
  24. * @param array $where
  25. * @return array
  26. */
  27. public static function getAll($where = [])
  28. {
  29. $model = new self;
  30. // if($where['status'] !== '') $model = $model->where('status',$where['status']);
  31. // if($where['access'] !== '') $model = $model->where('access',$where['access']);
  32. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  33. if ($where['cid'] !== '')
  34. $model = $model->where('cid', 'in', $where['cid']);
  35. else
  36. if ($where['merchant'])
  37. $model = $model->where('mer_id', '>', 0);
  38. else
  39. $model = $model->where('mer_id', 0);
  40. $model = $model->where('status', 1)->where('hide', 0)->order('id desc');
  41. return self::page($model, function ($item) {
  42. if (!$item['mer_id']) $item['admin_name'] = '总后台管理员---》' . SystemAdmin::where('id', $item['admin_id'])->value('real_name');
  43. else $item['admin_name'] = Merchant::where('id', $item['mer_id'])->value('mer_name') . '---》' . MerchantAdmin::where('id', $item['admin_id'])->value('real_name');
  44. $item['content'] = ArticleContent::where('nid', $item['id'])->value('content');
  45. $item['catename'] = ArticleCategory::where('id', $item['cid'])->value('title');
  46. $item['store_name'] = $item->profile->store_name ?? '';
  47. }, $where);
  48. }
  49. /**
  50. * 删除图文
  51. * @param $id
  52. * @return bool
  53. */
  54. public static function del($id)
  55. {
  56. return self::edit(['status' => 0], $id, 'id');
  57. }
  58. /**
  59. * 获取指定字段的值
  60. * @return array
  61. */
  62. public static function getNews()
  63. {
  64. return self::where('status', 1)->where('hide', 0)->order('id desc')->column('title', 'id');
  65. }
  66. /**
  67. * 给表中的字符串类型追加值
  68. * 删除所有有当前分类的id之后重新添加
  69. * @param $cid
  70. * @param $id
  71. * @return bool
  72. */
  73. public static function saveBatchCid($cid, $id)
  74. {
  75. $res_all = self::where('cid', 'LIKE', "%$cid%")->select(); //获取所有有当前分类的图文
  76. foreach ($res_all as $k => $v) {
  77. $cid_arr = explode(',', $v['cid']);
  78. if (in_array($cid, $cid_arr)) {
  79. $key = array_search($cid, $cid_arr);
  80. array_splice($cid_arr, $key, 1);
  81. }
  82. if (empty($cid_arr)) {
  83. $data['cid'] = 0;
  84. self::edit($data, $v['id']);
  85. } else {
  86. $data['cid'] = implode(',', $cid_arr);
  87. self::edit($data, $v['id']);
  88. }
  89. }
  90. $res = self::where('id', 'IN', $id)->select();
  91. foreach ($res as $k => $v) {
  92. if (!in_array($cid, explode(',', $v['cid']))) {
  93. if (!$v['cid']) {
  94. $data['cid'] = $cid;
  95. } else {
  96. $data['cid'] = $v['cid'] . ',' . $cid;
  97. }
  98. self::edit($data, $v['id']);
  99. }
  100. }
  101. return true;
  102. }
  103. public static function setContent($id, $content)
  104. {
  105. $count = ArticleContent::where('nid', $id)->count();
  106. $data['nid'] = $id;
  107. $data['content'] = $content;
  108. if ($count) {
  109. $contentSql = ArticleContent::where('nid', $id)->value('content');
  110. if ($contentSql == $content) $res = true;
  111. else $res = ArticleContent::where('nid', $id)->update(['content' => $content]);
  112. if ($res !== false) $res = true;
  113. } else {
  114. $res = ArticleContent::insert($data);
  115. }
  116. return $res;
  117. }
  118. public static function merchantPage($where = [])
  119. {
  120. $model = new self;
  121. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  122. if ($where['cid'] !== '') $model = $model->where('cid', 'LIKE', "%$where[cid]%");
  123. $model = $model
  124. ->where('status', 1)
  125. ->where('hide', 0)
  126. ->where('admin_id', $where['admin_id'])
  127. ->where('mer_id', $where['mer_id']);
  128. return self::page($model, function ($item) {
  129. $item['content'] = ArticleContent::where('nid', $item['id'])->value('content');
  130. }, $where);
  131. }
  132. /**
  133. * 获取指定文章列表 图文管理使用
  134. * @param string $id
  135. * @param string $field
  136. * @return false|\PDOStatement|string|\think\Collection
  137. */
  138. public static function getArticleList($id = '', $field = 'title,author,image_input,synopsis,id')
  139. {
  140. $list = self::where('id', 'IN', $id)->field($field)->select();
  141. foreach ($list as $k => $v) {
  142. $list[$k]['content'] = ArticleContent::where('nid', $v['id'])->value('content');
  143. }
  144. return $list;
  145. }
  146. }