CollectService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\CollectModel;
  13. /**
  14. * 收藏管理-服务类
  15. * @author wesmiler
  16. * @since 2020/11/11
  17. * Class CollectService
  18. * @package App\Services
  19. */
  20. class CollectService extends BaseService
  21. {
  22. protected static $instance = null;
  23. /**
  24. * 构造函数
  25. * @author wesmiler
  26. * @since 2020/11/11
  27. * CollectService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new CollectModel();
  32. }
  33. /**
  34. * 静态入口
  35. * @return ArticleService|null
  36. */
  37. public static function make(){
  38. if(!self::$instance){
  39. self::$instance = new ArticleService();
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * 获取列表
  45. * @return array
  46. * @since 2020/11/11
  47. * @author wesmiler
  48. */
  49. public function getDataList($params)
  50. {
  51. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  52. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  53. $dataList = $this->model::from('collect as a')
  54. ->leftJoin('article as c', 'a.source_id', '=', 'c.id')
  55. ->where(function ($query) use ($params) {
  56. $query->where(['a.mark'=> 1,'a.status'=> 1,'c.mark'=> 1,'c.status'=>1]);
  57. $type = isset($params['type']) ? intval($params['type']) : 0;
  58. if ($type > 0) {
  59. $query->where('a.type', $type);
  60. }
  61. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  62. if ($userId > 0) {
  63. $query->where('a.user_id', $userId);
  64. }
  65. })
  66. ->select(['a.id', 'a.user_id','a.source_id', 'c.title as title', 'c.thumb', 'c.description', 'c.view_num', 'a.status', 'a.create_time'])
  67. ->orderBy('a.create_time', 'desc')
  68. ->paginate($pageSize);
  69. $dataList = $dataList ? $dataList->toArray() : [];
  70. if ($dataList) {
  71. foreach ($dataList['data'] as &$item) {
  72. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  73. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  74. }
  75. unset($item);
  76. }
  77. return [
  78. 'code' => 0,
  79. 'success'=> true,
  80. 'msg' => '操作成功',
  81. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  82. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  83. ];
  84. }
  85. /**
  86. * 收藏
  87. * @param $userId
  88. * @return array
  89. */
  90. public function collect($userId){
  91. $params = request()->all();
  92. $id = isset($params['id'])? $params['id'] : 0;
  93. $type = isset($params['type'])? $params['type'] : 1;
  94. $status = isset($params['status'])? $params['status'] : 1;
  95. $type = $type<=0? 1 : $type;
  96. if($id<=0){
  97. return message('参数错误', false);
  98. }
  99. if(!in_array($status, [1,2])){
  100. return message('参数错误', false);
  101. }
  102. $name = $type==1? '收藏':'关注';
  103. $info = $this->model::where(['user_id'=> $userId, 'source_id'=> $id,'type'=> $type])->select(['id','status'])->first();
  104. if($info && $info->status == 1 && $status == 1){
  105. return message("您已{$name}过", false);
  106. }else if($info && $info->status == 2 && $status == 2){
  107. return message("您已取消{$name}", false);
  108. }else if(!$info && $status == 2){
  109. return message("您未{$name}过", false);
  110. }
  111. // 处理
  112. if($info){
  113. $info->status = $status;
  114. $info->create_time = time();
  115. if($info->save()){
  116. return message($status == 1? "{$name}成功":"取消{$name}成功", true);
  117. }
  118. }else{
  119. $data = [
  120. 'user_id'=> $userId,
  121. 'source_id'=> $id,
  122. 'type'=> $type,
  123. 'create_time'=> time(),
  124. 'status'=> 1,
  125. ];
  126. if($this->model::insertGetId($data)){
  127. return message("{$name}成功", true);
  128. }
  129. }
  130. return message('操作失败', false);
  131. }
  132. /**
  133. * 获取详情
  134. * @param $id
  135. */
  136. public function getDetail($id){
  137. $info = $this->model::from('article as a')
  138. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  139. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id])
  140. ->select(['a.id','a.title','a.thumb','a.cate_id','c.name as cate_name','a.view_num','a.description','a.content','a.publish_at','a.create_time'])
  141. ->first();
  142. $info = $info? $info->toArray() : [];
  143. if($info){
  144. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  145. $info['publish_at'] = $info['publish_at']? $info['publish_at'] : datetime( $info['create_time'],'Y-m-d H:i:s');
  146. }
  147. return $info;
  148. }
  149. /**
  150. * 添加或编辑
  151. * @return array
  152. * @since 2020/11/11
  153. * @author wesmiler
  154. */
  155. public function edit()
  156. {
  157. $data = request()->all();
  158. // 图片处理
  159. $type = isset($data['type'])? intval($data['type']) : 0;
  160. $image = $data['thumb']? trim($data['thumb']) : '';
  161. $id = isset($data['id']) ? $data['id'] : 0;
  162. if (!$id && !$image && $type == 1) {
  163. return message('请上传封面图片', false);
  164. }
  165. if (strpos($image, "temp")) {
  166. $data['thumb'] = save_image($image, 'item');
  167. } else {
  168. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  169. }
  170. $data['update_time'] = time();
  171. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  172. return parent::edit($data); // TODO: Change the autogenerated stub
  173. }
  174. }