MemberFollowTopicRepository.php 6.3 KB

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