MemberFollowTopicRepository.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 App\Models\Topic;
  11. use Illuminate\Database\QueryException;
  12. use Illuminate\Support\Carbon;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Redis;
  15. use Tymon\JWTAuth\Facades\JWTAuth;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\Log;
  18. class MemberFollowTopicRepository {
  19. public function __construct(MemberFollowTopic $memberFollowTopic,
  20. Topic $topic,
  21. CategoryRepository $categoryRepository) {
  22. $this->memberFollowTopic = $memberFollowTopic;
  23. $this->topic = $topic;
  24. $this->categoryRepository = $categoryRepository;
  25. }
  26. //关注话题
  27. public function setMemberFollowTopic($topic_group_ids){
  28. $data = $this->categoryRepository->getTopics($topic_group_ids);
  29. //获取uid
  30. $token = JWTAuth::decode(JWTAuth::getToken());
  31. if($data){
  32. $category_topic_data = [];
  33. $date = Carbon::now()->toDateTimeString();
  34. $topicIds = [];
  35. foreach($data as $value){
  36. $topicIds[] = $value['id'];
  37. $category_topic_data[] = [
  38. 'uid' => $token['user']->uid,
  39. 'topic_id' => $value['id'],
  40. 'created_at' => $date,
  41. 'updated_at' => $date,
  42. ];
  43. }
  44. //判断用户是否关注过话题
  45. if($this->memberFollowTopic->where('uid', $token['user']->uid)->exists()){
  46. return jsonSuccess();
  47. }
  48. DB::beginTransaction();
  49. try{
  50. $res = $this->memberFollowTopic->insert($category_topic_data);
  51. $this->topic->whereIn('id', $topicIds)->increment('follow_count');
  52. $topic = $this->updataMemberFollowSuggestTopic();
  53. DB::commit();
  54. if($res){
  55. //更改关注状态
  56. //用户话题集合
  57. $key = 'topic.user_uid'.$token['user']->uid;
  58. foreach($data as $value){
  59. if(!Redis::zscore($key, $value['id'])){
  60. Redis::zincrby($key, 0, $value['id']);
  61. }
  62. }
  63. return jsonSuccess();
  64. }else{
  65. return jsonError('关注失败');
  66. }
  67. }catch (QueryException $exception){
  68. DB::rollBack();
  69. Log::debug('新增话题:'.$exception->getMessage());
  70. return jsonError('关注失败');
  71. }
  72. }
  73. }
  74. //修改关注状态
  75. public function updataMemberFollowSuggestTopic(){
  76. try {
  77. $sign = generateSign([], config('customer.app_secret'));
  78. $url = config("customer.app_service_url").'/user/v2/member/updateFollowSuggestTopic';
  79. //$url = 'http://localhost:8080/v2/member/updateFollowSuggestTopic';
  80. $array = [
  81. 'json' => ['sign' => $sign], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
  82. ];
  83. return http($url,$array,'put');
  84. } catch (\Exception $e) {
  85. return [];
  86. }
  87. }
  88. //关注单个话题
  89. public function follow($topic_id){
  90. $token = JWTAuth::decode(JWTAuth::getToken());
  91. $info = $this->memberFollowTopic->where(['topic_id'=>$topic_id,'uid'=>$token['user']->uid])->first();
  92. if($info){
  93. return jsonError('您已关注该话题');
  94. }
  95. $data = ['uid'=>$token['user']->uid,'topic_id'=>$topic_id];
  96. DB::beginTransaction();
  97. try{
  98. $this->memberFollowTopic->create($data);
  99. $this->topic->where('id', $topic_id)->increment('follow_count');
  100. DB::commit();
  101. $key = 'topic.user_uid'.$token['user']->uid;
  102. if(!Redis::zscore($key, $topic_id)){
  103. Redis::zincrby($key, 0, $topic_id);
  104. }
  105. return jsonSuccess();
  106. }catch (QueryException $exception){
  107. DB::rollBack();
  108. Log::error('关注失败:'.$exception->getMessage());
  109. return jsonError('关注失败');
  110. }
  111. }
  112. //取消关注话题
  113. public function cancel($topic_id){
  114. $token = JWTAuth::decode(JWTAuth::getToken());
  115. $info = $this->memberFollowTopic->where(['topic_id'=>$topic_id,'uid'=>$token['user']->uid])->first();
  116. if(!$info){
  117. return jsonError('您没有关注该话题');
  118. }
  119. DB::beginTransaction();
  120. try{
  121. $this->memberFollowTopic->where('id',$info->id)->delete();
  122. $this->topic->where('id', $topic_id)->decrement('follow_count');
  123. DB::commit();
  124. return jsonSuccess();
  125. }catch (QueryException $exception){
  126. DB::rollBack();
  127. Log::error('取消关注失败:'.$exception->getMessage());
  128. return jsonError('取消关注失败');
  129. }
  130. }
  131. public function list($request){
  132. $token = JWTAuth::decode(JWTAuth::getToken());
  133. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  134. $where[] = ['uid',$token['user']->uid];
  135. if(isset($request['name'])){
  136. $where[] = ['topic.name', 'like', "%{$request['name']}%"];
  137. }
  138. return $this->memberFollowTopic
  139. ->leftJoin('topic', 'member_follow_topic.topic_id', '=', 'topic.id')
  140. ->where($where)
  141. ->select('member_follow_topic.id','uid','topic_id','topic.name as topic_name','topic.is_suggest')
  142. ->paginate($perPage);
  143. }
  144. //获取用户关注话题
  145. public function getMemberTopic($uid){
  146. return $this->memberFollowTopic
  147. ->leftJoin('topic', 'member_follow_topic.topic_id', '=', 'topic.id')
  148. ->where(['member_follow_topic.uid'=>$uid])
  149. //->select('member_follow_topic.id','uid','topic_id','topic.name as topic_name','topic.is_suggest')
  150. ->select('topic.name as topic_name')
  151. ->take(3)
  152. ->get();
  153. }
  154. }