98 lines
3.5 KiB
JavaScript
98 lines
3.5 KiB
JavaScript
'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/sections` 新增栏目
|
||
* - `PUT /api/web/admin/doc/sections/:id` 修改栏目(名称 / 排序 / 显隐 / 说明;`code` 不可改)
|
||
* - `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/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;
|