Article.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  36. if ($where['cid'] !== '')
  37. $model = $model->where('cid', 'in', $where['cid']);
  38. if ($where['merchant'] !=0){
  39. $model = $model->where('mer_id', $where['merchant']);
  40. }
  41. $model = $model->where('status', 1)->where('hide', 0)->order('add_time desc');
  42. return self::page($model, function ($item) {
  43. if (!$item['mer_id']) $item['admin_name'] = '总后台管理员---》' . SystemAdmin::where('id', $item['admin_id'])->value('real_name');
  44. else $item['admin_name'] = '卫视门店';
  45. $item['content'] = ArticleContent::where('nid', $item['id'])->value('content');
  46. $item['catename'] = ArticleCategory::where('id', $item['cid'])->value('title');
  47. $item['image_input'] = explode(',', $item['image_input']);
  48. }, $where);
  49. }
  50. /**
  51. * 删除图文
  52. * @param $id
  53. * @return bool
  54. */
  55. public static function del($id)
  56. {
  57. return self::edit(['status' => 0], $id, 'id');
  58. }
  59. /**
  60. * 获取指定字段的值
  61. * @return array
  62. */
  63. public static function getNews()
  64. {
  65. return self::where('status', 1)->where('hide', 0)->order('id desc')->column('title', 'id');
  66. }
  67. /**
  68. * 给表中的字符串类型追加值
  69. * 删除所有有当前分类的id之后重新添加
  70. * @param $cid
  71. * @param $id
  72. * @return bool
  73. */
  74. public static function saveBatchCid($cid, $id)
  75. {
  76. $res_all = self::where('cid', 'LIKE', "%$cid%")->select();//获取所有有当前分类的图文
  77. foreach ($res_all as $k => $v) {
  78. $cid_arr = explode(',', $v['cid']);
  79. if (in_array($cid, $cid_arr)) {
  80. $key = array_search($cid, $cid_arr);
  81. array_splice($cid_arr, $key, 1);
  82. }
  83. if (empty($cid_arr)) {
  84. $data['cid'] = 0;
  85. self::edit($data, $v['id']);
  86. } else {
  87. $data['cid'] = implode(',', $cid_arr);
  88. self::edit($data, $v['id']);
  89. }
  90. }
  91. $res = self::where('id', 'IN', $id)->select();
  92. foreach ($res as $k => $v) {
  93. if (!in_array($cid, explode(',', $v['cid']))) {
  94. if (!$v['cid']) {
  95. $data['cid'] = $cid;
  96. } else {
  97. $data['cid'] = $v['cid'] . ',' . $cid;
  98. }
  99. self::edit($data, $v['id']);
  100. }
  101. }
  102. return true;
  103. }
  104. public static function setContent($id, $content)
  105. {
  106. $count = ArticleContent::where('nid', $id)->count();
  107. $data['nid'] = $id;
  108. $data['content'] = $content;
  109. if ($count) {
  110. $contentSql = ArticleContent::where('nid', $id)->value('content');
  111. if ($contentSql == $content) $res = true;
  112. else $res = ArticleContent::where('nid', $id)->update(['content' => $content]);
  113. if ($res !== false) $res = true;
  114. } else {
  115. $res = ArticleContent::insert($data);
  116. }
  117. return $res;
  118. }
  119. public static function merchantPage($where = [])
  120. {
  121. $model = new self;
  122. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  123. if ($where['cid'] !== '') $model = $model->where('cid', 'LIKE', "%$where[cid]%");
  124. $model = $model
  125. ->where('status', 1)
  126. ->where('hide', 0)
  127. ->where('admin_id', $where['admin_id'])
  128. ->where('mer_id', $where['mer_id']);
  129. return self::page($model, function ($item) {
  130. $item['content'] = ArticleContent::where('nid', $item['id'])->value('content');
  131. }, $where);
  132. }
  133. /**
  134. * 获取指定文章列表 图文管理使用
  135. * @param string $id
  136. * @param string $field
  137. * @return false|\PDOStatement|string|\think\Collection
  138. */
  139. public static function getArticleList($id = '', $field = 'title,author,image_input,synopsis,id')
  140. {
  141. $list = self::where('id', 'IN', $id)->field($field)->select();
  142. foreach ($list as $k => $v) {
  143. $list[$k]['content'] = ArticleContent::where('nid', $v['id'])->value('content');
  144. }
  145. return $list;
  146. }
  147. }