request->only(["status" => "", "role_name" => "", "relaComNo" => "", "companyName" => "", "companyNo" => "", "level" => "", "page" => 1, "size" => 10], "post", "trim"); $condition = [["a.level", "<>", 1]]; 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']] : ""; isset($post['relaComNo']) && $post['relaComNo'] !== "" ? $condition[] = ["a.companyNo", "=", $post['relaComNo']] : ""; isset($post['companyNo']) && $post['companyNo'] !== "" ? $condition[] = ["a.companyNo", "=", $post['companyNo']] : ""; isset($post['companyName']) && $post['companyName'] !== "" ? $condition[] = ["b.name", "like", "%{$post['companyName']}%"] : ""; $count = Db::name("role")->alias("a") ->leftJoin("supplier_info b", "a.companyNo=b.code") ->where($condition)->count(); $page = isset($post['page']) ? intval($post['page']) : 1; $size = isset($post['size']) ? intval($post['size']) : 10; $page >= ceil($count / $size) ? $page = (int)ceil($count / $size) : ""; $list = Db::name("role")->alias("a") ->leftJoin("supplier_info b", "a.companyNo=b.code") // ->leftJoin("role_action t","a.id=t.role_id") ->field("a.*,b.name as companyName") ->order("a.addtime desc") ->where($condition)->page($page, $size)->select(); return app_show(0, "获取成功", ["list" => $list, "count" => $count]); } /** 角色新建 * @role_name 角色名称 * @level 角色等级 * @action 功能权限 * @action_data 操作权限 * @private_data 私有权限 * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function roleAdd(){ $post=$this->request->only(["role_name"=>"","level"=>"","action"=>"","action_data"=>"","private_data"=>"","relaComNo"=>"","companyNo"=>""],"post"); $valid =Validate::rule([ "role_name|角色名称"=>"require|max:255|min:2", "level|角色级别"=>"require|number|in:1,2,3", "relaComNo|业务公司编号"=>"max:255", "companyNo|业务公司编号"=>"max:255", "action|角色权限"=>"require|array", ]); if($valid->check($post)==false)return error_show(1004,$valid->getError()); if ($post['relaComNo'] != "" && $post["level"] == 1) { return error_show(1002, "超管权限角色不可关联业务公司"); } if ($post['relaComNo'] != "" && $post["level"] == 3) { return error_show(1002, "供应商权限角色不可关联业务公司"); } if($this->level==1){ $companyNo =$post['companyNo']??""; }else{ $companyNo =$post['relaComNo']??""; } $where=[["role_name","=",$post['role_name']]]; if($companyNo!=""){ $where[]=["companyNo","=",$companyNo]; // $company =Db::name("supplier_info")->where(["code"=>$companyNo])->find(); } $isT=Db::name("role")->where($where)->find(); if($isT){ return error_show(1002,"角色名称已存在"); } $action = isset($post['action'])&&is_array($post['action']) ? json_encode($post['action']) : ""; $data = isset($post['action_data']) &&is_array($post['action_data'])?json_encode($post['action_data']): ""; $private_data = isset($post['private_data']) &&is_array($post['private_data'])?json_encode($post['private_data']): ""; Db::startTrans(); try { $list = [ "role_name"=>$post['role_name'], "companyNo"=>$companyNo, "status"=>1, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), "level"=>$post['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()); } } /**查询角色信息 * @roleid 角色id * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function roleInfo(){ $post=$this->post; $roleid= isset($post['roleid']) ? intval($post['roleid']) : ""; if($roleid==""){ return error_show(1001,'roleid不能为空'); } $info =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") ->json(['action_conllect',"action_data","private_data"]) ->where("a.id","=",$roleid)->find(); if(!$info){ return error_show(1002,"未找到对应的数据"); } $info['action'] = $info['action_conllect'] ; return app_show(0,"获取成功",$info); } /** 角色信息编辑 * @param role_name 角色名称 * @param level 等级 * @param action 功能权限 * @param action_data 操作权限 * @param roleid 角色id * @param private_data 私有权限 * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function roleSave(){ $post=$this->request->only(["roleid"=>"","role_name"=>"","level"=>"","relaComNo"=>"","companyNo"=>"","action"=>"","action_data"=>"","private_data"=>""],"post"); $valid=Validate::rule([ "role_name|角色名称"=>"require|max:255|min:2", "relaComNo|业务公司编号"=>"max:255", "companyNo|业务公司编号"=>"max:255", "action|角色权限"=>"require|array", "level|角色级别"=>"require|in:1,2,3", ]); if($valid->check($post)==false)return error_show(1004,$valid->getError()); $info =Db::name("role")->where("id","=",$post['roleid'])->find(); if(!$info){ return error_show(1002,"未找到对应的数据"); } if ($post['relaComNo'] != "" && $info["level"] == 1) { return error_show(1002, "超管权限角色不可关联业务公司"); } if ($post['relaComNo'] != "" && $info["level"] == 3) { return error_show(1002, "供应商权限角色不可关联业务公司"); } // if($this->level==1){ // $companyNo =$post['companyNo']??""; // }else{ // $companyNo =$post['relaComNo']??""; // } $vers= Db::name("role")->where([["id",'<>',$post['roleid']],["role_name","=",$post['role_name']], ["companyNo","=",$info['companyNo']]])->find(); if($vers){ return error_show(1002,"角色名已存在"); } $action = isset($post['action'])&&is_array($post['action']) ?json_encode($post['action']) : ""; if($action==""){ return error_show(1004,"功能权限不能为空"); } $data = isset($post['action_data']) &&is_array($post['action_data'])?json_encode($post['action_data']): ""; $private_data = isset($post['private_data']) &&is_array($post['private_data'])?json_encode($post['private_data']): ""; Db::startTrans(); try { $list = [ "role_name"=>$post['role_name'], "level"=>$post['level'], "updatetime"=>date("Y-m-d H:i:s"), 'id'=>$post['roleid'] ]; $role= Db::name("role")->save($list); if($role){ $role_action = [ "role_id"=>$post['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","=",$post['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()); } } /** * 角色状态更新 * @roleid 角色id * @status 角色状态 0 /1 * @return \think\response\Json * @throws \think\exception\DbException */ public function roleStatus(){ $post=$this->request->only(["roleid"=>"","status"=>""],"post","intval"); $valid=Validate::rule([ "roleid|角色id"=>"require|number|gt:0", "status|状态"=>"require|number|in:0,1" ]); if($valid->check($post)==false)return error_show(1004,$valid->getError()); Db::startTrans(); try { $dat=[ 'status'=>$post['status'],'updatetime'=>date("Y-m-d H:i:s"), 'id'=>$post['roleid'] ]; $re = Db::name("role")->save($dat); if($re){ $dat2=[ 'status'=>$post['status'],'updatetime'=>date("Y-m-d H:i:s") ]; $action = Db::name("role_action")->where("role_id","=",$post['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->only(["relaComNo"=>"","companyNo"=>""],"post","trim"); $where=[["a.level","<>",1]]; if(isset($post['relaComNo']) && $post['relaComNo']!='')$where[]=["companyNo","=",$post['relaComNo']]; if(isset($post['companyNo'])&& $post['companyNo']!='')$where[]=["companyNo","=",$post['companyNo']]; $list =Db::name("role")->alias("a") ->leftJoin("supplier_info b","a.companyNo=b.code") ->field("a.*,b.name as companyName") ->where($where)->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)); } }