request->only(["status" => "", "role_name" => "", "relaComNo" => "", "companyName" => "", "companyNo" => "", "page" => 1, "size" => 10,'level'=>''], "post", "trim"); $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['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']}%"] : ""; isset($post['level']) && $post['level'] !== "" ? $condition[] = ["a.role_level", "=", $post['level']] : ""; $count = Db::name("role") ->alias("a") ->where($condition) ->count('a.id'); $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("role_action t", "a.id=t.role_id") ->field("a.*,t.action_conllect as action,t.action_data") ->order("a.addtime desc") ->where($condition) ->page($page, $size) ->select() ->toArray(); $userComon=\app\admin\common\User::getIns(); $supplier =$userComon->handle("getCodeAndName",["code"=>array_column($list,"companyNo")]); $supplierArr=$supplier['data']??[]; foreach ($list as &$value){ $value['companyName'] = $supplierArr[$value['companyNo']]??""; } 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", "action", "action_data" => "", "private_data" => "", "relaComNo" => "", 'is_allow_see_price', 'private_field','level','desc'=>''], "post"); $valid = Validate::rule([ "role_name|角色名称" => "require|max:255|min:2", "relaComNo|业务公司编号" => "requireIf:level,2|max:255", // "companyNo|业务公司编号" => "requireWithout:relaComNo|max:255", "action|角色权限" => "require|array", "is_allow_see_price|是否允许查看价格" => "require|in:0,1", 'private_field|关键字段权限' => 'require|array|max:10', "level|角色级别"=>"require|number|in:1,2,3", ]); if ($valid->check($post) == false) return error_show(1004, $valid->getError()); $where = [["role_name", "=", $post['role_name']]]; if ($post['relaComNo'] != "") $where[] = ["companyNo", "=", $post['relaComNo']]; $isT = Db::name("role")->field('id')->where($where)->find(); if ($isT) return error_show(1002, "角色名称已存在"); $action = isset($post['action']) && is_array($post['action']) ? $post['action'] : []; $data = isset($post['action_data']) && is_array($post['action_data']) ? $post['action_data'] :[]; $private_data = isset($post['private_data']) && is_array($post['private_data']) ? $post['private_data'] : ""; Db::startTrans(); try { $date = date("Y-m-d H:i:s"); $list = [ "role_name" => $post['role_name'], "companyNo" => $post['level']==2?$post['relaComNo']:'', "status" => 1, "addtime" => $date, "updatetime" => $date, 'role_level'=>$post['level'], 'is_allow_see_price' => $post['is_allow_see_price'], 'desc' => $post['desc'] ]; $role = Db::name("role")->insert($list, true); if ($role > 0) { $role_action = [ "role_id" => $role, "action_conllect" => json_encode($action), "action_data" => json_encode($data), "private_data" => json_encode($private_data), 'private_field' => is_array($post['private_field']) ? implode(',', $post['private_field']) : $post['private_field'], "status" => 1, "addtime" => $date, "updatetime" => $date, ]; $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("role_action t", "a.id=t.role_id") ->field("a.*,t.action_conllect,t.action_data,t.private_data,t.private_field") ->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" => "", "relaComNo" => "", "action" => "", "action_data" => "", "private_data" => "", 'is_allow_see_price', 'private_field', 'level', 'desc' => ''], "post"); $valid = Validate::rule([ "role_name|角色名称" => "require|max:255|min:2", // "relaComNo|业务公司编号" => "max:255", // "companyNo|业务公司编号" => "max:255", "relaComNo|业务公司编号" => "requireIf:level,2|max:255", "action|角色权限" => "require|array", "is_allow_see_price|是否允许查看价格" => "require|in:0,1", 'private_field|关键字段权限'=>'require|array|max:10', "level|角色级别"=>"require|number|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, "未找到对应的数据"); $tmp_where=[ ["id", '<>', $post['roleid']], ["role_name", "=", $post['role_name']], ]; if($post['level']==2) $tmp_where[]=['companyNo','=',$post['relaComNo']]; $vers = Db::name("role") ->field('id') ->where($tmp_where) ->find(); if ($vers) return error_show(1002, "角色名已存在"); $action = isset($post['action']) && is_array($post['action']) ? $post['action']: []; // if ($action == "") { // return error_show(1004, "功能权限不能为空"); // } $data = isset($post['action_data']) && is_array($post['action_data']) ?$post['action_data']: []; $private_data = isset($post['private_data']) && is_array($post['private_data']) ? $post['private_data']: []; Db::startTrans(); try { $list = [ "role_name" => $post['role_name'], "updatetime" => date("Y-m-d H:i:s"), 'id' => $post['roleid'], 'is_allow_see_price' => $post['is_allow_see_price'], 'role_level'=>$post['level'], "companyNo" => $post['level']==2?$post['relaComNo']:'', 'desc' => $post['desc'] ]; $role = Db::name("role")->save($list); if ($role) { $role_action = [ "role_id" => $post['roleid'], 'action_conllect' => json_encode($action), 'action_data' => json_encode($data), 'private_data' => json_encode($private_data), 'private_field'=>is_array($post['private_field'])?implode(',',$post['private_field']):$post['private_field'], "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" => "",'level'=>''], "post", "trim"); $where = []; if ($post['relaComNo'] != '') $where[] = ["companyNo", "=", $post['relaComNo']]; if ($post['companyNo'] != '') $where[] = ["companyNo", "=", $post['companyNo']]; if ($post['level'] != '') $where[] = ["role_level", "=", $post['level']]; $list = Db::name("role") ->field(true) ->where($where) ->select() ->toArray(); $companyNo = array_column($list,'companyNo'); $companyNames = get_headquarters_code_and_name($companyNo); foreach ($list as &$value){ $value['companyName'] = $companyNames[$value['companyNo']]??''; } 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 = $this->uid;//isset($user['data']['id']) ? $user['data']['id'] : 0; $uname = $this->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)); } }