157 lines
5.1 KiB
PHP
157 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace app\controller\admin;
|
|
|
|
use app\support\ContentImageService;
|
|
use app\support\ContentService;
|
|
use support\Request;
|
|
|
|
class ContentsController
|
|
{
|
|
public function bootstrap(Request $request)
|
|
{
|
|
$service = $this->service();
|
|
|
|
return api_success([
|
|
'home_config' => $service->getHomeConfig(),
|
|
'policy_config' => $service->getPolicyConfig(),
|
|
'meta_config' => $service->getMetaConfig(),
|
|
'help_articles' => $service->getHelpArticles(false),
|
|
]);
|
|
}
|
|
|
|
public function home(Request $request)
|
|
{
|
|
return api_success([
|
|
'home_config' => $this->service()->getHomeConfig(),
|
|
]);
|
|
}
|
|
|
|
public function saveHome(Request $request)
|
|
{
|
|
try {
|
|
$this->service()->saveHomeConfig((array)$request->input('home_config', []));
|
|
return api_success([
|
|
'home_config' => $this->service()->getHomeConfig(),
|
|
], '首页内容已保存');
|
|
} catch (\RuntimeException $e) {
|
|
return api_error($e->getMessage(), 422);
|
|
} catch (\Throwable $e) {
|
|
return api_error('首页内容保存失败', 500, [
|
|
'detail' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function uploadImage(Request $request)
|
|
{
|
|
try {
|
|
return api_success((new ContentImageService())->upload($request), '图片已上传');
|
|
} catch (\RuntimeException $e) {
|
|
return api_error($e->getMessage(), 422);
|
|
} catch (\Throwable $e) {
|
|
return api_error('图片上传失败', 500, [
|
|
'detail' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function helpArticles(Request $request)
|
|
{
|
|
return api_success([
|
|
'list' => $this->service()->getHelpArticles(false),
|
|
]);
|
|
}
|
|
|
|
public function policy(Request $request)
|
|
{
|
|
return api_success([
|
|
'policy_config' => $this->service()->getPolicyConfig(),
|
|
]);
|
|
}
|
|
|
|
public function savePolicy(Request $request)
|
|
{
|
|
try {
|
|
$this->service()->savePolicyConfig((array)$request->input('policy_config', []));
|
|
return api_success([
|
|
'policy_config' => $this->service()->getPolicyConfig(),
|
|
], '协议与说明已保存');
|
|
} catch (\RuntimeException $e) {
|
|
return api_error($e->getMessage(), 422);
|
|
} catch (\Throwable $e) {
|
|
return api_error('协议与说明保存失败', 500, [
|
|
'detail' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function meta(Request $request)
|
|
{
|
|
return api_success([
|
|
'meta_config' => $this->service()->getMetaConfig(),
|
|
]);
|
|
}
|
|
|
|
public function saveMeta(Request $request)
|
|
{
|
|
try {
|
|
$this->service()->saveMetaConfig((array)$request->input('meta_config', []));
|
|
return api_success([
|
|
'meta_config' => $this->service()->getMetaConfig(),
|
|
], '帮助分类与报告提示已保存');
|
|
} catch (\RuntimeException $e) {
|
|
return api_error($e->getMessage(), 422);
|
|
} catch (\Throwable $e) {
|
|
return api_error('帮助分类与报告提示保存失败', 500, [
|
|
'detail' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function saveHelpArticle(Request $request)
|
|
{
|
|
try {
|
|
$id = $this->service()->saveHelpArticle([
|
|
'id' => (int)$request->input('id', 0),
|
|
'category' => trim((string)$request->input('category', 'service')),
|
|
'title' => trim((string)$request->input('title', '')),
|
|
'summary' => trim((string)$request->input('summary', '')),
|
|
'keywords' => (array)$request->input('keywords', []),
|
|
'content_html' => (string)$request->input('content_html', ''),
|
|
'content_blocks' => (array)$request->input('content_blocks', []),
|
|
'is_recommended' => (bool)$request->input('is_recommended', false),
|
|
'is_enabled' => (bool)$request->input('is_enabled', true),
|
|
'sort_order' => (int)$request->input('sort_order', 0),
|
|
]);
|
|
|
|
return api_success(['id' => $id], '帮助文章已保存');
|
|
} catch (\RuntimeException $e) {
|
|
return api_error($e->getMessage(), 422);
|
|
} catch (\Throwable $e) {
|
|
return api_error('帮助文章保存失败', 500, [
|
|
'detail' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function deleteHelpArticle(Request $request)
|
|
{
|
|
try {
|
|
$this->service()->deleteHelpArticle((int)$request->input('id', 0));
|
|
return api_success([], '帮助文章已删除');
|
|
} catch (\RuntimeException $e) {
|
|
return api_error($e->getMessage(), 422);
|
|
} catch (\Throwable $e) {
|
|
return api_error('帮助文章删除失败', 500, [
|
|
'detail' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function service(): ContentService
|
|
{
|
|
return new ContentService();
|
|
}
|
|
}
|