Browse Source

add getByName()

visuddhinanda 1 year ago
parent
commit
a6fac1397a
1 changed files with 50 additions and 54 deletions
  1. 50 54
      api-v8/app/Http/Api/UserApi.php

+ 50 - 54
api-v8/app/Http/Api/UserApi.php

@@ -1,4 +1,5 @@
 <?php
+
 namespace App\Http\Api;
 
 use App\Models\UserInfo;
@@ -6,63 +7,46 @@ use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Facades\App;
 
-class UserApi{
-    public static function getIdByName($name){
-        return UserInfo::where('username',$name)->value('userid');
+class UserApi
+{
+    public static function getIdByName($name)
+    {
+        return UserInfo::where('username', $name)->value('userid');
     }
-    public static function getIdByUuid($uuid){
-        return UserInfo::where('userid',$uuid)->value('id');
+    public static function getIdByUuid($uuid)
+    {
+        return UserInfo::where('userid', $uuid)->value('id');
     }
-    public static function getIntIdByName($name){
-        return UserInfo::where('username',$name)->value('id');
+    public static function getIntIdByName($name)
+    {
+        return UserInfo::where('username', $name)->value('id');
+    }
+    public static function getById($id)
+    {
+        $user = UserInfo::where('id', $id)->first();
+        return UserApi::userInfo($user);
     }
-    public static function getById($id){
-        $user = UserInfo::where('id',$id)->first();
-        if($user){
-            return [
-                'id'=>$id,
-                'uid'=>$user->userid,
-                'nickName'=>$user['nickname'],
-                'userName'=>$user['username'],
-                'realName'=>$user['username'],
-                'avatar'=>'',
-            ];
-        }else{
-            Log::error('$user=null;$id='.$id);
-            return [
-                'id'=>0,
-                'nickName'=>'unknown',
-                'userName'=>'unknown',
-                'realName'=>'unknown',
-                'avatar'=>'',
-            ];
-        }
 
+    public static function getByName($name)
+    {
+        $user = UserInfo::where('username', $name)->first();
+        return UserApi::userInfo($user);
     }
-    public static function getByUuid($id){
-        $user = UserInfo::where('userid',$id)->first();
-        if($user){
-            return UserApi::userInfo($user);
-        }else{
-            Log::error('$user=null;$id='.$id);
-            return [
-                'id'=>0,
-                'nickName'=>'unknown',
-                'userName'=>'unknown',
-                'realName'=>'unknown',
-                'avatar'=>'',
-            ];
-        }
+    public static function getByUuid($id)
+    {
+        $user = UserInfo::where('userid', $id)->first();
+        return UserApi::userInfo($user);
     }
-    public static function getListByUuid($uuid){
-        if(!$uuid || !is_array($uuid)){
+    public static function getListByUuid($uuid)
+    {
+        if (!$uuid || !is_array($uuid)) {
             return null;
         };
-        $users = UserInfo::whereIn('userid',$uuid)->get();
+        $users = UserInfo::whereIn('userid', $uuid)->get();
         $output = array();
         foreach ($uuid as $key => $id) {
             foreach ($users as $user) {
-                if($user->userid === $id){
+                if ($user->userid === $id) {
                     $output[] = UserApi::userInfo($user);
                     continue;
                 };
@@ -70,21 +54,33 @@ class UserApi{
         }
         return $output;
     }
-    public static function userInfo($user){
+    public static function userInfo($user)
+    {
+        if (!$user) {
+            Log::error('$user=null;');
+            return [
+                'id' => 0,
+                'nickName' => 'unknown',
+                'userName' => 'unknown',
+                'realName' => 'unknown',
+                'avatar' => '',
+            ];
+        }
         $data = [
             'id' => $user->userid,
-            'nickName'=>$user->nickname,
-            'userName'=>$user->username,
-            'realName'=>$user->username,
+            'nickName' => $user->nickname,
+            'userName' => $user->username,
+            'realName' => $user->username,
+            'sn' => $user->id,
         ];
-        if(!empty($user->role)){
+        if (!empty($user->role)) {
             $data['roles'] = json_decode($user->role);
         }
-        if($user->avatar){
-            $img = str_replace('.jpg','_s.jpg',$user->avatar);
+        if ($user->avatar) {
+            $img = str_replace('.jpg', '_s.jpg', $user->avatar);
             if (App::environment('local')) {
                 $data['avatar'] = Storage::url($img);
-            }else{
+            } else {
                 $data['avatar'] = Storage::temporaryUrl($img, now()->addDays(6));
             }
         }