Article.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace app\models\article;
  3. use app\models\store\StoreProduct;
  4. use app\models\system\SystemAdmin;
  5. use crmeb\services\SystemConfigService;
  6. use think\facade\Db;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. /**
  10. * TODO 文章Model
  11. * Class Article
  12. * @package app\models\article
  13. */
  14. class Article extends BaseModel
  15. {
  16. /**
  17. * 数据表主键
  18. * @var string
  19. */
  20. protected $pk = 'id';
  21. /**
  22. * 模型名称
  23. * @var string
  24. */
  25. protected $name = 'article';
  26. use ModelTrait;
  27. public function profile()
  28. {
  29. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->field('store_name,image,price,id,ot_price');
  30. }
  31. protected function getImageInputAttr($value)
  32. {
  33. return explode(',', $value) ?: [];
  34. }
  35. /**
  36. * TODO 获取一条新闻
  37. * @param int $id
  38. * @return array|null|\think\Model
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @throws \think\exception\DbException
  42. */
  43. public static function getArticleOne($id = 0)
  44. {
  45. if (!$id) return [];
  46. $list = self::where('status', 1)->where('hide', 0)->where('id', $id)->order('id desc')->find();
  47. if ($list) {
  48. $list->store_info = $list->profile ? $list->profile->toArray() : null;
  49. $list = $list->hidden(['hide', 'status', 'admin_id', 'mer_id'])->toArray();
  50. $list["content"] = Db::name('articleContent')->where('nid', $id)->value('content');
  51. $list["content"] = htmlspecialchars_decode($list["content"]);
  52. return $list;
  53. } else return [];
  54. }
  55. /**
  56. * TODO 获取某个分类底下的文章
  57. * @param $cid
  58. * @param $page
  59. * @param $limit
  60. * @param string $field
  61. * @return mixed
  62. */
  63. public static function cidByArticleList($cid, $page, $limit, $field = 'id,title,image_input,visit,add_time,synopsis,url', $mer_id = '')
  64. {
  65. $model = new self();
  66. // if ($cid) $model->where("`cid` LIKE '$cid,%' OR `cid` LIKE '%,$cid,%' OR `cid` LIKE '%,$cid' OR `cid`=$cid ");
  67. if ((int)$cid) $model = $model->where("CONCAT(',',cid,',') LIKE '%,$cid,%'");
  68. if ($mer_id) $model = $model->where('mer_id', $mer_id);
  69. $model = $model->field($field);
  70. $model = $model->where('status', 1);
  71. $model = $model->where('hide', 0);
  72. $model = $model->order('sort DESC,add_time DESC');
  73. if ($page) $model = $model->page($page, $limit);
  74. return $model->select();
  75. }
  76. /**
  77. * TODO 获取热门文章
  78. * @param string $field
  79. * @return mixed]
  80. */
  81. public static function getArticleListHot($field = 'id,title,image_input,visit,add_time,synopsis,url', $mer_id = '')
  82. {
  83. $model = new self();
  84. if ($mer_id) $model = $model->where('mer_id', $mer_id);
  85. $model = $model->field($field);
  86. $model = $model->where('status', 1);
  87. $model = $model->where('hide', 0);
  88. $model = $model->where('is_hot', 1);
  89. $model = $model->order('sort DESC,add_time DESC');
  90. return $model->select();
  91. }
  92. /**
  93. * TODO 获取轮播文章
  94. * @param string $field
  95. * @return mixed
  96. */
  97. public static function getArticleListBanner($field = 'id,title,image_input,visit,add_time,synopsis,url', $mer_id = '')
  98. {
  99. $model = new self();
  100. if ($mer_id) $model = $model->where('mer_id', $mer_id);
  101. $model = $model->field($field);
  102. $model = $model->where('status', 1);
  103. $model = $model->where('hide', 0);
  104. $model = $model->where('is_banner', 1);
  105. $model = $model->order('sort DESC,add_time DESC');
  106. $model = $model->limit(sys_config('news_slides_limit') ?? 3);
  107. return $model->select();
  108. }
  109. //后台模型
  110. /**
  111. * 获取配置分类
  112. * @param array $where
  113. * @return array
  114. */
  115. public static function getAll($where = array())
  116. {
  117. $model = new self;
  118. // if($where['status'] !== '') $model = $model->where('status',$where['status']);
  119. // if($where['access'] !== '') $model = $model->where('access',$where['access']);
  120. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  121. if ($where['mer_id'] != '') $model = $model->where('mer_id', $where['mer_id']);
  122. if ($where['cid'] !== '')
  123. $model = $model->where('cid', 'in', $where['cid']);
  124. $model = $model->where('status', 1)->where('hide', 0);
  125. $model = $model->order('sort desc,id desc');
  126. // return self::page($model,function($item){
  127. // if(!$item['mer_id']) $item['admin_name'] = '总后台管理员---》'.SystemAdmin::where('id',$item['admin_id'])->value('real_name');
  128. // else $item['admin_name'] = Merchant::where('id',$item['mer_id'])->value('mer_name').'---》'.MerchantAdmin::where('id',$item['admin_id'])->value('real_name');
  129. // $item['content'] = ArticleContent::where('nid',$item['id'])->value('content');
  130. // $item['catename'] = ArticleCategory::where('id',$item['cid'])->value('title');
  131. // $item['store_name'] = $item->profile->store_name ?? '';
  132. // },$where);
  133. $list = $model->page((int)$where['page'], (int)$where['limit'])
  134. ->select()
  135. ->each(function ($item) {
  136. $item['content'] = ArticleContent::where('nid', $item['id'])->value('content');
  137. $item['catename'] = ArticleCategory::where('id', $item['cid'])->value('title');
  138. $item['store_name'] = $item->profile->store_name ?? '';
  139. });
  140. $count = $model->count();
  141. return compact('count', 'list');
  142. }
  143. /**
  144. * 获取指定字段的值
  145. * @return array
  146. */
  147. public static function getNews()
  148. {
  149. return self::where('status', 1)->where('hide', 0)->order('id desc')->column('title', 'id');
  150. }
  151. /**
  152. * 新增或修改文章内容
  153. * @param $id
  154. * @param $content
  155. * @return bool|int|string
  156. * @throws \think\Exception
  157. * @throws \think\exception\PDOException
  158. */
  159. public static function setContent($id, $content)
  160. {
  161. $count = ArticleContent::where('nid', $id)->count();
  162. $data['nid'] = $id;
  163. $data['content'] = $content;
  164. if ($count) {
  165. $contentSql = ArticleContent::where('nid', $id)->value('content');
  166. if ($contentSql == $content) $res = true;
  167. else $res = ArticleContent::where('nid', $id)->update(['content' => $content]);
  168. if ($res !== false) $res = true;
  169. } else {
  170. $res = ArticleContent::insert($data);
  171. }
  172. return $res;
  173. }
  174. /**
  175. * 给表中的字符串类型追加值
  176. * 删除所有有当前分类的id之后重新添加
  177. * @param $cid
  178. * @param $id
  179. * @return bool
  180. */
  181. public static function saveBatchCid($cid, $id)
  182. {
  183. $res_all = self::where('cid', 'LIKE', "%$cid%")->select();//获取所有有当前分类的图文
  184. foreach ($res_all as $k => $v) {
  185. $cid_arr = explode(',', $v['cid']);
  186. if (in_array($cid, $cid_arr)) {
  187. $key = array_search($cid, $cid_arr);
  188. array_splice($cid_arr, $key, 1);
  189. }
  190. if (empty($cid_arr)) {
  191. $data['cid'] = 0;
  192. self::edit($data, $v['id']);
  193. } else {
  194. $data['cid'] = implode(',', $cid_arr);
  195. self::edit($data, $v['id']);
  196. }
  197. }
  198. $res = self::where('id', 'IN', $id)->select();
  199. foreach ($res as $k => $v) {
  200. if (!in_array($cid, explode(',', $v['cid']))) {
  201. if (!$v['cid']) {
  202. $data['cid'] = $cid;
  203. } else {
  204. $data['cid'] = $v['cid'] . ',' . $cid;
  205. }
  206. self::edit($data, $v['id']);
  207. }
  208. }
  209. return true;
  210. }
  211. }