MemberFollowTopicRepository.php 6.0 KB

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