visuddhinanda 8 miesięcy temu
rodzic
commit
3570f1790c
1 zmienionych plików z 90 dodań i 39 usunięć
  1. 90 39
      api-v8/app/Http/Api/StudioApi.php

+ 90 - 39
api-v8/app/Http/Api/StudioApi.php

@@ -1,71 +1,122 @@
 <?php
+
 namespace App\Http\Api;
 
 use App\Models\UserInfo;
+use App\Models\GroupInfo;
+use App\Models\GroupMember;
+
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Facades\App;
 
-class StudioApi{
-    public static function getIdByName($name){
+class StudioApi
+{
+    public static function getIdByName($name)
+    {
         /**
          * 获取 uuid
          */
         //TODO 改为studio table
-        if(empty($name)){
+        if (empty($name)) {
             return false;
         }
-        $userInfo = UserInfo::where('username',$name)->first();
-        if(!$userInfo){
-            return false;
-        }else{
+        $userInfo = UserInfo::where('username', $name)->first();
+        if ($userInfo) {
+            //group
             return $userInfo->userid;
+        } else {
+            $group = GroupInfo::where('uid', $name)->first();
+            if ($group) {
+                return $group->uid;
+            } else {
+                return false;
+            }
         }
-
     }
-    public static function getById($id){
+    public static function getById($id)
+    {
         //TODO 改为studio table
-        if(empty($id)){
+        if (empty($id)) {
             return false;
         }
-        $userInfo = UserInfo::where('userid',$id)->first();
-        if(!$userInfo){
-            return false;
-        }
-        $data = [
-            'id'=>$id,
-            'nickName'=>$userInfo->nickname,
-            'realName'=>$userInfo->username,
-            'studioName'=>$userInfo->username,
-        ];
-        if(!empty($userInfo->role)){
-            $data['roles'] = json_decode($userInfo->role);
-        }
-        if($userInfo->avatar){
-            $img = str_replace('.jpg','_s.jpg',$userInfo->avatar);
-            if (App::environment('local')) {
-                $data['avatar'] = Storage::url($img);
-            }else{
-                $data['avatar'] = Storage::temporaryUrl($img, now()->addDays(6));
+        $userInfo = UserInfo::where('userid', $id)->first();
+        if ($userInfo) {
+            $data = [
+                'id' => $id,
+                'nickName' => $userInfo->nickname,
+                'realName' => $userInfo->username,
+                'studioName' => $userInfo->username,
+            ];
+            if (!empty($userInfo->role)) {
+                $data['roles'] = json_decode($userInfo->role);
+            }
+            if ($userInfo->avatar) {
+                $img = str_replace('.jpg', '_s.jpg', $userInfo->avatar);
+                if (App::environment('local')) {
+                    $data['avatar'] = Storage::url($img);
+                } else {
+                    $data['avatar'] = Storage::temporaryUrl($img, now()->addDays(6));
+                }
+            }
+            return $data;
+        } else {
+            $group = GroupInfo::where('uid', $id)->first();
+            if ($group) {
+                $data = [
+                    'id' => $id,
+                    'nickName' => $group->name,
+                    'realName' => $group->uid,
+                    'studioName' => $group->uid,
+                ];
+            } else {
+                return false;
             }
         }
-        return $data;
     }
 
-    public static function getByIntId($id){
+    public static function getByIntId($id)
+    {
         //TODO 改为studio table
-        if(empty($id)){
+        if (empty($id)) {
             return false;
         }
-        $userInfo = UserInfo::where('id',$id)->first();
-        if(!$userInfo){
+        $userInfo = UserInfo::where('id', $id)->first();
+        if (!$userInfo) {
             return false;
         }
         return [
-            'id'=>$userInfo['userid'],
-            'nickName'=>$userInfo['nickname'],
-            'realName'=>$userInfo['username'],
-            'studioName'=>$userInfo['username'],
-            'avatar'=>'',
+            'id' => $userInfo['userid'],
+            'nickName' => $userInfo['nickname'],
+            'realName' => $userInfo['username'],
+            'studioName' => $userInfo['username'],
+            'avatar' => '',
         ];
     }
+
+    public static function userCanManage($userId, $studioId)
+    {
+        if ($userId === $studioId) {
+            return true;
+        }
+        $group = GroupMember::where('user_id', $userId)
+            ->where('group_id', $studioId)
+            ->first();
+        if ($group && $group->power <= 1) {
+            return true;
+        }
+        return false;
+    }
+    public static function userCanList($userId, $studioId)
+    {
+        if ($userId === $studioId) {
+            return true;
+        }
+        $group = GroupMember::where('user_id', $userId)
+            ->where('group_id', $studioId)
+            ->first();
+        if ($group && $group->power <= 2) {
+            return true;
+        }
+        return false;
+    }
 }