liufumingren bde5dc7bad feat(backend): 图片上传接入腾讯云 COS,新增存储管理与未引用清理
- 头像 / 文档图片改由后端校验并转发到 COS,密钥只留在服务端
- 新增 file_records 登记表(迁移 20261123170000),记录对象键、大小、上传者等
- 引用关系实时计算(用户头像 / 文档正文),改删业务数据后状态自动同步
- 新增平台管理「存储管理」页:引用状态与明细、单删、两步清理未引用
- 新增接口 GET /api/web/admin/files、POST /api/web/admin/files/cleanup、
  DELETE /api/web/admin/files/:id(仍被引用默认 409,force 才删)
- COS 目录前缀(COS_DIR_AVATAR / COS_DIR_DOC)、大小上限、清理保护期改为可配置

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-23 16:39:47 +08:00

109 lines
3.8 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/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/upload-image` 上传文档图片到 COS(multipart `file`)
* - `POST /api/web/admin/doc/rebuild` 手动整站重生成(排查用)
*
* 所有写操作都会重生成 `/doc/**` 静态页面与 `/sitemap.txt`(见 `service/doc.js`)。
*/
class WebAdminDocController extends Controller {
/** POST /api/web/admin/doc/upload-image */
async uploadImage() {
const { ctx } = this;
const image = await ctx.service.imageUpload.upload(await ctx.getFileStream(), {
category: 'doc',
dir: this.config.cos.dirs.doc,
});
ctx.body = { ok: true, data: image };
}
/** 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;