['正文', '

在这里输入区块内容。

'], 'image' => ['图文', '

介绍主题

在这里说明图片或产品。

'], 'banner' => ['横幅', '

用一句话介绍你的项目

简要说明项目能为访客做什么。

了解更多
'], 'features' => ['特性卡片', '

特点一

介绍这个特点。

特点二

介绍这个特点。

特点三

介绍这个特点。

'], 'quote' => ['引文', '

在这里填写引用或用户反馈。

'], 'table' => ['对比表格', '
项目说明
项目一填写说明
项目二填写说明
'], 'list' => ['步骤列表', '
  1. 填写第一步
  2. 填写第二步
  3. 填写第三步
'], 'cta' => ['行动按钮', '

说明下一步。

联系来笙

'], 'faq' => ['常见问题', '
请输入问题

请输入答案。

'], ]; public function newPage(Request $request): Response { if ($failure = $this->guard($request)) { return $failure; } $data = ['title' => '新建页面', 'name' => '', 'slug' => '', 'error' => '']; if ($request->method() === 'POST') { $data['name'] = $this->input($request, 'name'); $data['slug'] = $this->input($request, 'slug'); try { if (!$this->slug($data['slug']) || in_array($data['slug'], ['admin', 'assets', 'articles', 'home', 'services', 'news', 'main', 'favicon'], true) || $data['name'] === '' || mb_strlen($data['name']) > 100) { throw new RuntimeException('请填写页面名称及可用的网址标识。'); } (new Repository())->createPage($data['slug'], $data['name']); return $this->success($request, '/admin/pages/' . $data['slug']); } catch (RuntimeException $error) { $data['error'] = $this->validationMessage($error); } } return $this->render($request, 'studio-new-page', $data, $data['error'] === '' ? 200 : 422); } public function previewPage(Request $request, string $slug): Response { if ($failure = $this->guard($request)) { return $failure; } $repo = new Repository(); $page = $repo->page($slug); return $page ? (new SiteController())->renderPage($repo, $page, true, $request->get('canvas') === '1') : $this->error($request, '找不到此页面。', 404); } public function blocks(Request $request, string $slug): Response { if ($failure = $this->guard($request)) { return $failure; } $repo = new Repository(); if (!$repo->page($slug)) { return $this->error($request, '找不到此页面。', 404); } $action = $this->input($request, 'operation'); $key = $this->input($request, 'key'); try { if (!$this->slug($key) || in_array($key, ['preview', 'home', 'contact', 'main-content', 'blog', 'partners', 'supported-projects'], true)) { throw new RuntimeException('区块锚点不可用。'); } if ($action === 'create') { $preset = self::BLOCKS[$this->input($request, 'preset')] ?? null; if ($preset === null) { throw new RuntimeException('请选择区块类型。'); } $repo->createSection($slug, $key, $preset[0], 'plain', $preset[1]); return $this->success($request, "/admin/pages/$slug/$key"); } if (!in_array($action, ['up', 'down', 'delete'], true)) { throw new RuntimeException('操作不可用。'); } $pageVersion = $this->input($request, 'page_version'); $repo->arrangeSection($slug, $key, $action, $this->version($request), ctype_digit($pageVersion) ? (int) $pageVersion : -1); return $this->success($request, "/admin/pages/$slug#blocks"); } catch (RuntimeException $error) { return $this->error($request, $this->validationMessage($error), 422); } } public function settings(Request $request, string $part): Response { if ($failure = $this->guard($request)) { return $failure; } $site = (new Repository())->site(); $defaults = match ($part) { 'header' => SiteDesign::DEFAULT_HEADER, 'footer' => SiteDesign::DEFAULT_FOOTER + array_intersect_key($site, array_flip(['name', 'address', 'email', 'copyright', 'registration', 'terms'])), 'cdn' => ['origin' => ''], }; $config = ($site[$part] ?? []) + $defaults; $version = $site['revisions'][$part] ?? 0; $error = ''; if ($request->method() === 'POST') { try { $config = $this->settingsInput($request, $part, $defaults); SiteDesign::saveSettings($part, $config, $this->version($request)); return $this->success($request, '/admin/settings/' . $part . '?saved=1'); } catch (RuntimeException $exception) { $error = $this->validationMessage($exception); $version = $this->version($request); } } return $this->render($request, 'studio-settings', [ 'part' => $part, 'config' => $config, 'revision' => $version, 'error' => $error, 'saved' => $request->get('saved') === '1', 'title' => ['header' => '页头', 'footer' => '页脚', 'cdn' => '静态资源 CDN'][$part], ], $error === '' ? 200 : 422); } public function catalog(Request $request): Response { if ($failure = $this->guard($request)) { return $failure; } return $this->render($request, 'studio-catalog', ['title' => '产品与服务', 'products' => SiteDesign::catalog()]); } public function product(Request $request, ?string $id = null): Response { if ($failure = $this->guard($request)) { return $failure; } $fields = ['name', 'category', 'body', 'image', 'alt', 'url', 'label', 'extra_url', 'extra_label', 'position']; $product = $id !== null ? SiteDesign::product((int) $id) : array_fill_keys($fields, '') + ['version' => 0]; if (!$product) { return $this->error($request, '找不到此产品或服务。', 404); } $error = ''; if ($request->method() === 'POST') { try { if ($id !== null && $this->input($request, 'operation') === 'delete') { SiteDesign::deleteProduct((int) $id, $this->version($request)); return $this->success($request, '/admin/catalog'); } foreach ($fields as $field) { $product[$field] = $this->input($request, $field); } $product['version'] = $this->version($request); $this->checkProduct($product); $product['body'] = Html::sanitize($product['body']); $savedId = SiteDesign::saveProduct($id === null ? null : (int) $id, $product, $product['version']); return $this->success($request, '/admin/catalog/' . $savedId . '?saved=1'); } catch (RuntimeException $exception) { $error = $this->validationMessage($exception); } } return $this->render($request, 'studio-product', [ 'title' => $id === null ? '添加产品或服务' : '编辑产品或服务', 'product' => $product, 'id' => $id, 'error' => $error, 'saved' => $request->get('saved') === '1', ], $error === '' ? 200 : 422); } public function media(Request $request): Response { if ($failure = $this->guard($request)) { return $failure; } $error = ''; if ($request->method() === 'POST') { try { $file = $request->file('file'); if (!$file instanceof UploadFile || !$file->isValid()) { throw new RuntimeException('请选择要上传的文件。'); } $label = $this->input($request, 'label') ?: mb_substr((string) $file->getUploadName(), 0, 160); if (mb_strlen($label) > 160) { throw new RuntimeException('资源名称最多 160 个字符。'); } Media::upload($file, $label); return $this->success($request, '/admin/media?uploaded=1'); } catch (RuntimeException $exception) { $error = $this->validationMessage($exception); } } return $this->render($request, 'studio-media', [ 'title' => '媒体库', 'files' => Media::all(true), 'error' => $error, 'saved' => $request->get('uploaded') === '1', ], $error === '' ? 200 : 422); } public function mediaOptions(Request $request): Response { if ($failure = $this->guard($request)) { return $failure; } return $this->json(['files' => Media::all()]); } public function mediaMeta(Request $request, string $name): Response { if ($failure = $this->guard($request)) { return $failure; } $label = $this->input($request, 'label'); if (!Media::find($name)) { return $this->error($request, '找不到此资源。', 404); } if ($label === '' || mb_strlen($label) > 160) { return $this->error($request, '请输入资源名称,最多 160 个字符。', 422); } Media::update($name, $label, $this->input($request, 'archived') === '1'); return $this->success($request, '/admin/media'); } private function settingsInput(Request $request, string $part, array $defaults): array { if ($part === 'cdn') { return ['origin' => Assets::cdn($this->input($request, 'origin'))]; } $result = []; foreach ($defaults as $key => $default) { $input = $this->input($request, $key); if (is_array($default)) { $result[$key] = SiteDesign::parseLinks($input); if ($key === 'button') { if (count($result[$key]) > 1) { throw new RuntimeException('页头按钮只接受一条链接。'); } $result[$key] = $result[$key][0] ?? []; } } else { if (mb_strlen($input) > 1000) { throw new RuntimeException('设置字段过长。'); } $result[$key] = $input; } } if ($result['logo'] !== '' && !Html::url($result['logo'], true)) { throw new RuntimeException('请输入有效的标志图片地址。'); } if ($part === 'footer' && ($result['name'] === '' || ($result['email'] !== '' && !filter_var($result['email'], FILTER_VALIDATE_EMAIL)))) { throw new RuntimeException('请填写公司名称及有效的联系邮箱。'); } return $result; } private function checkProduct(array $item): void { if ($item['name'] === '' || mb_strlen($item['name']) > 100 || $item['category'] === '' || mb_strlen($item['category']) > 70 || strlen($item['body']) > 300000 || strlen($item['image']) > 1000 || mb_strlen($item['alt']) > 200 || ($item['image'] !== '' && !Html::url($item['image'], true)) || !preg_match('/^-?\d{1,6}$/D', $item['position'])) { throw new RuntimeException('请检查名称、分类、简介、封面及排序字段。'); } foreach ([['url', 'label'], ['extra_url', 'extra_label']] as [$href, $label]) { if ($href === 'extra_url' && $item[$href] === '' && $item[$label] === '') { continue; } if (strlen($item[$href]) > 1000 || !Html::url($item[$href]) || $item[$label] === '' || mb_strlen($item[$label]) > 70) { throw new RuntimeException('请填写按钮文案和有效链接。'); } } } private function slug(string $value): bool { return strlen($value) <= 70 && preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/D', $value) === 1; } }