CollectService.php 7.2 KB

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