MemberFollowTopicRepository.php 5.2 KB

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