CollectService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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','c.status']);
  57. $type = isset($params['type']) ? intval($params['type']) : 0;
  58. if ($type > 0) {
  59. $query->where('a.type', $type);
  60. }
  61. })
  62. ->select(['a.id', 'a.user_id','a.source_id', 'c.title as title', 'c.thumb', 'c.description', 'c.view_num', 'a.view_num', 'a.status', 'a.create_time'])
  63. ->orderBy('a.create_time', 'desc')
  64. ->paginate($pageSize);
  65. $dataList = $dataList ? $dataList->toArray() : [];
  66. if ($dataList) {
  67. foreach ($dataList['data'] as &$item) {
  68. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  69. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  70. }
  71. unset($item);
  72. }
  73. return [
  74. 'code' => 0,
  75. 'success'=> true,
  76. 'msg' => '操作成功',
  77. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  78. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  79. ];
  80. }
  81. /**
  82. * 收藏
  83. * @param $userId
  84. * @return array
  85. */
  86. public function collect($userId){
  87. $params = request()->all();
  88. $id = isset($params['id'])? $params['id'] : 0;
  89. $type = isset($params['type'])? $params['type'] : 1;
  90. $status = isset($params['status'])? $params['status'] : 1;
  91. $type = $type<=0? 1 : $type;
  92. if($id<=0){
  93. return message('参数错误', false);
  94. }
  95. if(!in_array($status, [1,2])){
  96. return message('参数错误', false);
  97. }
  98. $info = $this->model::where(['user_id'=> $userId, 'source_id'=> $id,'type'=> $type])->select(['id','status'])->first();
  99. if($info && $info->status == 1 && $status == 1){
  100. return message('您已收藏过', false);
  101. }else if($info && $info->status == 2 && $status == 2){
  102. return message('您已取消收藏', false);
  103. }else if(!$info && $status == 2){
  104. return message('您未收藏过', false);
  105. }
  106. // 处理
  107. if($info){
  108. $info->status = $status;
  109. $info->create_time = time();
  110. if($info->save()){
  111. return message($status == 1? '收藏成功':'取消收藏成功', true);
  112. }
  113. }else{
  114. $data = [
  115. 'user_id'=> $userId,
  116. 'source_id'=> $id,
  117. 'type'=> $type,
  118. 'create_time'=> time(),
  119. 'status'=> 1,
  120. ];
  121. if($this->model::insertGetId($data)){
  122. return message('收藏成功', true);
  123. }
  124. }
  125. return message('操作失败', false);
  126. }
  127. /**
  128. * 获取详情
  129. * @param $id
  130. */
  131. public function getDetail($id){
  132. $info = $this->model::from('article as a')
  133. ->leftJoin('article_cates as c', 'a.cate_id', '=', 'c.id')
  134. ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id])
  135. ->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'])
  136. ->first();
  137. $info = $info? $info->toArray() : [];
  138. if($info){
  139. $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
  140. $info['publish_at'] = $info['publish_at']? $info['publish_at'] : datetime( $info['create_time'],'Y-m-d H:i:s');
  141. }
  142. return $info;
  143. }
  144. /**
  145. * 添加或编辑
  146. * @return array
  147. * @since 2020/11/11
  148. * @author wesmiler
  149. */
  150. public function edit()
  151. {
  152. $data = request()->all();
  153. // 图片处理
  154. $type = isset($data['type'])? intval($data['type']) : 0;
  155. $image = $data['thumb']? trim($data['thumb']) : '';
  156. $id = isset($data['id']) ? $data['id'] : 0;
  157. if (!$id && !$image && $type == 1) {
  158. return message('请上传封面图片', false);
  159. }
  160. if (strpos($image, "temp")) {
  161. $data['thumb'] = save_image($image, 'item');
  162. } else {
  163. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  164. }
  165. $data['update_time'] = time();
  166. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  167. return parent::edit($data); // TODO: Change the autogenerated stub
  168. }
  169. }