ArticleService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\ArticleModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 文章管理-服务类
  16. * Class ArticleService
  17. * @package App\Services\Common
  18. */
  19. class ArticleService extends BaseService
  20. {
  21. protected static $instance = null;
  22. /**
  23. * 构造函数
  24. * ArticleService constructor.
  25. */
  26. public function __construct()
  27. {
  28. $this->model = new ArticleModel();
  29. }
  30. /**
  31. * 静态入口
  32. * @return static|null
  33. */
  34. public static function make()
  35. {
  36. if (!self::$instance) {
  37. self::$instance = (new static());
  38. }
  39. return self::$instance;
  40. }
  41. /**
  42. * 获取挂单广告列表
  43. * @param $params 参数
  44. * @param int $pageSize 分页大小:默认 15
  45. * @return array
  46. */
  47. public function getDataList($params, $pageSize = 15, $field=[])
  48. {
  49. $where = ['a.mark' => 1];
  50. $type = isset($params['type'])? $params['type'] : 0;
  51. $status = isset($params['status'])? $params['status'] : 0;
  52. $attrType = isset($params['attr_type'])? $params['attr_type'] : 0;
  53. if($type>0){
  54. $where['a.type'] = $type;
  55. }
  56. if($status>0){
  57. $where['a.status'] = $status;
  58. }
  59. if($attrType>0){
  60. $where['a.attr_type'] = $attrType;
  61. }
  62. $list = $this->model->from('article as a')
  63. ->where($where)
  64. ->where(function($query) use($params){
  65. $title = isset($params['title'])? trim($params['title']) : '';
  66. if($title){
  67. $query->where('a.title','like',"%{$title}%");
  68. }
  69. $timeType = isset($params['time_type'])? $params['time_type'] : 0;
  70. if($timeType == 1){
  71. $query->where('a.post_time','<=', time());
  72. }
  73. })
  74. ->select($field? $field : ['a.*'])
  75. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  76. $list = $list? $list->toArray() :[];
  77. if($list){
  78. $siteName = \App\Services\ConfigService::make()->getConfigByCode('site_name');
  79. foreach($list['data'] as &$item){
  80. $item['create_time_text'] = $item['create_time']? datetime($item['create_time']):'';
  81. $item['post_time_text'] = $item['post_time']? datetime($item['post_time'], 'm-d H:i'):'';
  82. $item['post_time'] = $item['post_time']? datetime($item['post_time']):'';
  83. $item['cover'] = $item['cover']? get_image_url($item['cover']):'';
  84. $item['author'] = $item['author']? $item['author'] : $siteName;
  85. }
  86. }
  87. return [
  88. 'pageSize'=> $pageSize,
  89. 'total'=>isset($list['total'])? $list['total'] : 0,
  90. 'list'=> isset($list['data'])? $list['data'] : []
  91. ];
  92. }
  93. /**
  94. * 获取资料详情
  95. * @param $where
  96. * @param array $field
  97. */
  98. public function getInfo($where, array $field = [])
  99. {
  100. $field = $field ? $field : ['*'];
  101. if (is_array($where)) {
  102. $info = $this->model->where($where)->select($field)->first();
  103. } else {
  104. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  105. }
  106. $info = $info ? $info->toArray() : [];
  107. if($info){
  108. $info['cover'] = $info['cover']? get_image_url($info['cover']):'';
  109. $info['post_time_text'] = $info['post_time']? datetime($info['post_time']):'';
  110. }
  111. return $info;
  112. }
  113. /**
  114. * 更新浏览
  115. * @param $id
  116. * @return mixed
  117. */
  118. public function updateViews($id){
  119. return $this->model->where(['id'=> $id])->increment('view_num', 1);
  120. }
  121. /**
  122. * 添加或编辑
  123. * @return array
  124. * @since 2020/11/11
  125. * @author laravel开发员
  126. */
  127. public function edit()
  128. {
  129. $data = request()->all();
  130. // 图片处理
  131. if(isset($data['cover'])){
  132. $cover = isset($data['cover'])? trim($data['cover']) : '';
  133. if (strpos($cover, "temp")) {
  134. $data['cover'] = save_image($cover, 'ad');
  135. } else {
  136. $data['cover'] = str_replace(IMG_URL, "", $data['cover']);
  137. }
  138. }
  139. // 开始时间
  140. if (isset($data['post_time'])) {
  141. $data['post_time'] = $data['post_time']? strtotime($data['post_time']) : time();
  142. }else{
  143. $data['post_time'] = time();
  144. }
  145. $type = isset($data['type'])? $data['type'] : 0;
  146. $attrType = isset($data['attr_type'])? $data['attr_type'] : 0;
  147. $id = isset($data['id'])? $data['id'] : 0;
  148. if($attrType==2){
  149. if($id && $this->model->where(['type'=> $type,'mark'=> 1])->whereNotIn('id',[$id])->value('id')){
  150. return returnJson('已经存在该分类单页文章', false);
  151. }else if($id<=0 && $this->model->where(['type'=> $type,'attr_type'=>2,'mark'=> 1])->value('id')){
  152. return returnJson('已经存在该分类单页文章', false);
  153. }
  154. }
  155. return parent::edit($data); // TODO: Change the autogenerated stub
  156. }
  157. }