MemberFollowTopicRepository.php 5.0 KB

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