新增分享码功能

This commit is contained in:
eafonyang
2026-06-12 15:50:01 +08:00
parent 46fd35b0df
commit 8010cbd32f
24 changed files with 2610 additions and 66 deletions
+10
View File
@@ -22,6 +22,16 @@ func NewBrandHandler(repo *repository.BrandRepository, log *zap.Logger) *BrandHa
}
}
// GetBrand 获取品牌列表
//
// @Summary 获取品牌列表
// @Tags Brand
// @Produce json
// @Param brandName query string false "品牌名称(模糊匹配)"
// @Param base64Resp query string false "是否返回 base64 编码响应"
// @Success 200 {array} object
// @Failure 500 {object} object
// @Router /audio/getBrand [get]
func (h *BrandHandler) GetBrand(c *gin.Context) {
brandName := c.Query("brandName")
base64Resp := encode.ParseBase64Param(c)
+27
View File
@@ -39,6 +39,19 @@ func NewCurveHandler(repo *repository.CurveRepository, curveCache *cache.CurveCa
}
// ModelCurve 获取机型默认曲线(固定 target: Harman over-ear 2018
//
// @Summary 获取机型默认频响曲线
// @Description 返回指定机型的频响曲线数据(fr),固定使用 Harman over-ear 2018 target
// @Tags Curve
// @Produce json
// @Param brand query string true "品牌名称"
// @Param name query string true "型号名称"
// @Param base64Resp query string false "是否返回 base64 编码响应"
// @Success 200 {object} map[string]any "成功返回 fr 数据"
// @Failure 400 {object} map[string]any "参数校验失败"
// @Failure 500 {object} object
// @Router /audio/modelCurve [get]
//
// GET /audio/modelCurve?brand=xxx&name=xxx&base64Resp=true
func (h *CurveHandler) ModelCurve(c *gin.Context) {
brand := strings.TrimSpace(queryParam(c, "brand"))
@@ -114,6 +127,20 @@ func (h *CurveHandler) ModelCurve(c *gin.Context) {
}
// GetCurve 获取目标曲线
//
// @Summary 获取目标曲线参数化 EQ
// @Description 根据机型和目标曲线名称,计算并返回 parametric_eq 数据
// @Tags Curve
// @Produce json
// @Param brand query string true "品牌名称"
// @Param name query string true "型号名称"
// @Param target query string true "目标曲线名称"
// @Param base64Resp query string false "是否返回 base64 编码响应"
// @Success 200 {object} map[string]any "成功返回 parametric_eq 数据"
// @Failure 400 {object} map[string]any "参数校验失败"
// @Failure 500 {object} object
// @Router /audio/getCurve [get]
//
// GET /audio/getCurve?brand=xxx&name=xxx&target=xxx&base64Resp=true
func (h *CurveHandler) GetCurve(c *gin.Context) {
brand := strings.TrimSpace(queryParam(c, "brand"))
+11
View File
@@ -24,6 +24,17 @@ func NewDeviceHandler(redis *redis.Client, log *zap.Logger) *DeviceHandler {
}
}
// ReportDevInfo 上报设备信息
//
// @Summary 上报设备信息
// @Tags Device
// @Produce json
// @Param mac query string true "设备 MAC 地址"
// @Param model query string true "设备型号"
// @Param ver query string false "固件版本号"
// @Success 200 {object} map[string]any "操作成功"
// @Failure 500 {object} object
// @Router /audio/reportDevInfo [get]
func (h *DeviceHandler) ReportDevInfo(c *gin.Context) {
mac := strings.TrimSpace(c.Query("mac"))
model := strings.TrimSpace(c.Query("model"))
+7
View File
@@ -11,6 +11,13 @@ func NewHealthHandler() *HealthHandler {
return &HealthHandler{}
}
// Check 健康检查
//
// @Summary 健康检查
// @Tags System
// @Produce json
// @Success 200 {object} object
// @Router /api/v1/health [get]
func (h *HealthHandler) Check(c *gin.Context) {
response.OK(c, gin.H{
"status": "up",
+11
View File
@@ -22,6 +22,17 @@ func NewModelHandler(repo *repository.ModelRepository, log *zap.Logger) *ModelHa
}
}
// GetModel 获取型号列表
//
// @Summary 获取型号列表
// @Tags Model
// @Produce json
// @Param brandName query string false "品牌名称"
// @Param modelName query string false "型号名称"
// @Param base64Resp query string false "是否返回 base64 编码响应"
// @Success 200 {array} object
// @Failure 500 {object} object
// @Router /audio/getModel [get]
func (h *ModelHandler) GetModel(c *gin.Context) {
brandName := c.Query("brandName")
modelName := c.Query("modelName")
+14
View File
@@ -28,6 +28,20 @@ func NewModelCSVHandler(s3 *storage.S3Storage, log *zap.Logger) *ModelCSVHandler
}
// GetModelCSV 从 S3 读取耳机 CSV 频响数据
//
// @Summary 获取耳机原始频响 CSV 数据
// @Description 从 S3 读取指定机型的测量 CSV 数据,返回 frequency 和 raw 数组
// @Tags Curve
// @Produce json
// @Param brand query string true "品牌名称"
// @Param model query string true "型号名称"
// @Param form query string true "耳机类型 (in-ear/over-ear)"
// @Param base64Resp query string false "是否返回 base64 编码响应"
// @Success 200 {object} map[string]any "成功返回 frequency 和 raw 数组"
// @Failure 400 {object} map[string]any "参数校验失败"
// @Failure 500 {object} object
// @Router /audio/getModelCSV [get]
//
// GET /audio/getModelCSV?brand=Abyss&model=Dinan DZ&form=over-ear&base64Resp=true
func (h *ModelCSVHandler) GetModelCSV(c *gin.Context) {
brand := strings.TrimSpace(queryParam(c, "brand"))
+11
View File
@@ -23,6 +23,17 @@ func NewModelListHandler(searchClient *search.Client, log *zap.Logger) *ModelLis
}
}
// ModelList 搜索型号列表
//
// @Summary 搜索型号列表(基于 Meilisearch
// @Tags Model
// @Produce json
// @Param key query string false "搜索关键词"
// @Param count query int false "返回数量上限"default(100)
// @Param base64Resp query string false "是否返回 base64 编码响应"
// @Success 200 {array} string
// @Failure 500 {object} object
// @Router /audio/modelList [get]
func (h *ModelListHandler) ModelList(c *gin.Context) {
key := c.Query("key")
base64Resp := encode.ParseBase64Param(c)
+13 -1
View File
@@ -21,7 +21,19 @@ func NewOTAHandler(repo *repository.OTARepository, log *zap.Logger) *OTAHandler
}
// GetOTA 获取 OTA 升级信息
// GET /audio/ota?model=xxx&hw=1&mac=xx:xx:xx&beta=0
//
// @Summary 获取 OTA 升级信息
// @Description 根据设备型号和硬件版本查询最新 OTA 记录,支持黑名单过滤和定向升级逻辑
// @Tags OTA
// @Produce json
// @Param model query string true "设备型号"
// @Param hw query int true "硬件版本"
// @Param mac query string false "设备 MAC 地址"
// @Param beta query int false "是否 beta 通道 (0=否,1=是)"
// @Success 200 {object} object "成功返回 OTA 信息"
// @Failure 400 {object} object
// @Failure 500 {object} object
// @Router /audio/ota [get]
func (h *OTAHandler) GetOTA(c *gin.Context) {
modelVal := strings.TrimSpace(c.Query("model"))
hwStr := strings.TrimSpace(c.Query("hw"))
+170
View File
@@ -0,0 +1,170 @@
package handler
import (
"encoding/json"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/luxsin/app-api/internal/cache"
"github.com/luxsin/app-api/pkg/encode"
"go.uber.org/zap"
)
type ShareCodeHandler struct {
cache *cache.ShareCodeCache
log *zap.Logger
}
func NewShareCodeHandler(shareCache *cache.ShareCodeCache, log *zap.Logger) *ShareCodeHandler {
return &ShareCodeHandler{cache: shareCache, log: log}
}
// ExportShareCode 导出分享码
//
// @Summary 创建 EQ 分享码
// @Description 将用户的 EQ 数据生成一个 5 位分享码,有效期 30 分钟
// @Tags ShareCode
// @Accept json
// @Produce json
// @Param body body object{mac=string,eq_data=object} true "分享请求"
// @Success 200 {object} map[string]any "成功返回 share_code、expire_at、eq_data"
// @Failure 400 {object} map[string]any "参数校验失败"
// @Failure 500 {object} map[string]any "系统错误"
// @Router /audio/shareCreate [post]
//
// POST /audio/shareCreate
// Body: {"mac": "xx", "eq_data": {...}}
func (h *ShareCodeHandler) ExportShareCode(c *gin.Context) {
var req struct {
Mac string `json:"mac"`
EqData json.RawMessage `json:"eq_data"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": 400,
"msg": "参数校验失败",
})
return
}
mac := strings.TrimSpace(req.Mac)
eqDataRaw := strings.TrimSpace(string(req.EqData))
clientIP := encode.ClientPublicIP(c)
if mac == "" || eqDataRaw == "" {
c.JSON(http.StatusOK, gin.H{
"code": 400,
"msg": "参数校验失败",
})
return
}
if !json.Valid([]byte(eqDataRaw)) {
c.JSON(http.StatusOK, gin.H{
"code": 400,
"msg": "eq_data 格式错误",
})
return
}
ctx := c.Request.Context()
data, err := h.cache.Create(ctx, mac, clientIP, []byte(eqDataRaw))
if err != nil {
h.log.Error("create share code failed", zap.Error(err))
c.JSON(http.StatusOK, gin.H{
"code": 500,
"msg": "系统错误",
})
return
}
var eqData any
if err := json.Unmarshal([]byte(data.EqData), &eqData); err != nil {
eqData = data.EqData
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "操作成功",
"share_code": data.ShareCode,
"expire_at": data.ExpireAt.Format("2006-01-02 15:04:05"),
"eq_data": eqData,
})
}
// ImportShareCode 导入分享码
//
// @Summary 导入 EQ 分享码
// @Description 根据分享码获取他人分享的 EQ 数据
// @Tags ShareCode
// @Produce json
// @Param mac query string true "设备 MAC 地址"
// @Param shareCode query string true "5 位分享码"
// @Success 200 {object} map[string]any "成功返回 eq_data"
// @Failure 400 {object} map[string]any "参数校验失败"
// @Failure 500 {object} map[string]any "系统错误"
// @Router /audio/shareAccept [get]
//
// GET /audio/shareAccept?mac=xx&shareCode=ABC12
func (h *ShareCodeHandler) ImportShareCode(c *gin.Context) {
mac := strings.TrimSpace(c.Query("mac"))
shareCode := strings.TrimSpace(c.Query("shareCode"))
clientIP := encode.ClientPublicIP(c)
if mac == "" || shareCode == "" {
c.JSON(http.StatusOK, gin.H{
"code": 400,
"msg": "参数校验失败",
})
return
}
if len(shareCode) != 5 {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"msg": "分享码无效或已过期",
})
return
}
ctx := c.Request.Context()
data, err := h.cache.Get(ctx, shareCode)
if err != nil {
h.log.Error("get share code failed", zap.String("share_code", shareCode), zap.Error(err))
c.JSON(http.StatusOK, gin.H{
"code": 500,
"msg": "系统错误",
})
return
}
if data == nil {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"msg": "分享码无效或已过期",
})
return
}
if err := h.cache.EnqueueImportLog(ctx, mac, shareCode, clientIP, data.EqData, data.ExpireAt); err != nil {
h.log.Error("enqueue share import log failed",
zap.String("share_code", shareCode),
zap.Error(err),
)
c.JSON(http.StatusOK, gin.H{
"code": 500,
"msg": "系统错误",
})
return
}
var eqData any
if err := json.Unmarshal([]byte(data.EqData), &eqData); err != nil {
eqData = data.EqData
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "操作成功",
"eq_data": eqData,
})
}