Browse Source

修改部分接口参数验证和返回json数据

tangs 6 years ago
parent
commit
a4dba7970e

+ 29 - 11
app/Http/Controllers/ActionBarController.php

@@ -17,7 +17,7 @@ class ActionBarController extends Controller
             "description" => "max:255",
             "icon" => "max:255",
             "link_type" => "between:1,32",
-            "link" => "max255",
+            "link" => "max:255",
         ]);
         if ($validator->fails()) {
             return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
@@ -42,12 +42,13 @@ class ActionBarController extends Controller
     public function ModifyActionBar(Request $request)
     {
         $validator = Validator::make($request->all(), [
-            "parent" => "",
-            "name" => "required|between:1,64",
+            "id" => "required|integer|min:1",
+            "parent" => "integer|min:0",
+            "name" => "between:1,64",
             "description" => "max:255",
             "icon" => "max:255",
-            "link_type" => "",
-            "link" => "",
+            "link_type" => "max:32",
+            "link" => "max:255",
         ]);
         if ($validator->fails()) {
             return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
@@ -64,29 +65,46 @@ class ActionBarController extends Controller
         $params["name"] = $request->input("name");
 
         $bar = new Models\ActionBar();
-        $result = $bar->ModifyActionBar($params);
-        return $result;
+        $code = $bar->ModifyActionBar($params);
+        return $this->response($code, $this->error[$code]);
     }
 
     public function DeleteActionBar(Request $request)
     {
+        $validator = Validator::make($request->all(), [
+            "id" => "required|integer|min:1",
+        ]);
+        if ($validator->fails()) {
+            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+        }
+
         $params = [];
-        $bid = $request->input("id");
+        $bid = (int)$request->input("id");
         $params["id"] = $bid;
 
         $bar = new Models\ActionBar();
-        $result = $bar->DeleteActionBar($params);
-        return $result;
+        $code = $bar->DeleteActionBar($params);
+        return $this->response($code, $this->error[$code]);
     }
 
     public function LoadActionBar(Request $request)
     {
+        $validator = Validator::make($request->all(), [
+            "uid" => "required|integer|min:1",
+        ]);
+        if ($validator->fails()) {
+            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+        }
+
         $params = [];
         $uid = $request->input("uid");
         $params["uid"] = $uid;
 
         $bar = new Models\ActionBar();
         $result = $bar->ListActionBar($params);
-        return "success";
+        if ($result["code"] == 0){
+            return $this->success($result["data"]);
+        }
+        return $this->fail($result["code"], $this->error[$result["code"]]);
     }
 }

+ 2 - 2
app/Http/Controllers/PermissionController.php

@@ -35,8 +35,8 @@ class PermissionController extends Controller
         }
 
         // 有权分配
-        $userActionBar = new Models\UserActionBarController();
-        $userActionBar->AddUserActionBar($uid, $barIds);
+        $userActionBar = new Models\UserActionBar();
+        $userActionBar->AddUserActionBar($uid, $barIds, "");
         return "success";
     }
 

+ 26 - 26
app/Models/ActionBar.php

@@ -13,7 +13,7 @@ class ActionBar extends Model
 {
     protected $table = "action_bars";
     public $timestamps = false;
-    protected $fillable = ["name"];
+    protected $fillable = ["parent", "icon", "link_type", "link", "name", "description"];
 
     /**
      * @param array $params
@@ -60,7 +60,7 @@ class ActionBar extends Model
         $update = [];
         $id = $params["id"];
         if ($id == "") {
-            return "invalid id";
+            return EMPTY_ACTION_BAR_ID;
         }
         if ($params["parent"] > 0) {
             $update["parent"] = $params["parent"];
@@ -82,35 +82,37 @@ class ActionBar extends Model
             $update["description"] = $params["description"];
         }
         if (count($update) == 0) {
-            return "nothing to update";
+            return NOTHING_UPDATE;
         }
 
-        $num = $this->where("id", $id)
-            ->where("status", "normal")
-            ->where("is_del", false)
-            ->update($update);
-        return "success";
-    }
+        // check the data if exist in system.
+        $actionBar = $this->where("id", $id)->where("is_del", false)->first();
+        if (!$actionBar) {
+            return INVALID_ACTION_BAR_ID;
+        }
 
-    // todo.
+        $this->where("id", $id)->where("is_del", false)->update($update);
+        return SUCCESS;
+    }
 
     /**
-     * 删除一个菜单项目,这里存在一个问题,就是如果删除上层的菜单,下层的菜单也应该都被删掉。先留空。
+     * 删除一个菜单项目,这里存在一个问题,就是如果删除上层的菜单,下层的菜单也应该都被删掉。但是可以通过parent来控制
+     * 下层菜单不被加载出来
      *
      * @param array $params
-     * @return string
+     * @return int
      */
     public function DeleteActionBar(array $params)
     {
         $id = $params["id"];
-        if ($id == "") {
-            return "invalid id";
+        if ($id < 1) {
+            return EMPTY_ACTION_BAR_ID;
+        }
+        $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
+        if ($row == 0) {
+            return INVALID_ACTION_BAR_ID;
         }
-        $num = $this->where("id", $id)
-            ->where("status", "normal")
-            ->where("is_del", false)
-            ->update(["is_del" => true]);
-        return "success";
+        return SUCCESS;
     }
 
     /**
@@ -125,18 +127,16 @@ class ActionBar extends Model
 
         // 收集当前用户有权限的bar_id
         $userActionBar = new UserActionBar();
-        $bar_ids = $userActionBar->ListActionBarIds($uid);
+        $bar_ids = $userActionBar->ListActionBarIds("", $uid);
         if (count($bar_ids) == 0) {
-            return [];
+            return ["code" => SUCCESS, "data" => []];
         }
 
         // 查出bars的信息
-        $result = $this->select("id", "parent", "level", "name", "description", "icon", "link_type", "link")
+        $result = $this->select("id", "parent", "name", "description", "icon", "link_type", "link")
             ->where("id", $bar_ids)
-            ->where("status", "normal")
-            ->where("is_del", false)
-            ->all();
-        return $result;
+            ->where("status", "normal")->where("is_del", false)->get();
+        return ["code" => SUCCESS, "data" => $result];
     }
 }
 

+ 12 - 9
app/Models/UserActionBar.php

@@ -3,6 +3,7 @@
 namespace App\Models;
 
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\Log;
 
 class UserActionBar extends Model
 {
@@ -68,23 +69,25 @@ class UserActionBar extends Model
     }
 
     /**
-     * @param $uid
+     * @param $ownerType
+     * @param $ownerId
      * @return array
      */
-    public function ListActionBarIds($uid)
+    public function ListActionBarIds($ownerType, $ownerId)
     {
-        $bars = $this->select("bar_id")
-            ->where("user_id", $uid)
-            ->where("status", "normal")
-            ->where("is_del", false)
-            ->get();
+        $opera = $this->select("bar_id");
+        if ($ownerType != "") {
+            $opera = $opera->where("owner_type", $ownerType);
+        }
+        $bars = $opera->where("owner_id", $ownerId)->where("status", "normal")->where("is_del", false)->get();
         if (count($bars) == 0) {
             return [];
         }
         $bar_ids = [];
-        foreach ($bar_ids as $bar_id) {
-            array_push($bar_ids, $bar_id->bar_id);
+        foreach ($bars as $bar) {
+            array_push($bar_ids, $bar["bar_id"]);
         }
+        Log::debug("========" . json_encode($bar_ids));
         return $bar_ids;
     }
 }

+ 4 - 0
config/errorcode.php

@@ -33,6 +33,8 @@ const EMPTY_GROUP_USER_ID = 710;
 const INVALID_GROUP_USER_ID = 711;
 
 const EMPTY_ACTION_BAR_NAME = 810;
+const EMPTY_ACTION_BAR_ID = 811;
+const INVALID_ACTION_BAR_ID= 812;
 const INVALID_ACTION_PARENT = 813;
 return [
     "0" => "success",
@@ -66,5 +68,7 @@ return [
     "711" => "invalid group-user id",
 
     "810" => "empty action-bar name",
+    "811" => "empty action-bar id",
+    "812" => "invalid action-bar id",
     "813" => "invalid action-bar parent",
 ];