toArray(); $list = getTreeList($list); return $this->success($list); } // 添加权限 public function store (Request $request) { $validator = Validator::make($request->all(),[ 'name' => 'required', 'pid' => 'required', 'controller' => 'required', 'action' => 'required', ],[ 'name.required' => '权限名称不能为空', 'pid.required' => '上级权限不能为空', 'controller.required' => '访问控制器不能为空', 'action.required' => '访问方法不能为空', ]); if ($validator->fails()){ return $this->error(ERROR_CODE_PARAM_INVALID,$validator->errors()->first()); } $arr = $request->all(); if ($arr['pid'] == 0){ $arr['route_uri'] = $arr['controller']; }else{ $arr['route_uri'] = strtolower(trim($arr['controller'])).'/'.strtolower(trim($arr['action'])); } $arr['created_at'] = date('y-m-d H:i:s',time()); $count = Node::where('route_uri',$arr['route_uri'])->count(); if ($count && $arr['pid']!=0) { return $this->error(ERROR_CODE_INTERNAL_ERROR,'已有该权限的url地址了'); } $res = Node::insert($arr); if (! $res) { return $this->error(ERROR_CODE_INTERNAL_ERROR,'系统繁忙,稍后再试'); } return $this->success([]); } //修改权限 public function update ($id,Request $request) { if (! is_numeric($id)) { return $this->error(ERROR_CODE_PARAM_INVALID,'参数格式不对'); } $validator = Validator::make($request->all(),[ 'name' => 'required', 'pid' => 'required', 'controller' => 'required', 'action' => 'required', ],[ 'name.required' => '权限名称不能为空', 'pid.required' => '上级权限不能为空', 'controller.required' => '访问控制器不能为空', 'action.required' => '访问方法不能为空', ]); if ($validator->fails()){ return $this->error(ERROR_CODE_PARAM_INVALID,$validator->errors()->first()); } $arr = $request->all(); if ($arr['pid'] == 0){ $arr['route_uri'] = $arr['controller']; }else{ $arr['route_uri'] = strtolower(trim($arr['controller'])).'/'.strtolower(trim($arr['action'])); } $count = Node::where('route_uri',$arr['route_uri'])->count(); if ($count>1 && $arr['pid']!=0) { return $this->error(ERROR_CODE_INTERNAL_ERROR,'已有该权限的url地址了'); } $res = Node::where('id',$id)->update($arr); if (! $res) { return $this->error(ERROR_CODE_INTERNAL_ERROR,'系统繁忙,稍后再试'); } return $this->success([]); } //删除权限 public function destroy ($id) { if (! is_numeric($id)) { return $this->error(ERROR_CODE_PARAM_INVALID,'参数格式不对'); } try { $res = Node::destroy($id); if (! $res) { throw new \Exception('系统繁忙,稍后再试',ERROR_CODE_INTERNAL_ERROR); } $count = Node::where('pid',$id)->count(); if (! $count) { return $this->success([]); } $ref = Node::where('pid',$id)->delete(); if (! $ref) { throw new \Exception('系统繁忙,稍后再试',ERROR_CODE_INTERNAL_ERROR); } return $this->success([]); } catch (\Exception $e) { return $this->error($e->getCode(),$e->getMessage()); } } }