Article.php 5.7 KB

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