service()->workspace( $request->getQueryParams() ); } catch (\Throwable $exception) { error_log('User copy administrator page failed: ' . get_class($exception)); $error = '用户界面译文暂时无法读取,请稍后重试。'; $status = 503; } $bootstrap = json_encode( $workspace, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR ); $smarty = $this->view()->assign([ 'user_copy_workspace' => $workspace, 'user_copy_error' => $error, 'user_copy_bootstrap_json' => $bootstrap, 'user_copy_can_manage' => AdminPermission::isSuperAdmin(), ]); $response->getBody()->write( $smarty->fetch('admin/user_copy.tpl') ); return $response->withStatus($status) ->withHeader('Cache-Control', 'private, no-store'); } public function entries($request, $response, $args) { try { $workspace = $this->service()->workspace( $request->getQueryParams() ); return $this->json($response, ['workspace' => $workspace]); } catch (UserCopyAdminException $exception) { return $this->failure( $response, $exception->getMessage(), $exception->httpStatus ); } catch (UserCopyCatalogException $exception) { return $this->failure($response, $exception->getMessage(), 422); } catch (\Throwable $exception) { error_log('User copy entries failed: ' . get_class($exception)); return $this->failure( $response, '用户界面译文暂时无法读取,请稍后重试。', 503 ); } } public function history($request, $response, $args) { try { $history = $this->service()->history( (string) ($request->getQueryParams()['locale'] ?? '') ); return $this->json($response, ['history' => $history]); } catch (UserCopyAdminException $exception) { return $this->failure( $response, $exception->getMessage(), $exception->httpStatus ); } catch (UserCopyCatalogException $exception) { return $this->failure($response, $exception->getMessage(), 422); } catch (\Throwable $exception) { error_log('User copy history failed: ' . get_class($exception)); return $this->failure($response, '发布历史暂时无法读取。', 503); } } public function draft($request, $response, $args) { return $this->write($request, $response, $args, 'draft'); } public function synchronize($request, $response, $args) { return $this->write($request, $response, $args, 'synchronize'); } public function publish($request, $response, $args) { return $this->write($request, $response, $args, 'publish'); } public function rollback($request, $response, $args) { return $this->write($request, $response, $args, 'rollback'); } private function write($request, $response, array $args, string $action) { if (!AdminPermission::isSuperAdmin()) { return $this->failure($response, '只有超级管理员可以修改译文。', 403); } $input = $request->getParsedBody(); if (!is_array($input)) { return $this->failure($response, '请求格式不正确。', 400); } try { $actor = $this->actor(); $service = $this->service(); if ($action === 'synchronize') { $result = $service->synchronize($actor); } elseif ($action === 'draft') { $result = $service->saveDraft( $actor, (string) ($args['copy_key'] ?? ''), (string) ($input['locale'] ?? ''), (string) ($input['text'] ?? ''), ($input['hidden'] ?? false) === true ? 'hidden' : 'visible', (int) ($input['draft_version'] ?? 0) ); } elseif ($action === 'publish') { $idempotencyKey = trim((string) $request->getHeaderLine( 'Idempotency-Key' )); if ($idempotencyKey === '') { return $this->failure( $response, '发布请求缺少幂等标识,请刷新页面后重试。', 400 ); } $result = $service->publish( $actor, (string) ($input['locale'] ?? ''), hash('sha256', $idempotencyKey) ); } else { $result = $service->rollback( $actor, (string) ($input['locale'] ?? ''), (string) ($args['publication_key'] ?? '') ); } return $this->json( $response, [$action === 'synchronize' ? 'synchronization' : 'entry' => $result] ); } catch (UserCopyAdminException $exception) { return $this->failure( $response, $exception->getMessage(), $exception->httpStatus ); } catch (UserCopyCatalogException $exception) { return $this->failure($response, $exception->getMessage(), 409); } catch (\Throwable $exception) { error_log('User copy write failed: ' . get_class($exception)); return $this->failure( $response, '译文操作失败,请刷新页面核对后再试。', 503 ); } } private function json($response, array $payload, int $status = 200) { return $response->withJson(['ret' => 1] + $payload, $status) ->withHeader('Cache-Control', 'private, no-store'); } private function failure($response, string $message, int $status) { return $response->withJson([ 'ret' => 0, 'msg' => $message, ], $status)->withHeader('Cache-Control', 'private, no-store'); } private function service(): UserCopyAdminService { return UserCopyAdminService::fromApplication(); } /** @return array{user_id:int,is_admin:bool,is_super_admin:bool,can_manage:bool} */ private function actor(): array { $user = Auth::getUser(); $super = AdminPermission::isSuperAdmin(); return [ 'user_id' => is_object($user) && isset($user->id) ? (int) $user->id : 0, 'is_admin' => is_object($user) && method_exists($user, 'isAdmin') ? (bool) $user->isAdmin() : false, 'is_super_admin' => $super, 'can_manage' => $super, ]; } }