123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace App\Http\Controllers;
- use App\Models;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Validator;
- class RoleController extends Controller
- {
- /**
- * 创建一个新的角色
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- 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);
- if ($result["code"] == 0) {
- return $this->success($result["data"]);
- }
- return $this->fail($result["code"], $this->error[$result["code"]]);;
- }
- /**
- * 修改某个角色的信息
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- 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);
- $code = $result["code"];
- if ($code == 0) {
- return $this->success($result["data"]);
- }
- return $this->fail($code, $this->error[$code]);
- }
- /**
- * 删除某个角色
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- 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();
- $code = $role->DeleteRole($params);
- if ($code == 0) {
- return $this->success("success");
- }
- return $this->fail($code, $this->error[$code]);
- }
- /**
- * 列出所有的角色信息
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|mixed
- */
- 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"] = (int)$request->input("page");
- $params["pageCount"] = (int)$request->input("pageCount");
- $params["keyword"] = $request->input("keyword");
- $role = new Models\Role();
- $roleData = $role->ListRole($params);
- return $this->success($roleData);
- }
- /**
- * 加载当前用户的角色信息
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function LoadRole(Request $request)
- {
- $uid = (int)$request->input("currentUid");
- if ($uid < 1) {
- $this->fail(INVALID_USER_ID, $this->error[INVALID_USER_ID]);
- }
- $role = new Models\UserRole();
- $data = $role->LoadRole($uid);
- if ($data["code"] == 0){
- return $this->success($data["data"]);
- }
- return $this->fail($data["code"], $this->error[$data["code"]]);
- }
- /**
- * 管理员加载用户的角色信息
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function AdminLoadRole(Request $request)
- {
- $uid = (int)$request->input("userId");
- if ($uid < 1) {
- $this->fail(INVALID_USER_ID, $this->error[INVALID_USER_ID]);
- }
- $role = new Models\UserRole();
- $data = $role->LoadRole($uid);
- if ($data["code"] == 0){
- return $this->success($data["data"]);
- }
- return $this->fail($data["code"], $this->error[$data["code"]]);
- }
- /**
- * 列出角色下的用户
- *
- * @param Request $request
- * @return array|\Illuminate\Http\JsonResponse
- */
- public function LoadRoleUsers(Request $request)
- {
- $validator = Validator::make($request->all(), [
- "keyword" => "max: 255",
- "page" => "integer|min:1",
- "pageCount" => "integer|min:1",
- "ukeyword" => "max: 255",
- "upage" => "integer|min:1",
- "upageCount" => "integer|min:1"
- ]);
- if ($validator->fails()) {
- return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
- }
- $keyword = $request->input("keyword");
- $page = (int)$request->input("page");
- $pageCount = (int)$request->input("pageCount");
- $ukeyword = $request->input("ukeyword");
- $upage = (int)$request->input("page");
- $upageCount = (int)$request->input("pageCount");
- $role = new Models\Role();
- $data = $role->LoadRoleUsers($keyword, $page, $pageCount, $ukeyword, $upage, $upageCount);
- return $this->success($data);
- }
- }
|