ok(['project' => 'identity-admin', 'data_mode' => $mode, 'group_enabled' => $mode === 'shared' && GroupService::enabled(), 'source_login_url' => $mode === 'shared' ? SharedIdentityService::sourceLoginUrl() : '']); } // Select the provider before opening a database: shared mode never touches demo accounts. if ($mode === 'shared') { $needsBody = in_array($operation, ['login', 'password'], true) || in_array($operation, ['group.company.save', 'group.member.save', 'group.sso.authorize', 'group.sso.exchange', 'group.kefu.login', 'group.application.sync', 'group.application.access-update'], true) || preg_match('/^(admin|role|menu|dept)\.(add|edit|delete)$/D', $operation); $companyHeader = (string) request()->header('x-company-id', ''); $companyId = $companyHeader === '' ? null : Input::integer($companyHeader); return $this->ok((new SharedIdentityService())->dispatch($operation, (string) request()->header('authorization', ''), $needsBody ? $this->body($operation) : [], request()->get(), $companyId, (string) request()->server('REMOTE_ADDR', 'unknown'))); } if (str_starts_with($operation, 'group.')) { throw new ApiError(503, '集团功能需要配置现有医院身份源'); } $repository = new IdentityRepository(new Database()); $auth = new AuthService($repository); if ($operation === 'login') { return $this->ok($auth->login($this->body(), (string) request()->server('REMOTE_ADDR', 'unknown'))); } $context = $auth->authenticate((string) request()->header('authorization', '')); $admin = $context['admin']; if ($permission !== null) { $permissions = $auth->permissions($admin); if (!in_array('*', $permissions, true) && !in_array($permission, $permissions, true)) { throw new ApiError(403, '权限不足'); } } $result = match ($operation) { 'logout' => $auth->logout($context['token_hash']), 'myself' => $auth->myself($admin) + ['data_mode' => 'standalone', 'source_login_url' => '', 'requires_password_change' => false, 'requires_wecom_binding' => false], 'password' => $auth->password($admin, $this->body(), $context['token_hash']), 'options' => $this->options($repository), 'admin.lists' => (new AccountService($repository))->lists(request()->get()), 'role.lists' => (new RoleService($repository))->lists(request()->get()), 'menu.lists' => (new TreeService($repository))->lists('menu'), 'dept.lists' => (new TreeService($repository))->lists('dept'), default => $this->mutate($operation, $repository, $admin), }; return $this->ok($result); } private function options(IdentityRepository $repository): array { $roles = $repository->db->all('SELECT * FROM system_role ORDER BY sort DESC,id ASC'); return ['roles' => array_map($repository->publicRole(...), $roles), 'depts' => (new TreeService($repository))->lists('dept'), 'menus' => (new TreeService($repository))->lists('menu')]; } private function mutate(string $operation, IdentityRepository $repository, array $admin): array { [$kind, $action] = array_pad(explode('.', $operation, 2), 2, ''); if (!in_array($kind, ['admin', 'role', 'menu', 'dept'], true) || !in_array($action, ['add', 'edit', 'delete'], true)) { throw new ApiError(404, '接口不存在'); } $data = $this->body(); if ($kind === 'admin') { $service = new AccountService($repository); return $action === 'delete' ? $service->delete($data, $admin) : $service->save($action, $data, $admin); } if ($kind === 'role') { $service = new RoleService($repository); return $action === 'delete' ? $service->delete($data) : $service->save($action, $data); } $service = new TreeService($repository); return $action === 'delete' ? $service->delete($kind, $data) : $service->save($kind, $action, $data); } private function body(string $operation = ''): array { $type = strtolower(explode(';', (string) request()->header('content-type', ''))[0]); if ($type !== 'application/json') { throw new ApiError(400, '请使用 application/json 请求'); } $raw = request()->getInput(); if (strlen($raw) > ($operation === 'group.application.access-update' ? 262144 : 65536)) { throw new ApiError(422, '请求体过大'); } try { $value = json_decode($raw, false, 32, JSON_THROW_ON_ERROR); } catch (\JsonException $error) { throw new ApiError(400, 'JSON 格式错误'); } if (!$value instanceof \stdClass) { throw new ApiError(400, '请求体必须是JSON对象'); } if ($operation === 'group.application.access-update' && property_exists($value,'permissions') && $value->permissions !== null && !is_array($value->permissions)) { throw new ApiError(422, '客服权限必须为数组或null'); } return (array) json_decode($raw, true, 32, JSON_THROW_ON_ERROR); } private function ok(array $data): \think\Response { return json(['code' => 1, 'msg' => '', 'data' => $data]); } }