Egg 的 BaseContextClass 构造时会把 `this.config = app.config` 挂到实例上, 控制器里叫 `config()` 的动作会被这个实例属性覆盖:路由能匹配上,但 egg-router 拿到的不是函数,最终静默返回 404「接口不存在」。 - controller/web/extension.js:动作改名为 show() - router.js:/api/web/extension/config 指向 controller.web.extension.show - 插件:同步周期文案改为「每 5 分钟」,版本 1.1.1 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
40 lines
1.5 KiB
JavaScript
40 lines
1.5 KiB
JavaScript
'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;
|