request->post(); $token = isset($post['token']) ? trim($post['token']) : ""; if($token==""){ return error_show(101,'token不能为空'); } $effetc = VerifyTokens($token); if(!empty($effetc) && $effetc['code']!=0){ return error_show($effetc['code'],$effetc['message']); } $condition=[]; isset($post['status'])&&$post['status']!=="" ? $condition[]=['a.status',"=",$post['status']]:""; isset($post['role_name'])&&$post['role_name']!=="" ? $condition[]=['a.role_name',"like","%".$post['role_name']."%"]:""; isset($post['level'])&&$post['level']!=="" ? $condition[]=["a.level","=",$post['level']]:""; $roleList = Db::name("role"); $count =$roleList->alias("a")->where($condition)->count(); $page = isset($post['page']) ? intval($post['page']) : 1; $size = isset($post['size']) ? intval($post['size']) : 10; $page >=ceil($count/$size) ? $page=ceil($count/$size) :""; $list=$roleList->alias("a")->leftJoin("cfp_role_action t","a.id=t.role_id")->field("a.*,t.action_conllect as action,t.action_data")->where($condition)->page($page,$size)->select(); return app_show(0,"获取成功",["list"=>$list,"count"=>$count]); } /** * @param role_name * @param level * @param action * @param action_data * @param role_name * @return \think\response\Json * @throws \think\exception\DbException */ public function RoleAdd(){ $post =$this->request->post(); $token = isset($post['token']) ? trim($post['token']) : ""; if($token==""){ return error_show(101,'token不能为空'); } $effetc = VerifyTokens($token); if(!empty($effetc) && $effetc['code']!=0){ return error_show($effetc['code'],$effetc['message']); } $rolename = isset($post['role_name']) ? trim($post['role_name']) : ""; if($rolename==""){ return error_show(1002,"角色名称不能为空"); } $isT=Db::name("role")->where(['role_name'=>$rolename])->find(); if($isT){ return error_show(1002,"角色名称已存在"); } $level = isset($post['level']) ? intval($post['level']) : ""; if($level==""){ return error_show(1003,"角色等级不能为空"); } $action = isset($post['action'])&&is_array($post['action']) ? implode(",",$post['action']) : ""; if($action==""){ return error_show(1004,"功能权限不能为空"); } $data = isset($post['action_data']) &&is_array($post['action_data'])?implode(",",$post['action_data']): ""; $private_data = isset($post['private_data']) &&is_array($post['private_data'])?implode(",",$post['private_data']): ""; // if($data==""){ // return error_show(1004,"字段权限不能为空"); // } Db::startTrans(); try { $list = [ "role_name"=>$rolename, "status"=>1, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), "level"=>$level, ]; $role= Db::name("role")->insert($list,true); if($role>0){ $role_action = [ "role_id"=>$role, "action_conllect"=>$action, "action_data"=>$data, "private_data"=>$private_data, "status"=>1, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), ]; $roleaction= Db::name("role_action")->insert($role_action,true); if($roleaction){ Db::commit(); return app_show(0,"新建成功"); }else{ Db::rollback(); return app_show(1005,"权限录入失败"); } }else{ Db::rollback(); return app_show(1006,"角色新建失败"); } }catch (\Exception $e){ Db::rollback(); return error_show(1008,$e->getMessage()); } } /** * @param roleid * @return \think\response\Json * @throws \think\exception\DbException */ public function RoleInfo(){ $post =$this->request->post(); $token = isset($post['token']) ? trim($post['token']) : ""; if($token==""){ return error_show(101,'token不能为空'); } $effetc = VerifyTokens($token); if(!empty($effetc) && $effetc['code']!=0){ return error_show($effetc['code'],$effetc['message']); } $roleid= isset($post['roleid']) ? trim($post['roleid']) : ""; if($roleid==""){ return error_show(1001,'roleid不能为空'); } $info = $list=Db::name("role")->alias("a")->leftJoin("cfp_role_action t","a.id=t.role_id")->field("a.*,t.action_conllect,t.action_data,t.private_data")->where("a.id","=",$roleid)->find(); if(!$info){ return error_show(1002,"未找到对应的数据"); } $info['action'] =explode(",",$info['action_conllect']); $info['action_data'] =$info['action_data']!=""?explode(",",$info['action_data']):""; $info['private_data'] =$info['private_data']!=""?explode(",",$info['private_data']) :""; return app_show(0,"获取成功",$info); } /** * @param role_name * @param level * @param action * @param action_data * @param roleid * @return \think\response\Json * @throws \think\exception\DbException */ public function RoleSave(){ $post =$this->request->post(); $token = isset($post['token']) ? trim($post['token']) : ""; if($token==""){ return error_show(101,'token不能为空'); } $effetc = VerifyTokens($token); if(!empty($effetc) && $effetc['code']!=0){ return error_show($effetc['code'],$effetc['message']); } $roleid= isset($post['roleid']) ? intval($post['roleid']) : ""; if($roleid==""){ return error_show(1001,'roleid不能为空'); } $info =Db::name("role")->where("id","=",$roleid)->find(); if(!$info){ return error_show(1002,"未找到对应的数据"); } $rolename = isset($post['role_name']) ? trim($post['role_name']) : ""; if($rolename==""){ return error_show(1002,"角色名称不能为空"); } $vers= Db::name("role")->where("id",'<>',$roleid)->where("role_name","=",$rolename)->find(); if($vers){ return error_show(1002,"角色名已存在"); } $level = isset($post['level']) ? intval($post['level']) : ""; if($level==""){ return error_show(1003,"角色等级不能为空"); } $action = isset($post['action'])&&is_array($post['action']) ? implode(",",$post['action']) : ""; if($action==""){ return error_show(1004,"功能权限不能为空"); } $data = isset($post['action_data']) &&is_array($post['action_data'])?implode(",",$post['action_data']): ""; $private_data = isset($post['private_data']) &&is_array($post['private_data'])?implode(",",$post['private_data']): ""; // if($data==""){ // return error_show(1004,"字段权限不能为空"); // } Db::startTrans(); try { $list = [ "role_name"=>$rolename, "level"=>$level, "updatetime"=>date("Y-m-d H:i:s"), 'id'=>$roleid ]; $role= Db::name("role")->save($list); if($role){ $role_action = [ "role_id"=>$roleid, "action_conllect"=>$action, "action_data"=>$data, "private_data"=>$private_data, "updatetime"=>date("Y-m-d H:i:s") ]; $roleaction= Db::name("role_action")->where("role_id","=",$roleid)->update($role_action); if($roleaction){ Db::commit(); return app_show(0,"更新成功"); }else{ Db::rollback(); return app_show(1005,"权限更新失败"); } }else{ Db::rollback(); return app_show(1006,"角色更新失败"); } }catch (\Exception $e){ Db::rollback(); return error_show(1008,$e->getMessage()); } } /** * @return \think\response\Json * @throws \think\exception\DbException */ public function RoleStatus(){ $post =$this->request->post(); $token = isset($post['token']) ? trim($post['token']) : ""; if($token==""){ return error_show(101,'token不能为空'); } $effetc = VerifyTokens($token); if(!empty($effetc) && $effetc['code']!=0){ return error_show($effetc['code'],$effetc['message']); } $roleid= isset($post['roleid']) ? intval($post['roleid']) : ""; if($roleid==""){ return error_show(1001,'roleid不能为空'); } $status = isset($post['status']) ? intval($post['status']) : ""; if($status===""){ return error_show(1001,'status不能为空'); } if(!in_array($status,[0,1])){ return error_show(1001,'status参数非法'); } Db::startTrans(); try { // $re = PasRoleList::update(['status'=>$status,'update'=>date("Y-m-d H:i:s")],['id'=>$roleid]); $dat=[ 'status'=>$status,'updatetime'=>date("Y-m-d H:i:s"), 'id'=>$roleid ]; $re = Db::name("role")->save($dat); if($re){ $dat2=[ 'status'=>$status,'updatetime'=>date("Y-m-d H:i:s") ]; $action = Db::name("role_action")->where("role_id","=",$roleid)->update($dat2); if($action){ Db::commit(); return app_show(0,"状态更新成功"); } } Db::rollback(); return error_show(1003,"状态更新失败"); }catch (\Exception $e){ Db::rollback(); return error_show(1002,$e->getMessage()); } } /** * @return \think\response\Json * @throws \think\exception\DbException */ public function RoleAll(){ $post =$this->request->post(); $token = isset($post['token']) ? trim($post['token']) : ""; if($token==""){ return error_show(101,'token不能为空'); } $effetc = VerifyTokens($token); if(!empty($effetc) && $effetc['code']!=0){ return error_show($effetc['code'],$effetc['message']); } $list =Db::name("role")->select(); return app_show(0,"获取成功",$list); } }