'use strict'; const { Controller } = require('egg'); /** * 运维/内部:系统配置(sys_config)读写 * * 挂在 `middleware.internalAuth()` 后面;读接口带 Redis 缓存,写接口自动失效。 */ class InternalConfigController extends Controller { /** GET /api/configs?group=wechat —— 列表(管理用,含禁用项) */ async index() { const { ctx } = this; const list = await ctx.service.config.list({ groupName: ctx.query.group }); ctx.body = { ok: true, data: { list, total: list.length } }; } /** GET /api/configs/:group/:key —— 取单个配置值(带 Redis 缓存) */ async show() { const { ctx } = this; const { group, key } = ctx.params; const value = await ctx.service.config.get(group, key); if (value === null) { ctx.status = 404; ctx.body = { ok: false, message: '配置不存在' }; return; } ctx.body = { ok: true, data: { group, key, value } }; } /** GET /api/configs/:group —— 取一个分组的所有启用配置 */ async showGroup() { const { ctx } = this; const { group } = ctx.params; const data = await ctx.service.config.getGroup(group); ctx.body = { ok: true, data }; } /** PUT /api/configs —— 新增或更新(幂等) */ async upsert() { const { ctx } = this; const { configKey } = ctx.request.body || {}; if (!configKey) { ctx.status = 422; ctx.body = { ok: false, message: 'configKey 不能为空' }; return; } const data = await ctx.service.config.set(ctx.request.body); ctx.body = { ok: true, data }; } /** DELETE /api/configs/:group/:key —— 软删除 */ async destroy() { const { ctx } = this; const { group, key } = ctx.params; const removed = await ctx.service.config.remove(group, key); if (!removed) { ctx.status = 404; ctx.body = { ok: false, message: '配置不存在' }; return; } ctx.body = { ok: true, data: { group, key } }; } } module.exports = InternalConfigController;