MemberFollowTopicRepository.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019-06-15
  6. * Time: 15:01
  7. */
  8. namespace App\Repositories;
  9. use App\Models\MemberFollowTopic;
  10. use Tymon\JWTAuth\Facades\JWTAuth;
  11. use Illuminate\Support\Facades\Auth;
  12. class MemberFollowTopicRepository {
  13. public function __construct(MemberFollowTopic $memberFollowTopic,CategoryRepository $categoryRepository) {
  14. $this->memberFollowTopic = $memberFollowTopic;
  15. $this->categoryRepository = $categoryRepository;
  16. }
  17. //关注话题
  18. public function setMemberFollowTopic($topic_group_ids){
  19. $data = $this->categoryRepository->getTopics($topic_group_ids);
  20. //获取uid
  21. $token = JWTAuth::decode(JWTAuth::getToken());
  22. if($data){
  23. $category_topic_data = [];
  24. foreach($data as $value){
  25. $category_topic_data[] = [
  26. 'uid' => $token['user']->uid,
  27. 'topic_id' => $value['id'],
  28. ];
  29. }
  30. $res = $this->memberFollowTopic->insert($category_topic_data);
  31. if($res){
  32. return jsonSuccess();
  33. }else{
  34. return jsonError('关注失败');
  35. }
  36. }
  37. }
  38. //关注单个话题
  39. public function follow($topic_id){
  40. $token = JWTAuth::decode(JWTAuth::getToken());
  41. $info = $this->memberFollowTopic->where(['topic_id'=>$topic_id,'uid'=>$token['user']->uid])->first();
  42. if($info){
  43. return jsonError('您已关注该话题');
  44. }
  45. $data = ['uid'=>$token['user']->uid,'topic_id'=>$topic_id];
  46. $res = $this->memberFollowTopic->create($data);
  47. if($res){
  48. return jsonSuccess();
  49. }else{
  50. return jsonError('关注失败');
  51. }
  52. }
  53. //取消关注话题
  54. public function cancel($topic_id){
  55. $token = JWTAuth::decode(JWTAuth::getToken());
  56. $info = $this->memberFollowTopic->where(['topic_id'=>$topic_id,'uid'=>$token['user']->uid])->first();
  57. if(!$info){
  58. return jsonError('您没有关注该话题');
  59. }
  60. $res = $this->memberFollowTopic->where('id',$info->id)->delete();
  61. if($res){
  62. return jsonSuccess();
  63. }else{
  64. return jsonError('取关失败');
  65. }
  66. }
  67. public function list($request){
  68. $token = JWTAuth::decode(JWTAuth::getToken());
  69. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  70. $where[] = ['uid',$token['user']->uid];
  71. if(isset($request['name'])){
  72. $where[] = ['topic.name', 'like', "%{$request['name']}%"];
  73. }
  74. return $this->memberFollowTopic
  75. ->leftJoin('topic', 'member_follow_topic.topic_id', '=', 'topic.id')
  76. ->where($where)
  77. ->select('member_follow_topic.id','uid','topic_id','topic.name as topic_name','topic.is_suggest')
  78. ->paginate($perPage);
  79. }
  80. //获取用户关注话题
  81. public function getMemberTopic($uid){
  82. return $this->memberFollowTopic
  83. ->leftJoin('topic', 'member_follow_topic.topic_id', '=', 'topic.id')
  84. ->where(['member_follow_topic.uid'=>$uid])
  85. //->select('member_follow_topic.id','uid','topic_id','topic.name as topic_name','topic.is_suggest')
  86. ->select('topic.name as topic_name')
  87. ->take(3)
  88. ->get();
  89. }
  90. }