Sfoglia il codice sorgente

支持default_channel

visuddhinanda 2 anni fa
parent
commit
9a40e40471

+ 60 - 58
app/Http/Controllers/CollectionController.php

@@ -28,11 +28,18 @@ class CollectionController extends Controller
                 //
         $userinfo = new \UserInfo();
 		$result=false;
-		$indexCol = ['uid','title','subtitle','summary','article_list','owner','status','lang','updated_at','created_at'];
+		$indexCol = ['uid','title','subtitle','summary',
+                      'article_list','owner','status',
+                      'default_channel','lang',
+                      'updated_at','created_at'];
 		switch ($request->get('view')) {
             case 'studio_list':
 		        $indexCol = ['owner'];
-                $table = Collection::select($indexCol)->selectRaw('count(*) as count')->where('status', 30)->groupBy('owner');
+                //TODO ?
+                $table = Collection::select($indexCol)
+                                    ->selectRaw('count(*) as count')
+                                    ->where('status', 30)
+                                    ->groupBy('owner');
                 break;
 			case 'studio':
                 $user = AuthApi::current($request);
@@ -67,7 +74,7 @@ class CollectionController extends Controller
 				break;
 			default:
 				# code...
-			    return $this->error("没有查询到数据");
+			    return $this->error("无法识别的view参数",200,200);
 				break;
 		}
         if($request->has("search") && !empty($request->has("search"))){
@@ -150,34 +157,31 @@ class CollectionController extends Controller
     public function store(Request $request)
     {
         $user = \App\Http\Api\AuthApi::current($request);
-        if($user){
-            //判断当前用户是否有指定的studio的权限
-            if($user['user_uid'] === \App\Http\Api\StudioApi::getIdByName($request->get('studio'))){
-                //查询是否重复
-                if(Collection::where('title',$request->get('title'))->where('owner',$user['user_uid'])->exists()){
-                    return $this->error(__('validation.exists'));
-                }else{
-                    $newOne = new Collection;
-                    $newOne->id = app('snowflake')->id();
-                    $newOne->uid = Str::uuid();
-                    $newOne->title = $request->get('title');
-                    $newOne->lang = $request->get('lang');
-                    $newOne->article_list = "[]";
-                    $newOne->owner = $user['user_uid'];
-                    $newOne->owner_id = $user['user_id'];
-                    $newOne->editor_id = $user['user_id'];
-                    $newOne->create_time = time()*1000;
-                    $newOne->modify_time = time()*1000;
-                    $newOne->save();
-                    return $this->ok(new CollectionResource($newOne));
-                }
-            }else{
-                return $this->error(__('auth.failed'));
-            }
+        if(!$user){
+            return $this->error(__('auth.failed'),401,401);
+        }
+        //判断当前用户是否有指定的studio的权限
+        if($user['user_uid'] !== \App\Http\Api\StudioApi::getIdByName($request->get('studio'))){
+            return $this->error(__('auth.failed'),403,403);
+        }
+        //查询是否重复
+        if(Collection::where('title',$request->get('title'))->where('owner',$user['user_uid'])->exists()){
+            return $this->error(__('validation.exists'),200,200);
         }else{
-            return $this->error(__('auth.failed'));
+            $newOne = new Collection;
+            $newOne->id = app('snowflake')->id();
+            $newOne->uid = Str::uuid();
+            $newOne->title = $request->get('title');
+            $newOne->lang = $request->get('lang');
+            $newOne->article_list = "[]";
+            $newOne->owner = $user['user_uid'];
+            $newOne->owner_id = $user['user_id'];
+            $newOne->editor_id = $user['user_id'];
+            $newOne->create_time = time()*1000;
+            $newOne->modify_time = time()*1000;
+            $newOne->save();
+            return $this->ok(new CollectionResource($newOne));
         }
-
     }
 
     /**
@@ -188,34 +192,31 @@ class CollectionController extends Controller
      */
     public function show(Request  $request,$id)
     {
-        //
-		$indexCol = ['uid','title','subtitle','summary','article_list','status','owner','lang','updated_at','created_at'];
-
-		$result  = Collection::select($indexCol)->where('uid', $id)->first();
-		if($result){
-            if($result->status<30){
-                //私有文章,判断权限
-                Log::error('私有文章,判断权限'.$id);
-                $user = \App\Http\Api\AuthApi::current($request);
-                if(!$user){
-                    Log::error('未登录');
-                    return $this->error(__('auth.failed'));
-                }
-                //判断当前用户是否有指定的studio的权限
-                if($user['user_uid'] !== $result->owner){
-                    Log::error($user["user_uid"].'私有文章,判断权限'.$id);
-                    //非所有者
-                    if(CollectionController::UserCanRead($user['user_uid'],$result)===false){
-                        Log::error($user["user_uid"].'没有读取权限');
-                        return $this->error(__('auth.failed'));
-                    }
+		$result  = Collection::where('uid', $id)->first();
+		if(!$result){
+            return $this->error("没有查询到数据");
+        }
+        if($result->status<30){
+            //私有文章,判断权限
+            Log::error('私有文章,判断权限'.$id);
+            $user = \App\Http\Api\AuthApi::current($request);
+            if(!$user){
+                Log::error('未登录');
+                return $this->error(__('auth.failed'),401,401);
+            }
+            //判断当前用户是否有指定的studio的权限
+            if($user['user_uid'] !== $result->owner){
+                Log::error($user["user_uid"].'私有文章,判断权限'.$id);
+                //非所有者
+                if(CollectionController::UserCanRead($user['user_uid'],$result)===false){
+                    Log::error($user["user_uid"].'没有读取权限');
+                    return $this->error(__('auth.failed'),403,403);
                 }
             }
-            $result->fullArticleList = true;
-			return $this->ok(new CollectionResource($result));
-		}else{
-			return $this->error("没有查询到数据");
-		}
+        }
+        $result->fullArticleList = true;
+        return $this->ok(new CollectionResource($result));
+
     }
 
     /**
@@ -235,19 +236,20 @@ class CollectionController extends Controller
         //鉴权
         $user = AuthApi::current($request);
         if(!$user){
-            return $this->error(__('auth.failed'));
+            return $this->error(__('auth.failed'),401,401);
         }
         if(!CollectionController::UserCanEdit($user["user_uid"],$collection)){
-            return $this->error(__('auth.failed'));
+            return $this->error(__('auth.failed'),403,403);
         }
         $collection->title = $request->get('title');
         $collection->subtitle = $request->get('subtitle');
         $collection->summary = $request->get('summary');
         if($request->has('aritcle_list')){
             $collection->article_list = \json_encode($request->get('aritcle_list'));
-        } ;
+        }
         $collection->lang = $request->get('lang');
         $collection->status = $request->get('status');
+        $collection->default_channel = $request->get('default_channel');
         $collection->modify_time = time()*1000;
         $collection->save();
         return $this->ok(new CollectionResource($collection));

+ 5 - 0
app/Http/Resources/CollectionResource.php

@@ -4,6 +4,7 @@ namespace App\Http\Resources;
 
 use Illuminate\Http\Resources\Json\JsonResource;
 use App\Http\Api\StudioApi;
+use App\Http\Api\ChannelApi;
 use App\Models\ArticleCollection;
 
 
@@ -29,6 +30,10 @@ class CollectionResource extends JsonResource
             "created_at" => $this->created_at,
             "updated_at" => $this->updated_at,
         ];
+        $channel = ChannelApi::getById($this->default_channel);
+        if($channel){
+            $data['default_channel'] = $channel;
+        }
         if($this->fullArticleList===true){
             $data["article_list"] = \json_decode($this->article_list);
         }else{