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.
116 lines
4.9 KiB
PHP
116 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Controllers\AdminController;
|
|
use App\Services\AnnouncementService;
|
|
use App\Services\Cloud\CloudAccess;
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
class AnnController extends AdminController
|
|
{
|
|
public function index($request, $response, $args)
|
|
{
|
|
try {
|
|
$actor = CloudAccess::actor(false);
|
|
AnnouncementService::authorize($actor);
|
|
} catch (CloudError $exception) {
|
|
return $response->withStatus($exception->httpStatus)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
$page = null;
|
|
$error = '';
|
|
$status = 200;
|
|
try {
|
|
$page = AnnouncementService::page($actor, $request->getQueryParams());
|
|
} catch (CloudError $exception) {
|
|
$error = $exception->getMessage();
|
|
$status = $exception->httpStatus;
|
|
} catch (\Throwable $exception) {
|
|
error_log('Announcement list failed: ' . get_class($exception));
|
|
$error = '公告暂时无法读取,请确认已执行 cloudInit 后重试。';
|
|
$status = 503;
|
|
}
|
|
return $this->render($response, 'admin/announcement/index.tpl', [
|
|
'announcements_page' => $page, 'announcements_error' => $error,
|
|
'announcement_can_manage' => (bool) ($actor['is_super_admin'] && $actor['can_manage']),
|
|
], $status);
|
|
}
|
|
|
|
public function create($request, $response, $args)
|
|
{
|
|
try {
|
|
AnnouncementService::authorize(CloudAccess::actor(false), true);
|
|
} catch (CloudError $exception) {
|
|
return $response->withStatus($exception->httpStatus)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
return $this->render($response, 'admin/announcement/create.tpl', [
|
|
'announcement' => ['id' => '', 'title' => '', 'type' => 'notice', 'type_label' => '通知',
|
|
'body' => '', 'status' => 'published', 'published_at' => null, 'updated_at' => ''],
|
|
'announcement_can_manage' => true,
|
|
]);
|
|
}
|
|
|
|
public function add($request, $response, $args)
|
|
{
|
|
return $this->write($request, $response, null, false);
|
|
}
|
|
|
|
public function edit($request, $response, $args)
|
|
{
|
|
try {
|
|
$actor = CloudAccess::actor(false);
|
|
AnnouncementService::authorize($actor, true);
|
|
$announcement = AnnouncementService::detail($actor, (string) ($args['id'] ?? ''));
|
|
} catch (CloudError $exception) {
|
|
return $response->withStatus($exception->httpStatus)->withHeader('Cache-Control', 'private, no-store');
|
|
} catch (\Throwable $exception) {
|
|
error_log('Announcement detail failed: ' . get_class($exception));
|
|
return $response->withStatus(503)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
return $this->render($response, 'admin/announcement/edit.tpl', [
|
|
'announcement' => $announcement, 'announcement_can_manage' => true,
|
|
]);
|
|
}
|
|
|
|
public function update($request, $response, $args)
|
|
{
|
|
return $this->write($request, $response, (string) ($args['id'] ?? ''), false);
|
|
}
|
|
|
|
public function status($request, $response, $args)
|
|
{
|
|
return $this->write($request, $response, (string) ($args['id'] ?? ''), true);
|
|
}
|
|
|
|
private function write($request, $response, ?string $id, bool $statusOnly)
|
|
{
|
|
try {
|
|
$actor = CloudAccess::actor(false);
|
|
AnnouncementService::authorize($actor, true);
|
|
$input = $request->getParsedBody();
|
|
if (!is_array($input)) {
|
|
throw new CloudError('公告请求格式不正确', 400);
|
|
}
|
|
$announcement = $statusOnly
|
|
? AnnouncementService::setStatus($actor, $id, $input)
|
|
: AnnouncementService::save($actor, $input, $id);
|
|
$message = $statusOnly ? ($announcement['status'] === 'published' ? '公告已发布' : '公告已撤下') : '公告已保存';
|
|
return $response->withJson(['ret' => 1, 'msg' => $message, 'announcement' => $announcement])
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
} catch (CloudError $exception) {
|
|
return $response->withJson(['ret' => 0, 'msg' => $exception->getMessage()], $exception->httpStatus)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
} catch (\Throwable $exception) {
|
|
error_log('Announcement write failed: ' . get_class($exception));
|
|
return $response->withJson(['ret' => 0, 'msg' => '公告保存失败,请稍后重试。'], 500)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
}
|
|
|
|
private function render($response, string $template, array $data, int $status = 200)
|
|
{
|
|
$response->getBody()->write($this->view()->assign($data)->fetch($template));
|
|
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
}
|