Article.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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\article;
  12. use app\admin\model\system\RecommendRelation;
  13. use app\admin\model\system\WebRecommendRelation;
  14. use app\admin\model\system\SystemAdmin;
  15. use traits\ModelTrait;
  16. use basic\ModelBasic;
  17. use think\Db;
  18. /**
  19. * 新闻管理 Model
  20. * Class Article
  21. * @package app\admin\model\article
  22. */
  23. class Article extends ModelBasic
  24. {
  25. use ModelTrait;
  26. public function profile()
  27. {
  28. return $this->hasOne('ArticleContent', 'nid', 'id')->field('content');
  29. }
  30. public static function getAddTimeAttr($value)
  31. {
  32. return $value ? date('Y-m-d H:i:s', $value) : '';
  33. }
  34. public static function getLabelAttr($value)
  35. {
  36. return is_string($value) ? json_decode($value, true) : $value;
  37. }
  38. public static function PreWhere($alias = '')
  39. {
  40. $alias = $alias ? $alias . '.' : '';
  41. return self::where(["{$alias}is_show" => 1]);
  42. }
  43. public static function setWhere($where, $alias = '', $model = null)
  44. {
  45. if ($model === null) $model = new self();
  46. if ($alias) {
  47. $model->alias($alias);
  48. $alias .= '.';
  49. }
  50. if (isset($where['store_name']) && $where['store_name']) $model->where("{$alias}title", 'LIKE', "%$where[store_name]%");
  51. if (isset($where['cid']) && $where['cid']) $model->where("{$alias}cid", $where['cid']);
  52. if (isset($where['order']) && $where['order'])
  53. $model->order($alias . self::setOrder($where['order']));
  54. else
  55. $model->order($alias . 'sort desc,id desc');
  56. if (isset($where['is_show']) && $where['is_show'] !== '') $model->where("{$alias}is_show", $where['is_show']);
  57. $model->where("{$alias}hide", 0);
  58. return $model;
  59. }
  60. public static function getArticleLayList($where)
  61. {
  62. $data = self::setWhere($where, 'A')->field('A.*')->page((int)$where['page'], (int)$where['limit'])->select();
  63. foreach ($data as &$item) {
  64. $item['cate_name'] = ArticleCategory::where('id', $item['cid'])->where('is_del', 0)->value('title');
  65. $item['recommend'] = RecommendRelation::where('a.link_id', $item['id'])->where('a.type', 1)->alias('a')
  66. ->join('__RECOMMEND__ r', 'a.recommend_id=r.id')->column('a.id,r.title');
  67. $item['web_recommend'] = WebRecommendRelation::where('a.link_id', $item['id'])->where('a.type', 4)->alias('a')
  68. ->join('__WEB_RECOMMEND__ r', 'a.recommend_id=r.id')->column('a.id,r.title');
  69. }
  70. $count = self::setWhere($where)->count();
  71. return compact('data', 'count');
  72. }
  73. /**
  74. * 获取配置分类
  75. * @param array $where
  76. * @return array
  77. */
  78. public static function getAll($where = array())
  79. {
  80. $model = new self;
  81. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  82. if ($where['cid'] !== '') $model = $model->where("CONCAT(',',cid,',') LIKE '%,$where[cid],%'");
  83. if ($where['cid'] == '') {
  84. if (!$where['merchant']) $model = $model->where('mer_id', 0);
  85. if ($where['merchant']) $model = $model->where('mer_id', 'GT', 0);
  86. }
  87. $model = $model->where('status', 1)->where('hide', 0);
  88. return self::page($model, function ($item) {
  89. $item['admin_name'] = '后台管理员---》' . SystemAdmin::where('id', $item['admin_id'])->value('real_name');
  90. $item['content'] = Db::name('ArticleContent')->where('nid', $item['id'])->value('content');
  91. $item['catename'] = Db::name('ArticleCategory')->where('id', $item['cid'])->value('title');
  92. }, $where);
  93. }
  94. /**
  95. * 删除图文
  96. * @param $id
  97. * @return bool
  98. */
  99. public static function del($id)
  100. {
  101. return self::edit(['status' => 0], $id, 'id');
  102. }
  103. /**
  104. * 获取指定字段的值
  105. * @return array
  106. */
  107. public static function getNews()
  108. {
  109. return self::where('status', 1)->where('hide', 0)->order('id desc')->column('id,title');
  110. }
  111. /**
  112. * 给表中的字符串类型追加值
  113. * 删除所有有当前分类的id之后重新添加
  114. * @param $cid
  115. * @param $id
  116. * @return bool
  117. */
  118. public static function saveBatchCid($cid, $id)
  119. {
  120. $res_all = self::where('cid', 'LIKE', "%$cid%")->select();//获取所有有当前分类的图文
  121. foreach ($res_all as $k => $v) {
  122. $cid_arr = explode(',', $v['cid']);
  123. if (in_array($cid, $cid_arr)) {
  124. $key = array_search($cid, $cid_arr);
  125. array_splice($cid_arr, $key, 1);
  126. }
  127. if (empty($cid_arr)) {
  128. $data['cid'] = 0;
  129. self::edit($data, $v['id']);
  130. } else {
  131. $data['cid'] = implode(',', $cid_arr);
  132. self::edit($data, $v['id']);
  133. }
  134. }
  135. $res = self::where('id', 'IN', $id)->select();
  136. foreach ($res as $k => $v) {
  137. if (!in_array($cid, explode(',', $v['cid']))) {
  138. if (!$v['cid']) {
  139. $data['cid'] = $cid;
  140. } else {
  141. $data['cid'] = $v['cid'] . ',' . $cid;
  142. }
  143. self::edit($data, $v['id']);
  144. }
  145. }
  146. return true;
  147. }
  148. public static function setContent($id, $content)
  149. {
  150. $count = Db::name('ArticleContent')->where('nid', $id)->count();
  151. $data['nid'] = $id;
  152. $data['content'] = $content;
  153. if ($count) {
  154. $res = Db::name('ArticleContent')->where('nid', $id)->setField('content', $content);
  155. if ($res !== false) $res = true;
  156. } else
  157. $res = Db::name('ArticleContent')->insert($data);
  158. return $res;
  159. }
  160. public static function merchantPage($where = array())
  161. {
  162. $model = new self;
  163. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  164. if ($where['cid'] !== '') $model = $model->where('cid', 'LIKE', "%$where[cid]%");
  165. $model = $model
  166. ->where('status', 1)
  167. ->where('hide', 0)
  168. ->where('admin_id', $where['admin_id']);
  169. return self::page($model, function ($item) {
  170. $item['content'] = Db::name('ArticleContent')->where('nid', $item['id'])->value('content');
  171. }, $where);
  172. }
  173. /**
  174. * 获取指定文章列表 图文管理使用
  175. * @param string $id
  176. * @param string $field
  177. * @return false|\PDOStatement|string|\think\Collection
  178. */
  179. public static function getArticleList($id = '', $field = 'title,author,image_input,synopsis,id')
  180. {
  181. $list = self::where('id', 'IN', $id)->field($field)->select();
  182. foreach ($list as $k => $v) {
  183. $list[$k]['content'] = Db::name('ArticleContent')->where('nid', $v['id'])->value('content');
  184. }
  185. return $list;
  186. }
  187. /**
  188. * 获取活动咨询列表
  189. * @param array $where
  190. * @return array
  191. */
  192. public static function getConsultList($where = array())
  193. {
  194. $model = new self;
  195. if (isset($where['title']) && $where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  196. if (isset($where['consult_type']) && $where['consult_type']) $model = $model->where('consult_type', $where['consult_type']);
  197. $model = $model->where('is_consult', 1)->where('hide', 0);
  198. return self::page($model, $where);
  199. }
  200. }