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']); } } /** * @param status * @param role_name * @param level * @param page * @param size * @return \think\response\Json * @throws \think\exception\DbException */ public function RoleList(){ $post =$this->request->post(); $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['item'])&&$post['item']!=="" ? $condition[]=["a.item","=",$post['item']]:""; isset($post['createrId'])&&$post['createrId']!=="" ? $condition[]=["a.createrId","=",$post['createrId']]:""; $roleList = Db::name("role"); $count =$roleList->alias("a")->where($condition)->count(); $page = isset($post['page'])&& $post['page']!=''? intval($post['page']) : 1; $size = isset($post['size'])&& $post['size']!=''? intval($post['size']) : 10; $page >=ceil($count/$size) ? $page=ceil($count/$size) :""; $list=$roleList->alias("a")->leftJoin("role_action t","a.id=t.role_id")->leftJoin("company_item i","a .item=i.id")->field("a.*,i.name as item_name,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']); } $creater= GetUserInfo($post['token']); if(isset($creater['code'])&&$creater['code']==0){ $uid= isset($userinfo['data']['id']) ? $userinfo['data']['id']: "0"; $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: ""; }else{ $username = ''; $uid= 0; } $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,"角色名称已存在"); } $role_code= isset($post['role_code'])&&$post['role_code']!=''?trim($post['role_code']) :""; // if($role_code==""){ // return error_show(1002,"参数role_code 不能为空"); // } // $isCode=Db::name("role")->where(['role_code'=>$role_code])->find(); // if($isCode){ // return error_show(1002,"角色编号已存在"); // } // $item = isset($post['item']) ? trim($post['item']) : ""; // if($item==""){ // return error_show(1003,"参数item 不能为空"); // } // $iteminfo = Db::name('company_item')->where('id',"=",$item)->find(); // if(empty($iteminfo) || $iteminfo['is_del']==1){ // return error_show(1003,"该部门不存在"); // } $work_company = isset($post['work_company']) && $post['work_company'] !==""? intval($post['work_company']):""; // if($work_company==""){ // return error_show(1002,"参数work_company不能为空"); // } $level = isset($post['level']) ? intval($post['level']) : 0; $remark = isset($post['remark']) ? trim($post['remark']) : ''; // 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']): ""; $private_field = isset($post['private_field']) &&is_array($post['private_field'])?implode(",", $post['private_field']): ""; // if($data==""){ // return error_show(1004,"字段权限不能为空"); // } Db::startTrans(); try { $list = [ "role_name"=>$rolename, "role_code"=>$role_code, "work_company"=>$work_company, "status"=>1, 'creater'=>$username, 'createrId'=>$uid, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), "level"=>$level, "desc"=>$remark, "item"=>"", ]; $role= Db::name("role")->insert($list,true); $st = ["order_code"=>$role_code,"status"=>1,"action_remark"=>'',"action_type"=>"create"]; ActionLog::logAdd($token,$st,"role",1,$st); if($role>0){ $role_action = [ "role_id"=>$role, "action_conllect"=>$action, "action_data"=>$data, "private_data"=>$private_data, "private_field"=>$private_field, "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){ $sty = ["order_code"=>$role,"status"=>1,"action_remark"=>'',"action_type"=>"create"]; ActionLog::logAdd($token,$sty,"role_action",1,$sty); Db::commit(); return app_show(0,"新建成功"); }else{ Db::rollback(); return error_show(1005,"权限录入失败"); } }else{ Db::rollback(); return error_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(); $roleid= isset($post['roleid']) ? trim($post['roleid']) : ""; if($roleid==""){ return error_show(1001,'roleid不能为空'); } $info = $list=Db::name("role")->alias("a")->leftJoin("role_action t","a.id=t.role_id")->field("a.*,t.action_conllect,t.action_data,t.private_data,t.private_field")->where("a.id","=",$roleid)->find(); if(!$info){ return error_show(1002,"未找到对应的数据"); } $info['item_name'] =''; if($info['item']!=''){ $iteminfo = Db::name('company_item')->where('id',"=",$info['item'])->find(); $info['item_name'] = isset($iteminfo['name'])? $iteminfo['name']:""; } $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']) :""; $info['private_field'] =$info['private_field']!=""?explode(",",$info['private_field']) :[]; 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,"角色名已存在"); } $role_code= isset($post['role_code'])&&$post['role_code']!=''?trim($post['role_code']) :""; // if($role_code==""){ // return error_show(1002,"参数role_code 不能为空"); // } // $isCode=Db::name("role")->where(['role_code'=>$role_code])->find(); // if($isCode){ // return error_show(1002,"角色编号已存在"); // } // $item = isset($post['item']) ? trim($post['item']) : ""; // if($item==""){ // return error_show(1003,"参数item 不能为空"); // } // $iteminfo = Db::name('company_item')->where('id',"=",$item)->find(); // if($iteminfo['is_del']==1){ // return error_show(1003,"该部门不存在"); // } $level = isset($post['level']) ? intval($post['level']) : 0; $remark = isset($post['remark']) ? trim($post['remark']) : ''; $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']): ""; $private_field = isset($post['private_field']) &&is_array($post['private_field'])?implode(",",$post['private_field']): ""; // if($data==""){ // return error_show(1004,"字段权限不能为空"); // } Db::startTrans(); try { $list = [ "role_name"=>$rolename, "level"=>$level, 'desc'=>$remark, 'role_code'=>$role_code, 'item'=>"", "updatetime"=>date("Y-m-d H:i:s"), 'id'=>$roleid ]; $role= Db::name("role")->save($list); $stm = ["order_code"=>"YS","status"=>0,"action_remark"=>'',"action_type"=>"edit"]; ActionLog::logAdd($token,$stm,"resign_info",0,$stm); if($role){ $role_action = [ "role_id"=>$roleid, "action_conllect"=>$action, "action_data"=>$data, "private_data"=>$private_data, "private_field"=>$private_field, "updatetime"=>date("Y-m-d H:i:s") ]; $roleaction= Db::name("role_action")->where("role_id","=",$roleid)->update($role_action); if($roleaction){ $stl = ["order_code"=>"YS","status"=>0,"action_remark"=>'',"action_type"=>"edit"]; ActionLog::logAdd($token,$stl,"role_action",0,$stl); 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){ $sti = ["order_code"=>$roleid,"status"=>$status,"action_remark"=>'',"action_type"=>"edit"]; ActionLog::logAdd($token,$sti,"role_action",$status,$sti); 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(); $list =Db::name("role")->select(); return app_show(0,"获取成功",$list); } //获取角色对应的流程权限id public function roleProcessInfo() { $roleid = $this->request->filter('trim')->post('roleid/d', 0); $res = Db::name("role") ->alias("a") ->leftJoin("role_process b", "a.id=b.role_id AND b.is_del=0") ->field("a.*,b.action_data") ->withAttr('action_data', function ($val) { return explode(',', $val); }) ->where("a.id", $roleid) ->findOrEmpty(); return app_show(0, '请求成功', $res); } //修改角色对应的流程权限 public function roleProcessSave() { $param = $this->request->filter('trim')->only(['token', 'roleid', 'action_data'], 'post'); $val = Validate::rule([ 'token' => 'require', 'roleid|角色ID' => 'require|number|gt:0', 'action_data|所选节点id' => 'require|array', ]); if (!$val->check($param)) return error_show(1005, $val->getError()); $user = GetUserInfo($param['token']); $uid = isset($user['data']['id']) ? $user['data']['id'] : 0; $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : ''; $date = date('Y-m-d H:i:s'); $res = Db::name('role_process') ->where(['is_del' => 0, 'role_id' => $param['roleid']]) ->field('id') ->findOrEmpty(); if (empty($res)) { $rs = Db::name('role_process') ->insert([ 'role_id' => $param['roleid'], 'action_data' => implode(',', $param['action_data']), 'is_del' => 0, 'createrid' => $uid, 'creater' => $uname, 'addtime' => $date, 'updaterid' => $uid, 'updater' => $uname, 'updatetime' => $date, ]); } else { $rs = Db::name('role_process') ->where('id', $res['id']) ->update([ 'action_data' => implode(',', $param['action_data']), 'updaterid' => $uid, 'updater' => $uname, 'updatetime' => $date ]); } return $rs ? app_show(0, '操作成功') : error_show(1005, '操作失败'); } //获取角色对应的流程权限详情 public function roleProcessDetail() { $roleid = $this->request->filter('trim')->post('roleid/d', 0); $action_data = Db::name("role_process") ->where('role_id', $roleid) ->value('action_data', []); $data = PModel::where(['is_del' => PModel::$is_del_normal, 'status' => PModel::$status_normal]) ->append(['child']) ->withAttr('child', function () { return []; }) ->column('id,process_name,process_type', 'id'); $action = APModel::where([ 'is_del' => APModel::$is_del_normal, 'status' => APModel::$status_normal, 'operation_type' => APModel::$operation_type_approval ])->whereIn('id', $action_data) ->field('id,process_id,order_process,status_name') ->cursor(); foreach ($action as $item) { if (isset($data[$item->process_id])) $data[$item->process_id]['child'][] = $item->toArray(); } return app_show(0, '请求成功', array_column($data, null, null)); } }