liufumingren 78cee0fe1a feat(doc): 首页登录分流 + 文档站(简易 CMS / 静态 HTML / sitemap)
- 首页:`/` 未登录原地显示登录页(不再跳 /login),已登录直接进 `/dashboard`;
  看板路由由 `/` 迁到 `/dashboard`,侧边栏「首页」与无权限回跳同步;
  桌面端「工具 → 打开配置页」的 `/?token=` 免登录仍然可用
- 文档站:`/doc/**` 由后端整站生成静态 HTML(顶部分类 tab 全局切换、左侧栏目 → 内容页、
  右侧本页目录);`/sitemap.txt` 新链接追加到末尾,下架 / 删除的会被清理
- 平台管理 · 文档管理(`/admin/doc`):分类 / 栏目 / 内容页(Markdown)简易 CMS,
  内容页有编辑表单与列表,保存后自动重生成目录与静态页面
- 登录页右下角、顶栏(登录后)、看板右上角新增进入文档站的链接
- 部署:nginx 模板为 `/doc`、`/doc/`、`/sitemap.txt` 增加反代(否则会落到 SPA 回退);
  Vite dev server 增加同名代理;`middleware/frontend.js` 排除这些路径
- 登录页插画改为内联 SVG(沿用工作区已有的未提交改动)
- 测试:新增 Markdown 渲染单测;后端 218 个用例通过,前端 typecheck / build 通过

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-07 20:33:46 +08:00

121 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const { Controller } = require('egg');
/** 路径参数转正整数,非法返回 null */
function toId(value) {
const id = Number(value);
return Number.isInteger(id) && id > 0 ? id : null;
}
/**
* web 平台管理员:文档站(简易 CMS)
*
* 鉴权:`middleware.apiAuth()` + `middleware.platformAdmin()`(能进来的都是平台管理员)。
*
* - `GET /api/web/admin/doc/tree` 分类 → 栏目 → 内容页(树,文章不带正文)
* - `POST /api/web/admin/doc/categories` 新增分类
* - `PUT /api/web/admin/doc/categories/:id` 修改分类(名称 / 排序 / 显隐;`code` 不可改)
* - `DELETE /api/web/admin/doc/categories/:id` 删除分类(栏目与内容页一起删)
* - `POST /api/web/admin/doc/sections` 新增栏目
* - `PUT /api/web/admin/doc/sections/:id` 修改栏目
* - `DELETE /api/web/admin/doc/sections/:id` 删除栏目(内容页一起删)
* - `GET /api/web/admin/doc/articles/:id` 单篇(含正文)
* - `POST /api/web/admin/doc/articles` 新增内容页
* - `PUT /api/web/admin/doc/articles/:id` 修改内容页
* - `DELETE /api/web/admin/doc/articles/:id` 删除内容页
* - `POST /api/web/admin/doc/rebuild` 手动整站重生成(排查用)
*
* 所有写操作都会重生成 `/doc/**` 静态页面与 `/sitemap.txt`(见 `service/doc.js`)。
*/
class WebAdminDocController extends Controller {
/** GET /api/web/admin/doc/tree */
async tree() {
const data = await this.ctx.service.doc.tree();
this.ctx.body = { ok: true, data };
}
/** POST /api/web/admin/doc/categories */
async createCategory() {
const data = await this.ctx.service.doc.createCategory(this.ctx.request.body || {});
this.ctx.status = 201;
this.ctx.body = { ok: true, data };
}
/** PUT /api/web/admin/doc/categories/:id */
async updateCategory() {
const id = this.requireId();
const data = await this.ctx.service.doc.updateCategory(id, this.ctx.request.body || {});
this.ctx.body = { ok: true, data };
}
/** DELETE /api/web/admin/doc/categories/:id */
async destroyCategory() {
const data = await this.ctx.service.doc.removeCategory(this.requireId());
this.ctx.body = { ok: true, data };
}
/** POST /api/web/admin/doc/sections */
async createSection() {
const data = await this.ctx.service.doc.createSection(this.ctx.request.body || {});
this.ctx.status = 201;
this.ctx.body = { ok: true, data };
}
/** PUT /api/web/admin/doc/sections/:id */
async updateSection() {
const id = this.requireId();
const data = await this.ctx.service.doc.updateSection(id, this.ctx.request.body || {});
this.ctx.body = { ok: true, data };
}
/** DELETE /api/web/admin/doc/sections/:id */
async destroySection() {
const data = await this.ctx.service.doc.removeSection(this.requireId());
this.ctx.body = { ok: true, data };
}
/** GET /api/web/admin/doc/articles/:id */
async showArticle() {
const data = await this.ctx.service.doc.findArticle(this.requireId());
this.ctx.body = { ok: true, data };
}
/** POST /api/web/admin/doc/articles */
async createArticle() {
const data = await this.ctx.service.doc.createArticle(this.ctx.request.body || {});
this.ctx.status = 201;
this.ctx.body = { ok: true, data };
}
/** PUT /api/web/admin/doc/articles/:id */
async updateArticle() {
const id = this.requireId();
const data = await this.ctx.service.doc.updateArticle(id, this.ctx.request.body || {});
this.ctx.body = { ok: true, data };
}
/** DELETE /api/web/admin/doc/articles/:id */
async destroyArticle() {
const data = await this.ctx.service.doc.removeArticle(this.requireId());
this.ctx.body = { ok: true, data };
}
/** POST /api/web/admin/doc/rebuild */
async rebuild() {
const { ctx } = this;
const files = await ctx.service.docSite.generate(ctx);
const model = await ctx.service.docSite.loadModel();
const urls = ctx.service.docSite.buildSitemapUrls(model, ctx.service.docSite.baseUrl(ctx));
ctx.body = { ok: true, data: { files: files.length, urls: urls.length } };
}
requireId() {
const id = toId(this.ctx.params.id);
if (!id) this.ctx.throw(400, 'id 必须是正整数');
return id;
}
}
module.exports = WebAdminDocController;