Files
sundynix-agentix/sundynix-gateway/internal/handler/voice_config.go
T
Blizzard a28ae49b6a refactor(voice): 语音配置改用新版 API Key 鉴权(弃旧版 appid+token)
用户提醒:火山新版走 API Key 鉴权,不用旧版 appid+access_token。新版 WS 握手只带两个
header——Authorization(Bearer <APIKey>) + X-Api-Resource-Id。

配置从 {appid, access_token, 2×resource-id, voice} 收敛为 {api_key, 2×resource-id,
voice}:APIKey 走 secrets AES 加密入库;ASREnabled/TTSEnabled 改为只看 api_key+对应
resource-id。admin 配置页两个字段并一个 API Key 字段,清单同步改为新版口径。build+tsc 绿。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-21 17:06:22 +08:00

79 lines
2.4 KiB
Go

package handler
import (
"context"
"encoding/json"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/sundynix/sundynix-gateway/internal/voice"
)
// 语音(火山引擎豆包语音)配置的管理端存取。设计见 VOICE_DESIGN.md。
// AccessToken AES 加密入库;单管理员后台明文回显(同微信配置),方便核对/复制。
const SettingVoice = "voice_config" // 语音配置(setting 表)
func (h *Handler) loadVoiceConfig(ctx context.Context) voice.Config {
raw := h.db.GetSetting(ctx, SettingVoice)
if raw == "" {
return voice.Config{}
}
var c voice.Config
if json.Unmarshal([]byte(raw), &c) != nil {
return voice.Config{}
}
return c.DecryptFromStore()
}
// AdminGetVoiceConfig: GET /api/v1/admin/voice —— 回显语音配置(api_key 明文,RequireAdmin 已拦)。
func (h *Handler) AdminGetVoiceConfig(c *gin.Context) {
cfg := h.loadVoiceConfig(c.Request.Context())
c.JSON(http.StatusOK, gin.H{
"api_key": cfg.APIKey,
"asr_resource_id": cfg.ASRResourceID,
"tts_resource_id": cfg.TTSResourceID,
"tts_voice_type": cfg.TTSVoiceType,
"asr_enabled": cfg.ASREnabled(),
"tts_enabled": cfg.TTSEnabled(),
})
}
// AdminSaveVoiceConfig: PUT /api/v1/admin/voice —— 保存语音配置(api_key 空串=沿用已存)。
func (h *Handler) AdminSaveVoiceConfig(c *gin.Context) {
var b struct {
APIKey string `json:"api_key"`
ASRResourceID string `json:"asr_resource_id"`
TTSResourceID string `json:"tts_resource_id"`
TTSVoiceType string `json:"tts_voice_type"`
}
if err := c.ShouldBindJSON(&b); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
return
}
ctx := c.Request.Context()
key := strings.TrimSpace(b.APIKey)
if key == "" {
key = h.loadVoiceConfig(ctx).APIKey // 留空=沿用已存
}
cfg := voice.Config{
APIKey: key,
ASRResourceID: strings.TrimSpace(b.ASRResourceID),
TTSResourceID: strings.TrimSpace(b.TTSResourceID),
TTSVoiceType: strings.TrimSpace(b.TTSVoiceType),
}
stored, err := cfg.EncryptedForStore()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "密钥加密失败: " + err.Error()})
return
}
raw, _ := json.Marshal(stored)
if err := h.db.SetSetting(ctx, SettingVoice, string(raw)); err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ok", "asr_enabled": cfg.ASREnabled(), "tts_enabled": cfg.TTSEnabled()})
}