Browse Source

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

tangs 6 years ago
parent
commit
2a748f1e3d

+ 19 - 0
app/Http/Controllers/Controller.php

@@ -52,4 +52,23 @@ class Controller extends BaseController
             'message' => $message
         ]);
     }
+
+
+    /**
+     * HTTP请求响应
+     *
+     * @param $code
+     * @param string $codeMessage
+     * @param null $message
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function response($code, $codeMessage = "", $message = null)
+    {
+        return response()->json([
+            'code' => (int)$code,
+            "codeMessage" => $codeMessage,
+            'message' => $message
+        ]);
+    }
+
 }

+ 49 - 7
app/Http/Controllers/RoleController.php

@@ -4,47 +4,89 @@ namespace App\Http\Controllers;
 
 use App\Models;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Validator;
 
 class RoleController extends Controller
 {
     public function CreateRole(Request $request)
     {
+        $validator = Validator::make($request->all(), [
+            "name" => "required|between:1,64",
+            "description" => "max:255",
+        ]);
+        if ($validator->fails()) {
+            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+        }
+
         $params = [];
         $params["name"] = $request->input("name");
         $params["description"] = $request->input("description");
 
         $role = new Models\Role();
         $result = $role->CreateRole($params);
-        return $result;
+        if ($result["code"] == 0) {
+            return $this->success($result["data"]);
+        }
+        return $this->fail($result["code"], $this->error[$result["code"]]);;
     }
 
     public function ModifyRole(Request $request)
     {
+        $validator = Validator::make($request->all(), [
+            "id" => "required|integer|min:1",
+            "name" => "between:1,64",
+            "description" => "max:255",
+        ]);
+        if ($validator->fails()) {
+            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+        }
+
         $params = [];
         $params["id"] = $request->input("id");
         $params["name"] = $request->input("name");
         $params["description"] = $request->input("description");
 
         $role = new Models\Role();
-        $result = $role->ModifyRole($params);
-        return $result;
+        $code = $role->ModifyRole($params);
+        if ($code == 0) {
+            return $this->success("success");
+        }
+        return $this->fail($code, $this->error[$code]);
     }
 
     public function DeleteRole(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 = [];
         $params["id"] = $request->input("id");
 
         $role = new Models\Role();
-        $result = $role->DeleteRole($params);
-        return $result;
+        $code = $role->DeleteRole($params);
+        if ($code == 0) {
+            return $this->success("success");
+        }
+        return $this->fail($code, $this->error[$code]);
     }
 
     public function ListRole(Request $request)
     {
+        $validator = Validator::make($request->all(), [
+            "page" => "integer|min:1",
+            "pageCount" => "integer|min:1"
+        ]);
+        if ($validator->fails()) {
+            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+        }
+
         $params = [];
-        $params["page"] = $request->input("page");
-        $params["pageCount"] = $request->input("pageCount");
+        $params["page"] = (int)$request->input("page");
+        $params["pageCount"] = (int)$request->input("pageCount");
 
         $role = new Models\Role();
         $result = $role->ListRole($params);

+ 35 - 11
app/Http/Controllers/UserActionBarController.php

@@ -4,29 +4,53 @@ namespace App\Http\Controllers;
 
 use App\Models;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Validator;
 
 // 这个class的方法需要在中间件里加上权限判断,管理员才能执行
 class UserActionBarController extends Controller
 {
     public function AddUserActionBar(Request $request)
     {
-        $owner_type = $request->input("owner_type");
-        $owner_id = $request->input("owner_id");
-        $barId = $request->input("barId");
+        $validator = Validator::make($request->all(), [
+            "ownerType" => "required|max:32",
+            "ownerId" => "required|integer|min:1",
+            "barId" => "required|integer|min:1"
+        ]);
+        if ($validator->fails()) {
+            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+        }
+
+        $ownerType = $request->input("ownerType");
+        $ownerId = (int)$request->input("ownerId");
+        $barId = (int)$request->input("barId");
 
         $userActionBar = new Models\UserActionBar();
-        $userActionBar->AddUserActionBar($owner_type, $owner_id, $barId);
-        return "success";
+        $result = $userActionBar->AddUserActionBar($ownerType, $ownerId, $barId);
+        if ($result["code"] == 0) {
+            return $this->success($result["data"]);
+        }
+        return $this->fail($result["code"], $this->error[$result["code"]]);
     }
 
-    public function RemoveActionBar(Request $request)
+    public function RemoveUserActionBar(Request $request)
     {
-        $owner_type = $request->input("owner_type");
-        $owner_id = $request->input("owner_id");
-        $barId = $request->input("barId");
+        $validator = Validator::make($request->all(), [
+            "id" => "required|integer|min:1",
+//            "ownerType" => "required|max:32",
+//            "ownerId" => "required|integer|min:1",
+//            "barId" => "required|integer|min:1"
+        ]);
+        if ($validator->fails()) {
+            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+        }
+
+        $id = (int)$request->input("id");
+//        $ownerType = $request->input("ownerType");
+//        $ownerId = $request->input("ownerId");
+//        $barId = $request->input("barId");
 
         $userActionBar = new Models\UserActionBar();
-        $userActionBar->RemoveUserActionBar($owner_type, $owner_id, $barId);
-        return "success";
+        $code = $userActionBar->RemoveUserActionBar($id);
+        return $this->response($code, $this->error[$code]);
     }
 }

+ 43 - 12
app/Http/Controllers/UserController.php

@@ -13,8 +13,12 @@ class UserController extends Controller
     {
 
         $validator = Validator::make($request->all(), [
-            "username" => "required|between:1,3",
+            "username" => "required|between:1,32",
             "password" => "required|between:8,16",
+            "nickname" => "max:32",
+            "icon" => "max:255",
+            "tel" => "max:32",
+            "email" => "max:64",
         ]);
         if ($validator->fails()) {
             return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
@@ -30,15 +34,22 @@ class UserController extends Controller
 
         $user = new Models\User();
         $result = $user->CreateUser($params);
-        return $this->success($result);
+        if ($result["code"] == 0) {
+            return $this->success($result["data"]);
+        }
+        return $this->fail($result["code"], $this->error[$result["code"]]);
     }
 
     public function ModifyUser(Request $request)
     {
-        $validator = Validator::make($request->all(),[
-            "id" => "required|gt:0"
+        $validator = Validator::make($request->all(), [
+            "id" => "required|integer|min:1",
+            "nickname" => "max:32",
+            "icon" => "max:255",
+            "tel" => "max:32",
+            "email" => "max:64",
         ]);
-        if ($validator->fails()){
+        if ($validator->fails()) {
             return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
         }
 
@@ -52,31 +63,51 @@ class UserController extends Controller
         $params["email"] = $request->input("email");
 
         $user = new Models\User();
-        $result = $user->ModifyUser($params);
-        if ($result["code"] == 0){
+        $code = $user->ModifyUser($params);
+        if ($code == 0) {
             return $this->success("success");
-        } else{
-            $code = $result["code"];
+        } else {
             return $this->fail($code, $this->error[$code]);
         }
     }
 
     public function DeleteUser(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());
+        }
+
         $uid = $request->input("id");
 
         $user = new Models\User();
-        $result = $user->DeleteUser($uid);
-        return $result;
+        $code = $user->DeleteUser($uid);
+        if ($code == 0) {
+            return $this->success("success");
+        }
+        return $this->fail($code, $this->error[$code]);
     }
 
+
+    // todo, 列出用户要根据业务在中间件添加权限拦截
     public function ListUser(Request $request)
     {
+        $validator = Validator::make($request->all(), [
+            "page" => "integer|min:1",
+            "pageCount" => "integer|min:1",
+        ]);
+        if ($validator->fails()) {
+            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+        }
+
         $page = $request->input("page");
         $pageCount = $request->input("pageCount");
+        $keyword = $request->input("keyword");
 
         $user = new Models\User();
-        $result = $user->ListUser($page, $pageCount);
+        $result = $user->ListUser($page, $pageCount, $keyword);
         return $result;
     }
 }

+ 45 - 21
app/Models/Role.php

@@ -20,34 +20,51 @@ class Role extends Model
      * 创建一个用户的角色
      *
      * @param array $params
-     * @return string
+     * @return array
      */
     public function CreateRole(array $params)
     {
+        if ($params["description"] == null) {
+            $params["description"] = "";
+        }
         $this->name = $params["name"];
         $this->description = $params["description"];
         $this->status = "normal";
 
         if ($this->name == "") {
-            return "empty role name";
+            return ["code" => EMPTY_ROLE_NAME];
+        }
+        // check if has the same role name in system.
+        $role = $this->where("name", $this->name)->where("is_del", false)->first();
+        if ($role) {
+            return ["code" => ALREADY_EXIST_ROLE];
         }
+        //
         $this->save();
-        return "success";
+        $params["id"] = $this->getQueueableId();
+        return ["code" => SUCCESS, "data" => $params];
     }
 
+
     /**
-     * 修改用户的角色信息
+     * 更新用户角色的名字,描述等信息
      *
      * @param array $params
-     * @return string
+     * @return int
      */
     public function ModifyRole(array $params)
     {
         $update = [];
         $id = $params["id"];
         if ($id == "") {
-            return "empty role id";
+            return EMPTY_ROLE_ID;
         }
+        // check if role exist
+        $role = $this->where("id", $id)->where("is_del", false)->first();
+        if (!$role) {
+            return INVALID_ROLE_ID;
+        }
+
         if ($params["name"] != "") {
             $update["name"] = $params["name"];
         }
@@ -55,30 +72,30 @@ class Role extends Model
             $update["description"] = $params["description"];
         }
         if (count($update) == 0) {
-            return "nothing to update";
+            return NOTHING_UPDATE;
         }
-        $this->where("id", $id)
-            ->where("is_del", false)
-            ->update($update);
-        return "success";
+        $this->where("id", $id)->where("is_del", false)->update($update);
+        return SUCCESS;
     }
 
     /**
      * 删除一个用户角色
      *
      * @param array $params
-     * @return string
+     * @return int
      */
     public function DeleteRole(array $params)
     {
         $id = $params["id"];
         if ($id == "") {
-            return "empty role id";
+            return EMPTY_ROLE_ID;
         }
-        $this->where("id", $id)
-            ->where("is_del", false)
-            ->update(["is_del" => true]);
-        return "success";
+        $role = $this->where("id", $id)->where("is_del", false)->first();
+        if (!$role) {
+            return INVALID_ROLE_ID;
+        }
+        $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
+        return SUCCESS;
     }
 
     /**
@@ -91,10 +108,17 @@ class Role extends Model
     {
         $page = $params["page"];
         $pageCount = $params["pageCount"];
-        $data = $this->where("is_del", false)
-            ->orderBy("created_at", "asc")
-            ->paginate($pageCount, ["*"], "page", $page)
-            ->get();
+
+        if ($page < 1) {
+            $page = 1;
+        }
+        if ($pageCount > 15 || $pageCount < 1) {
+            $pageCount = 15;
+        }
+
+        $data = $this->select("name", "description")
+            ->where("is_del", false)->orderBy("created_at", "asc")
+            ->paginate($pageCount, ["*"], "page", $page);
         return $data;
     }
 

+ 31 - 19
app/Models/User.php

@@ -21,8 +21,11 @@ class User extends Model
         $this->tel = $params["tel"];
         $this->email = $params["email"];
 
-        if ($this->username == "" || $this->password == "") {
-            return "empty username or password";
+        if ($this->username == "") {
+            return ["code" => EMPTY_USER_NAME];
+        }
+        if ($this->password == "") {
+            return ["code" => EMPTY_USER_PASSWORD];
         }
         if ($this->nickname == "") {
             $this->nickname = $this->username;
@@ -31,7 +34,8 @@ class User extends Model
         // todo 这里需要对密码加密
         $this->save();
         unset($params["password"]);
-        return $params;
+        $params["id"] = $this->getQueueableId();
+        return ["code" => SUCCESS, "data" => $params];
     }
 
     public function ChangePassword($uid, $oldPwd, $newPwd)
@@ -46,6 +50,12 @@ class User extends Model
         if ($uid == "") {
             return "empty user id";
         }
+        // check if user exist
+        $user = $this->where("id", $uid)->where("is_del", false)->first();
+        if (!$user) {
+            return 110;
+        }
+
         if ($params["username"] != "") {
             $update["username"] = $params["username"];
         }
@@ -64,14 +74,8 @@ class User extends Model
         if (count($update) == 0) {
             return "nothing to update";
         }
-        $result = $this->where("id", $uid)
-            ->where("is_del", false)
-            ->update($update);
-        Log::debug("result is " . $result, ["aa" => $this->getGrammar()]);
-        return [
-            "code" => $result == false ? 0 : NO_DATA_IN_SYS_TO_UPDATE,
-            "message" => $result == false ? "success" : "invalid update",
-        ];
+        $this->where("id", $uid)->where("is_del", false)->update($update);
+        return 0;
     }
 
     public function DeleteUser($uid)
@@ -79,17 +83,25 @@ class User extends Model
         if ($uid == "") {
             return "empty user id";
         }
-        $result = $this->where("id", $uid)
-            ->where("is_del", false)
-            ->update(["is_del" => true]);
-        return $result > 0 ? "success" : "fail";
+        $user = $this->where("id", $uid)->where("is_del", false)->first();
+        if (!$user) {
+            return 110;
+        }
+        $this->where("id", $uid)->where("is_del", false)->update(["is_del" => true]);
+        return 0;
     }
 
-    public function ListUser($page, $pageCount)
+    public function ListUser($page, $pageCount, $keyword)
     {
-        $data = $this->where("is_del", false)
-            ->paginate($pageCount, ["*"], "page", $page)
-            ->get();
+        if ($page < 1) {
+            $page = 15;
+        }
+        if ($pageCount > 15 || $pageCount < 1) {
+            $pageCount = 15;
+        }
+        $data = $this->select("id", "username", "nickname", "icon", "tel", "email")
+            ->where("is_del", false)->orderBy("created_at", "asc")
+            ->paginate($pageCount, ["*"], "page", $page);
         return $data;
     }
 }

+ 40 - 40
app/Models/UserActionBar.php

@@ -3,65 +3,65 @@
 namespace App\Models;
 
 use Illuminate\Database\Eloquent\Model;
-use Illuminate\Support\Facades\DB;
-use Illuminate\Support\Facades\Log;
 
 class UserActionBar extends Model
 {
     protected $table = "user_action_bars";
+    protected $fillable = ["owner_type", "owner_id", "bar_id"];
     public $timestamps = false;
 
-    public function AddUserActionBar($owner_type, $owner_id, $barId)
+    public function AddUserActionBar($ownerType, $ownerId, $barId)
     {
-        if ($owner_type == "") {
-            return "empty user type";
+        if ($ownerType == "") {
+            return ["code" => EMPTY_OWNER_TYPE];
         }
-        if ($owner_id == "") {
-            return "empty user id";
+        if ($ownerId == "") {
+            return ["code" => EMPTY_OWNER_ID];
         }
         if ($barId == "") {
-            return "empty action bar id";
+            return ["code" => EMPTY_BAR_ID];
         }
 
-        //INSERT INTO demo_in(a,b,c) SELECT 123, 2, 4 FROM DUAL WHERE NOT EXISTS(SELECT c FROM demo_in WHERE c = 4);
-        $sql = sprintf("insert into user_action_bars(owner_type, owner_id, bar_id) 
-                                    select ?, ?, ? from temp_uab 
-                                    where not exists(
-                                    select id from user_action_bars
-                                    where owner_type = ? and owner_id = ? and bar_id = ? and is_del = false);");
+        // check owner and barId if exist in system.
+        $code = $this->checkOwner($ownerType, $ownerId);
+        if ($code != 0) {
+            return ["code" => $code];
+        }
+        $code = $this->checkActionBar($barId);
+        if ($code != 0) {
+            return ["code" => $code];
+        }
 
-        $result = DB::insert($sql, [$owner_type, $owner_id, $barId, $owner_type, $owner_id, $barId]);
-        Log::debug("AddUserActionBar " . $result);
-        return "success";
+        $result = $this->firstOrCreate(["owner_type" => $ownerType, "owner_id" => $ownerId, "bar_id" => $barId, "is_del" => false]);
+        unset($result["is_del"]);
+        unset($result["created_user_id"]);
+        unset($result["updated_user_id"]);
+        return ["code" => 0, "data" => $result];
     }
 
-    public function RemoveUserActionBar($owner_type, $owner_id, $barId)
+    private function checkActionBar($barId)
     {
-        if ($owner_type == "") {
-            return "empty user type";
-        }
-        if ($owner_id == "") {
-            return "empty user id";
+        return SUCCESS;
+    }
+
+    private function checkOwner($ownerType, $ownerId)
+    {
+        return SUCCESS;
+    }
+
+    public function RemoveUserActionBar($id)
+    {
+        if ($id < 1) {
+            return INVALID_U_A_ID;
         }
-        if ($barId == "") {
-            return "empty action bar id";
+        // check the data if exist in system.
+        $item = $this->where("id", $id)->where("is_del", false)->first();
+        if (!$item) {
+            return INVALID_U_A_ID;
         }
 
-        $data = $this->where("owner_type", $owner_type)
-            ->where("owner_id", $owner_id)
-            ->where("bar_id", $barId)
-            ->where("is_del", false)
-            ->first();
-        if (!$data) {
-            return "nothing to remove";
-        }
-        $this->is_del = true;
-        $this->where("owner_type", $owner_type)
-            ->where("owner_id", $owner_id)
-            ->where("bar_id", $barId)
-            ->where("is_del", false)
-            ->update(["is_del" => true]);
-        return "success";
+        $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
+        return SUCCESS;
     }
 
     /**

+ 39 - 3
config/errorcode.php

@@ -1,13 +1,49 @@
 <?php
 
+const SUCCESS = 0;
+const NOTHING_UPDATE = 1;
+
 const REQUEST_SUCCESS = 0;
 const REQUEST_PARAM_ERROR = 101;
 const REQUEST_DATABASE_ERROR = 102;
-const NO_DATA_IN_SYS_TO_UPDATE = 110;
+
+const EMPTY_USER_NAME = 110;
+const EMPTY_USER_ID = 111;
+const INVALID_USER_ID = 112;
+const EMPTY_USER_PASSWORD = 113;
+
+const EMPTY_ROLE_NAME = 220;
+const EMPTY_ROLE_ID = 221;
+const INVALID_ROLE_ID = 222;
+const ALREADY_EXIST_ROLE = 223;
+
+const EMPTY_OWNER_TYPE = 310;
+const EMPTY_OWNER_ID = 311;
+
+const EMPTY_BAR_ID = 410;
+
+const INVALID_U_A_ID = 510;
 
 return [
-    "0" =>"success",
+    "0" => "success",
+    "1" => "nothing to update",
+
     "101" => "params error",
     "102" => "database operation error",
-    "110" => "no data in system to update"
+
+    "111" => "empty user id",
+    "112" => "invalid user id",
+
+
+    "220" => "empty role name",
+    "221" => "empty role id",
+    "222" => "invalid role id",
+    "223" => "already exist role",
+
+    "310" => "empty owner type",
+    "311" => "empty owner id",
+
+    "410" => "empty action bar id",
+
+    "510" => "invalid user-action-bar id",
 ];

+ 14 - 10
routes/web.php

@@ -16,19 +16,23 @@
 Route::get('/', function () {
     return view('welcome');
 });
-//
-//Route::get("basic", function () {
-//    echo ("halo");
-//    return "halo";
-//});
-//
-//Route::get("a", function () {
-//    return "halo";
-//});
 
+//user
 Route::get("createUser", "UserController@CreateUser");
 Route::get("modifyUser", "UserController@ModifyUser");
-Route::get("delUser", "UserController@DelUser");
+Route::get("deleteUser", "UserController@DeleteUser");
+Route::get("listUser", "UserController@ListUser");
+
+// role
+Route::get("createRole", "RoleController@CreateRole");
+Route::get("modifyRole", "RoleController@ModifyRole");
+Route::get("deleteRole", "RoleController@DeleteRole");
+Route::get("listRole", "RoleController@ListRole");
+
+// user action bar
+Route::get("addUserActionBar", "UserActionBarController@AddUserActionBar");
+Route::get("removeUserActionBar", "UserActionBarController@RemoveUserActionBar");
+
 Route::get("grantPrivilege", "PermissionController@GrantPrivilege");
 
 // action bar