优化
This commit is contained in:
@@ -30,10 +30,53 @@ type CurveHandler struct {
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
const defaultModelCurveTarget = "Harman over-ear 2018"
|
||||
|
||||
func NewCurveHandler(repo *repository.CurveRepository, curveCache *cache.CurveCache, cfg config.EqualizeConfig, log *zap.Logger) *CurveHandler {
|
||||
return &CurveHandler{repo: repo, cache: curveCache, cfg: cfg, log: log}
|
||||
}
|
||||
|
||||
// ModelCurve 获取机型默认曲线(固定 target: Harman over-ear 2018)
|
||||
// GET /audio/modelCurve?brand=xxx&name=xxx&base64Resp=true
|
||||
func (h *CurveHandler) ModelCurve(c *gin.Context) {
|
||||
brand := strings.TrimSpace(queryParam(c, "brand"))
|
||||
name := strings.TrimSpace(queryParam(c, "name"))
|
||||
base64Resp := encode.ParseBase64Param(c)
|
||||
|
||||
if brand == "" || name == "" {
|
||||
h.writeCurveResponse(c, base64Resp, gin.H{
|
||||
"code": 400,
|
||||
"msg": "参数校验失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
|
||||
result, err := h.getCurvePoint(ctx, brand, name, defaultModelCurveTarget)
|
||||
if err != nil {
|
||||
h.log.Error("get model curve failed", zap.Error(err))
|
||||
response.InternalError(c, "获取曲线数据失败")
|
||||
return
|
||||
}
|
||||
|
||||
if result == "" {
|
||||
h.writeCurveResponse(c, base64Resp, gin.H{
|
||||
"code": 0,
|
||||
"msg": "无曲线数据",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var resp map[string]any
|
||||
if err := json.Unmarshal([]byte(result), &resp); err != nil {
|
||||
response.InternalError(c, "解析曲线数据失败")
|
||||
return
|
||||
}
|
||||
|
||||
h.writeCurveResponse(c, base64Resp, resp)
|
||||
}
|
||||
|
||||
// GetCurve 获取目标曲线
|
||||
// GET /audio/getCurve?brand=xxx&name=xxx&target=xxx&base64Resp=true
|
||||
func (h *CurveHandler) GetCurve(c *gin.Context) {
|
||||
@@ -57,7 +100,7 @@ func (h *CurveHandler) GetCurve(c *gin.Context) {
|
||||
}
|
||||
|
||||
if result == "" {
|
||||
response.Fail(c, http.StatusOK, 40004, "无曲线数据")
|
||||
response.Fail(c, http.StatusOK, 0, "无曲线数据")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -90,6 +133,20 @@ func (h *CurveHandler) GetCurve(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, resultData)
|
||||
}
|
||||
|
||||
func (h *CurveHandler) writeCurveResponse(c *gin.Context, base64Resp bool, data any) {
|
||||
if base64Resp {
|
||||
encoded, err := encode.EncodeJSON(data)
|
||||
if err != nil {
|
||||
h.log.Error("encode response failed", zap.Error(err))
|
||||
response.InternalError(c, "编码响应失败")
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, encoded)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, data)
|
||||
}
|
||||
|
||||
// getCurvePoint 获取曲线数据:先查缓存,缓存不存在则请求 EQ 接口并缓存结果
|
||||
func (h *CurveHandler) getCurvePoint(ctx context.Context, brand, name, target string) (string, error) {
|
||||
data, acquired, err := h.cache.GetWithLock(ctx, brand, name, target)
|
||||
|
||||
Reference in New Issue
Block a user