MemberFollowTopicRepository.php 4.7 KB

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