You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Controllers/Admin/LegalController.php

119 lines
4.5 KiB
PHP

<?php
namespace App\Controllers\Admin;
use App\Controllers\AdminController;
use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudError;
use App\Services\Config;
use App\Services\LegalDocumentService;
final class LegalController extends AdminController
{
public function index($request, $response, $args)
{
try {
$actor = CloudAccess::actor(false);
} catch (CloudError $exception) {
return $response->withStatus($exception->httpStatus)
->withHeader('Cache-Control', 'private, no-store');
}
$query = $request->getQueryParams();
$slug = $query['document'] ?? 'terms';
$locale = $query['locale'] ?? 'zh-CN';
$state = null;
$error = '';
$status = 200;
try {
$state = LegalDocumentService::editorState($actor, $slug, $locale);
} catch (CloudError $exception) {
$error = $exception->getMessage();
$status = $exception->httpStatus;
} catch (\Throwable $exception) {
error_log('Legal document editor failed: ' . get_class($exception));
$error = '法律页面暂时无法读取,请检查数据库升级状态。';
$status = 503;
}
$smarty = $this->view()->assign([
'legal_state' => $state,
'legal_error' => $error,
'legal_can_manage' => (bool) ($actor['is_super_admin'] && $actor['can_manage']),
'legal_selected_slug' => is_string($slug) ? $slug : 'terms',
'legal_selected_locale' => is_string($locale) ? $locale : 'zh-CN',
]);
$response->getBody()->write($smarty->fetch('admin/legal.tpl'));
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
}
public function draft($request, $response, $args)
{
return $this->write($request, $response, $args, false);
}
public function publish($request, $response, $args)
{
return $this->write($request, $response, $args, true);
}
public function preview($request, $response, $args)
{
try {
$actor = CloudAccess::actor(false);
$input = $this->input($request);
$document = LegalDocumentService::preview(
$actor,
(string) ($args['slug'] ?? ''),
$input,
(string) Config::get('appName')
);
return $response->withJson(['ret' => 1, 'document' => $document])
->withHeader('Cache-Control', 'private, no-store');
} catch (CloudError $exception) {
return $this->error($response, $exception);
} catch (\Throwable $exception) {
error_log('Legal document preview failed: ' . get_class($exception));
return $response->withJson(['ret' => 0, 'msg' => '预览生成失败,请稍后重试。'], 500)
->withHeader('Cache-Control', 'private, no-store');
}
}
private function write($request, $response, array $args, bool $publish)
{
try {
$actor = CloudAccess::actor(false);
$input = $this->input($request);
$state = $publish
? LegalDocumentService::publish($actor, (string) ($args['slug'] ?? ''), $input)
: LegalDocumentService::saveDraft($actor, (string) ($args['slug'] ?? ''), $input);
return $response->withJson([
'ret' => 1,
'msg' => $publish ? '法律页面已发布' : '草稿已保存',
'state' => $state,
])->withHeader('Cache-Control', 'private, no-store');
} catch (CloudError $exception) {
return $this->error($response, $exception);
} catch (\Throwable $exception) {
error_log('Legal document write failed: ' . get_class($exception));
return $response->withJson(['ret' => 0, 'msg' => '法律页面保存失败,请稍后重试。'], 500)
->withHeader('Cache-Control', 'private, no-store');
}
}
private function input($request): array
{
$input = $request->getParsedBody();
if (!is_array($input)) {
throw new CloudError('请求格式不正确', 400);
}
return $input;
}
private function error($response, CloudError $exception)
{
return $response->withJson(['ret' => 0, 'msg' => $exception->getMessage()], $exception->httpStatus)
->withHeader('Cache-Control', 'private, no-store');
}
}