From fc4ec2c03015a1fabcc94cb2e30fea6bfb0ba054 Mon Sep 17 00:00:00 2001 From: eafonyang Date: Mon, 15 Jun 2026 17:58:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=86=E4=BA=AB=E7=A0=81?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3=EF=BC=8C=E4=B8=8D=E5=86=99?= =?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C=E6=97=A5=E5=BF=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/docs.go | 44 ++++++++++++++++ docs/swagger.json | 44 ++++++++++++++++ docs/swagger.yaml | 30 +++++++++++ internal/handler/share_code.go | 94 ++++++++++++++++++++++++++-------- internal/router/router.go | 1 + 5 files changed, 191 insertions(+), 22 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index e600337..f06c0b5 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -464,6 +464,50 @@ const docTemplate = `{ } } }, + "/audio/shareQuery": { + "get": { + "description": "根据分享码预览 EQ 数据,不写入导入流水", + "produces": [ + "application/json" + ], + "tags": [ + "ShareCode" + ], + "summary": "查询 EQ 分享码", + "parameters": [ + { + "type": "string", + "description": "5 位分享码", + "name": "shareCode", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "成功返回 eq_data、expire_at", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "400": { + "description": "参数校验失败", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "500": { + "description": "系统错误", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, "/audio/shareAccept": { "get": { "description": "根据分享码获取他人分享的 EQ 数据", diff --git a/docs/swagger.json b/docs/swagger.json index b7a607e..effe6fb 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -457,6 +457,50 @@ } } }, + "/audio/shareQuery": { + "get": { + "description": "根据分享码预览 EQ 数据,不写入导入流水", + "produces": [ + "application/json" + ], + "tags": [ + "ShareCode" + ], + "summary": "查询 EQ 分享码", + "parameters": [ + { + "type": "string", + "description": "5 位分享码", + "name": "shareCode", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "成功返回 eq_data、expire_at", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "400": { + "description": "参数校验失败", + "schema": { + "type": "object", + "additionalProperties": true + } + }, + "500": { + "description": "系统错误", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, "/audio/shareAccept": { "get": { "description": "根据分享码获取他人分享的 EQ 数据", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index b48f3bb..6bab092 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -303,6 +303,36 @@ paths: summary: 上报设备信息 tags: - Device + /audio/shareQuery: + get: + description: 根据分享码预览 EQ 数据,不写入导入流水 + parameters: + - description: 5 位分享码 + in: query + name: shareCode + required: true + type: string + produces: + - application/json + responses: + "200": + description: 成功返回 eq_data、expire_at + schema: + additionalProperties: true + type: object + "400": + description: 参数校验失败 + schema: + additionalProperties: true + type: object + "500": + description: 系统错误 + schema: + additionalProperties: true + type: object + summary: 查询 EQ 分享码 + tags: + - ShareCode /audio/shareAccept: get: description: 根据分享码获取他人分享的 EQ 数据 diff --git a/internal/handler/share_code.go b/internal/handler/share_code.go index 01b8385..e6def73 100644 --- a/internal/handler/share_code.go +++ b/internal/handler/share_code.go @@ -165,6 +165,39 @@ func (h *ShareCodeHandler) ListShareCodesByMac(c *gin.Context) { }) } +// QueryShareCode 查询分享码(预览,不记录导入) +// +// @Summary 查询 EQ 分享码 +// @Description 根据分享码预览 EQ 数据,不写入导入流水 +// @Tags ShareCode +// @Produce json +// @Param shareCode query string true "5 位分享码" +// @Success 200 {object} map[string]any "成功返回 eq_data、expire_at" +// @Failure 400 {object} map[string]any "参数校验失败" +// @Failure 500 {object} map[string]any "系统错误" +// @Router /audio/shareQuery [get] +// +// GET /audio/shareQuery?shareCode=ABC12 +func (h *ShareCodeHandler) QueryShareCode(c *gin.Context) { + shareCode := strings.TrimSpace(c.Query("shareCode")) + data, ok := h.lookupShareCode(c, shareCode) + if !ok { + 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, + "expire_at": data.ExpireAt.Format("2006-01-02 15:04:05"), + }) +} + // ImportShareCode 导入分享码 // // @Summary 导入 EQ 分享码 @@ -191,32 +224,13 @@ func (h *ShareCodeHandler) ImportShareCode(c *gin.Context) { }) return } - if len(shareCode) != 5 { - c.JSON(http.StatusOK, gin.H{ - "code": 0, - "msg": "分享码无效或已过期", - }) + + data, ok := h.lookupShareCode(c, shareCode) + if !ok { 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), @@ -240,3 +254,39 @@ func (h *ShareCodeHandler) ImportShareCode(c *gin.Context) { "eq_data": eqData, }) } + +func (h *ShareCodeHandler) lookupShareCode(c *gin.Context, shareCode string) (*cache.ShareCodeData, bool) { + if shareCode == "" { + c.JSON(http.StatusOK, gin.H{ + "code": 400, + "msg": "参数校验失败", + }) + return nil, false + } + if len(shareCode) != 5 { + c.JSON(http.StatusOK, gin.H{ + "code": 0, + "msg": "分享码无效或已过期", + }) + return nil, false + } + + 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 nil, false + } + if data == nil { + c.JSON(http.StatusOK, gin.H{ + "code": 0, + "msg": "分享码无效或已过期", + }) + return nil, false + } + return data, true +} diff --git a/internal/router/router.go b/internal/router/router.go index 33daa99..73aebf6 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -72,6 +72,7 @@ func New(log *zap.Logger, db *sql.DB, searchClient *search.Client, rdb *redis.Cl audio.GET("/getModelCSV", modelCSV.GetModelCSV) audio.POST("/shareCreate", shareCode.ExportShareCode) audio.GET("/shareList", shareCode.ListShareCodesByMac) + audio.GET("/shareQuery", shareCode.QueryShareCode) audio.GET("/shareAccept", shareCode.ImportShareCode) }