OrgRoleModel = new OrgRoleModel; $this->orgModel = new OrgModel; } /** * @title: 统一创建查询条件 * @desc: * @param {*} * @return {*} * @Author: Rock * @Date: 2022-03-30 14:38:10 * @LastEditTime: Do not edit */ private function createWhere() { $data = $this->request->param(); $where = []; if (!empty($data['keyword'])) { $keyword = $data['keyword']; $where[] = ['name|code|desc', 'LIKE', "%$keyword%"]; } if (!empty($data['depart_id'])) { $where[] = ['depart_id', '=', $data['depart_id']]; } $org_id = !empty($data['org_id']) ? $data['org_id'] : (!$this->userinfo['is_developer'] ? $this->userinfo['org_id'] : 0); if (!empty($org_id)) { if (!empty($data['child'])) { $ids = OrgModel::getChildrenIds($org_id, true); $where[] = ['org_id', 'IN', $ids]; } else { $where[] = ['org_id', '=', $org_id]; } } if (!$this->userinfo['is_developer']) { $where[] = ['code', 'not in', ['DEVELOPER', 'SUPERADMIN']]; } if (!empty($data['status'])) { $where[] = ['status', '=', $data['status']]; } return $where; } /** * @title: 组织角色列表 * @param {int} {pageNo} {非必填,默认值为0} {页码,不传或传0则不分页} * @param {int} {pageSize} {非必填,默认值为10} {每页数量} * @param {int} {org_id} {非必填,自动获取} {组织id} * @param {int} {child} {非必填,0} {是否包含下级组织} * @param {int} {depart_id} {非必填} {部门id} * @param {string} {name} {非必填} {名称搜索} * @return array * @Author: wangkewei * @Date: 2021/5/18 9:57 */ public function getList($pageNo = 0, $pageSize = 10) { $where = $this->createWhere(); if (empty($pageNo)) { $list = $this->OrgRoleModel->where($where)->with(['org'])->select(); return res(1, "获取成功", $list, $where); } else { $list = $this->OrgRoleModel->where($where)->with(['org'])->paginate(['page' => $pageNo, 'list_rows' => $pageSize]); $list = FieldConverList($list); return pageRes(1, "获取成功", $list['total'], $list['data'], $where); } } /** * @title: 组织角色编辑 * @param array * @return array * @Author: wangkewei * @Date: 2021/5/18 10:00 */ public function doEdit() { $data = $this->request->param(); $check = validate(OrgRoleValid::class)->check($data); if (true !== $check) { return Res(2, $check); } // 管理员只允许有一个 if ($data['code'] == 'ADMIN') { $where = []; if (!empty($data['depart_id'])) { $where[] = ['depart_id', '=', $data['depart_id']]; } else { $where[] = ['org_id', '=', $data['org_id']]; } $hasAdmin = $this->OrgRoleModel->where($where)->where('code', $data['code'])->find(); if ($hasAdmin && !$hasAdmin->isEmpty() && !isset($data['role_id'])) { return res(2, "只允许存在一个管理员"); } } $data['status'] = !empty($data['status']) ? $data['status'] : 1; $res = $this->OrgRoleModel->replace()->save($data); $role_id = $this->OrgRoleModel->role_id; $info = $this->OrgRoleModel->where('role_id', $role_id)->with(['org'])->find(); slog(1, "编辑了" . $info->org->name . "的角色" . $info->name); return res(1, "保存成功"); } /** * @title: 组织角色删除 * @param {string} {ids} {必填} {多个id} * @return array * @Author: wangkewei * @Date: 2021/5/18 10:00 */ public function doDelete($ids = []) { if (empty($ids)) return res(2, '参数错误'); if (is_string($ids)) { $ids = explode(',', $ids); } elseif (is_int($ids)) { $ids = [$ids]; } $users = UserRole::where("role_id", "IN", $ids)->count(); if (!empty($users)) { return res(2, "角色已有用户绑定,无法删除"); } $this->OrgRoleModel->destroy(function ($query) use ($ids) { $query->where('role_id', 'IN', $ids); }); slog(1, "删除了组织角色"); return res(1, "删除成功"); } /** * @title: 变更状态 * @return array * @Author: wangkewei * @Date: 2021/5/18 10:01 */ public function changeStatus($ids = [], $status = 0) { if (empty($ids)) return res(2, "参数错误"); if (is_string($ids)) { $ids = explode(',', $ids); } elseif (is_int($ids)) { $ids = [$ids]; } // 变更角色状态 $where = []; $where[] = ['role_id', 'IN', $ids]; if (empty($status)) { $this->OrgRoleModel->where($where)->update(['status' => Db::raw('ABS(3 * `status` - 5)')]); } else { $this->OrgRoleModel->where($where)->update(['status' => $status]); } // 获取变更为禁用状态的角色ids $role_ids = $this->OrgRoleModel->where($where)->where('status', 2)->column('role_id'); slog(1, "批量改变了角色状态"); return res(1, "操作成功"); } /** * @title: 获取角色已有的权限 * @desc: 描述 * @param {int} {role_id} {} {角色ID} * @return {*} * @author: Rock * @method: POST * @Date: 2023-02-06 17:46:53 */ public function getAuthList(int $role_id = 0) { try { $roleInfo = OrgRoleModel::find($role_id); if (empty($roleInfo) || $roleInfo->isEmpty()) { return error('未找到此角色'); } $menu_ids = is_string($roleInfo->menu_ids) ? explode(',', $roleInfo->menu_ids) : $roleInfo->menu_ids; $request_ids = is_string($roleInfo->request_ids) ? explode(',', $roleInfo->request_ids) : $roleInfo->request_ids; $front_ids = is_string($roleInfo->front_ids) ? explode(',', $roleInfo->front_ids) : $roleInfo->front_ids; // 此角色的所有菜单权限 $allMenuList = Menu::where('menu_id', 'IN', $menu_ids)->with(['menurequest'])->select()->toArray(); $menuAuthList = []; foreach ($allMenuList as $menu) { $MenuItemRequestIds = array_column($menu['menurequest'], 'menu_request_id'); $selected = array_intersect($request_ids, $MenuItemRequestIds); // 当所有请求权限都勾选了,菜单才会被选中 if (count($selected) == count($MenuItemRequestIds)) { $menuAuthList[] = $menu['menu_id']; } } $menuAuthList = !empty($menuAuthList) ? array_filter($menuAuthList) : []; $request_ids = !empty($request_ids) ? array_filter($request_ids) : []; $front_ids = !empty($front_ids) ? array_filter($front_ids) : []; $request_ids = array_map('intval', $request_ids); $front_ids = array_map('intval', $front_ids); $menuAuthList = array_map('intval', $menuAuthList); return res(1, '获取成功', ['menuAuthList' => $menuAuthList, 'requestAuthList' => $request_ids, 'frontAuthList' => $front_ids]); } catch (\Exception $e) { return res(2, '获取失败', $e->getMessage(), $e->getTrace()); } } /** * @title: 获取角色所在组织的管理员角色权限(没有管理员则获取组织的权限) * @desc: 描述 * @param {int} {role_id} {} {角色ID} * @return {*} * @author: Rock * @method: POST * @Date: 2023-02-06 17:50:56 */ public function getAdminAuth(int $role_id = 0) { if ($this->userinfo['is_developer']) { $menuList = Menu::where('hidden',2)->with(['menurequest'])->order('menu_id asc')->select()->toArray(); $menuList = array2tree($menuList, 'pid', 'menu_id'); $frontList = Frontmenus::select(); } else { //当前角色 $roleInfo = OrgRoleModel::find($role_id); $where = []; $where[] = ['code', '=', 'ADMIN']; if ($roleInfo->depart_id) { $where[] = ['depart_id', '=', $roleInfo->depart_id]; } else { $where[] = ['org_id', '=', $roleInfo->org_id]; } // 管理员角色 $adminInfo = OrgRoleModel::where($where)->find(); if ($roleInfo->code == 'ADMIN' || empty($adminInfo) || $adminInfo->isEmpty()) { $orgInfo = OrgModel::where('org_id', $roleInfo->org_id)->find(); $menu_ids = $orgInfo->menu_ids; $request_ids = $orgInfo->request_ids; $front_ids = $orgInfo->front_ids; } else { $menu_ids = $adminInfo->menu_ids; $request_ids = $adminInfo->request_ids; $front_ids = $adminInfo->front_ids; } $parentList = Menu::where('menu_id', 'IN', $menu_ids)->column('parent_path'); $parentIds = explode(',', implode(',', $parentList)); $menuList = Menu::where('menu_id', 'IN', $menu_ids)->whereOr('menu_id', 'IN', $parentIds)->with(['menurequest' => function ($query) use ($request_ids) { $query->where('menu_request_id', 'IN', $request_ids); }])->order('menu_id asc')->select()->toArray(); $menuList = array2tree($menuList, 'pid', 'menu_id'); $frontList = Frontmenus::where('menu_id', 'IN', $front_ids)->select(); } return res(1, "获取成功", ['menuList' => $menuList, 'frontList' => $frontList]); } /** * @title: 设置角色权限 * @desc: 描述 * @param {int} {role_id} {} {角色ID} * @param {array} {menuAuthList} {} {菜单权限} * @param {array} {requestAuthList} {} {请求权限} * @param {array} {frontAuthList} {} {移动端权限} * @return {*} * @author: Rock * @method: POST * @Date: 2023-02-02 17:00:27 */ public function setAuth(int $role_id = 0, array $menuAuthList = [], array $requestAuthList = [], array $frontAuthList = []) { $menuAuthList = array_unique(array_filter($menuAuthList)); $requestAuthList = array_unique(array_filter($requestAuthList)); $frontAuthList = array_unique(array_filter($frontAuthList)); try { // 事务开始 OrgRoleModel::startTrans(); $roleInfo = OrgRoleModel::find($role_id); $requestMenu = Menurequest::where('menu_request_id', 'IN', $requestAuthList)->column('menu_id'); $menuAuthList = array_unique(array_merge($menuAuthList, $requestMenu)); // 更新类型权限 $roleInfo->menu_ids = $menuAuthList;//菜单权限 $roleInfo->request_ids = $requestAuthList;//请求权限 $roleInfo->front_ids = $frontAuthList;//小程序权限 $roleInfo->save(); OrgRoleModel::commit(); return res(1, '保存成功'); } catch (\Exception $e) { OrgRoleModel::rollback(); return res(2, '保存失败', $e->getMessage(), $e->getTrace()); } } }