'use strict'; const { Controller } = require('egg'); /** * Chrome 插件读取配置(`GET /api/web/extension/config`) * * **公开只读**:插件装在员工浏览器里,没有用户令牌;下发的只有 * 识别前缀 / 生效站点 / 跳转模板 / 安装包地址,不含任何账号或业务数据。 * * 两道保护: * - `config.extension.appKey` 非空时要求 `?key=` 一致(env `EXTENSION_APP_KEY`), * 避免站点清单被随便抓走;不设就完全公开(内网部署的常见做法); * - 响应 `Cache-Control: no-cache`:配置改了要能马上生效,别被 nginx/浏览器缓存一小时。 */ class WebExtensionController extends Controller { /** * GET /api/web/extension/config * * ⚠️ 方法不能叫 `config`:Egg 的 BaseContextClass 会给实例挂 `this.config = app.config`, * 同名方法会被这个实例属性顶掉(路由能匹配上,但拿到的不是函数 → 静默 404)。 */ async show() { const { ctx, app } = this; const expected = (app.config.extension && app.config.extension.appKey) || ''; if (expected && String(ctx.query.key || '') !== expected) { ctx.throw(403, '插件配置需要正确的 key'); } const data = await ctx.service.extensionConfig.publicView(); ctx.set('Cache-Control', 'no-cache, max-age=0'); ctx.set('X-Config-Version', String(data.version)); ctx.body = { ok: true, data }; } } module.exports = WebExtensionController;