wzq преди 5 години
родител
ревизия
35a9388797
променени са 3 файла, в които са добавени 64 реда и са изтрити 16 реда
  1. 1 1
      app/Http/Controllers/V2/MemberGroupController.php
  2. 34 15
      app/Repositories/MemberGroupRepository.php
  3. 29 0
      app/Traits/UserTrait.php

+ 1 - 1
app/Http/Controllers/V2/MemberGroupController.php

@@ -22,7 +22,7 @@ class MemberGroupController extends Controller {
     public function memberList(Request $request){
         $data = $request->all();
         $validator = Validator::make($data, [
-            'take_count' => 'integer|max:50',
+            'take_count' => 'required|integer|max:50',
         ]);
         if ($validator->fails()) {
             return $this->jsonError($validator->errors()->first());

+ 34 - 15
app/Repositories/MemberGroupRepository.php

@@ -12,6 +12,7 @@ namespace App\Repositories;
 use App\Models\MemberGroup;
 use App\Models\MemberGroupInfo;
 use App\Traits\UserTrait;
+use Illuminate\Support\Facades\Redis;
 
 class MemberGroupRepository {
     use UserTrait;
@@ -26,31 +27,49 @@ class MemberGroupRepository {
      * 获取推荐用户
      */
     public function isSuggestMember($request){
+        $userInfo = $this->getUserInfo();
+        if (empty($userInfo)) {
+            return jsonError('获取用户信息失败');
+        }
        $group = $this->memberGroup->where('is_suggest',1)->first();
        $take = 50;
        if(!empty($request['take_count'])){
            $take = $request['take_count'];
        }
        if($group){
-           $groupInfo = $this->memberGroupInfo
+           $ids = $this->memberGroupInfo
                ->where('member_group_id',$group->id)
                ->orderBy('sort')
                ->take($take)
-               ->select('uid','sort')
-               ->get();
-           if($groupInfo){
-               $array  = $groupInfo->toArray();
-               $userData = [];
-               foreach ($array as $key=>$value){
-                   $userData[] = $value['uid'];
+               ->pluck('uid')
+               ->toArray();
+           if($ids){
+               $data = [];
+               foreach($ids as $uid){
+                   $user = Redis::HGETALL('userInfo:'.$uid);
+                   if(!$user){
+                       $user = $this->userInfo($uid);
+                   }
+                   if($user){
+                       $topics = Redis::zrevrange('topic.user_uid' . $user['uid'], 0, 2, 'withscores');
+                       $names = [];
+                       foreach($topics as $key => $item){
+                           $topicNameArray = Redis::ZRANGEBYSCORE('topic.name', $key, $key);
+                           if($topicNameArray){
+                               $names[] = reset($topicNameArray);
+                           }
+                       }
+                       $data[] = [
+                           'uid' => $user['uid'],
+                           'username' => $user['username'],
+                           'avatar' => $user['avatar'],
+                           'follow_status' => $this->getFollowStatus($userInfo['uid'], $user['uid']),
+                           'follow_topic' => $names,
+                           'topic_name' => $names[0]??'',
+                       ];
+                   }
                }
-               //请求用户接口获取用户信息
-               $uids = implode(',',$userData);
-               $data = $this->getMemberSortIdList($uids);
-               if($data){
-                   return jsonSuccess(['list'=>$data]);
-               }
-               return jsonSuccess();
+               return jsonSuccess(['list'=>$data]);
            }
        }
        return jsonSuccess();

+ 29 - 0
app/Traits/UserTrait.php

@@ -7,6 +7,7 @@
  * Time: 17:11
  */
 namespace App\Traits;
+use Illuminate\Support\Facades\Redis;
 use Tymon\JWTAuth\Facades\JWTAuth;
 
 trait UserTrait
@@ -26,6 +27,21 @@ trait UserTrait
 
     }
 
+    //获取用户信息
+    public function userInfo($uid) {
+        try {
+            $sign = generateSign(['uid' => $uid], config('customer.app_secret'));
+            $url = config("customer.app_service_url").'/user/getUserInfo';
+            $array = [
+                'json' => ['sign' => $sign,'uid' => $uid], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
+            ];
+            return http($url,$array,'get');
+        } catch (\Exception $e) {
+            return [];
+        }
+
+    }
+
     public function getBanner($ids) {
         try {
             $sign = generateSign(['ids' => $ids], config('customer.app_secret'));
@@ -96,4 +112,17 @@ trait UserTrait
         }
 
     }
+
+    //获取关注状态
+    public function getFollowStatus($uid, $followUid)
+    {
+        $status = 0;
+        if(Redis::ZSCORE('follow:'.$uid, $followUid)){
+            $status = 1;
+            if(Redis::ZSCORE('follow:'.$followUid, $uid)){
+                $status = 2;
+            }
+        }
+        return $status;
+    }
 }