b2aed2a6b5
- 新增 GET /audio/shareList?mac=xx 查询未过期分享码(纯 Redis,ZSET 索引)
- share:mac:{mac} 从 SET 改为 ZSET(score=expire_at),支持 ZREMRANGEBYSCORE 精确过期清理
- 空 ZSET 自动 DEL,避免 key 累积
- share:import:pending 增加 12h 兜底 TTL,防止 DB 不可用时内存泄漏
- 导入日志 field 改为 mac:code(去掉 nanotime),同 MAC+code 多次导入幂等去重
- MarkPersisted 保存/恢复 PTTL,防御性编程
- shareCreate 改为 POST + JSON body
- 全量 handler 补充 Swagger 注释,集成 swag 文档生成
- Makefile 使用 $(go env GOPATH)/bin/swag 解决 PATH 问题
94 lines
2.0 KiB
Go
94 lines
2.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/luxsin/app-api/pkg/encode"
|
|
"github.com/redis/go-redis/v9"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type DeviceHandler struct {
|
|
redis *redis.Client
|
|
log *zap.Logger
|
|
}
|
|
|
|
func NewDeviceHandler(redis *redis.Client, log *zap.Logger) *DeviceHandler {
|
|
return &DeviceHandler{
|
|
redis: redis,
|
|
log: log,
|
|
}
|
|
}
|
|
|
|
// 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"))
|
|
ver := strings.TrimSpace(c.Query("ver"))
|
|
clientIP := encode.ClientPublicIP(c)
|
|
|
|
if mac == "" || model == "" {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 400,
|
|
"msg": "参数校验失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
if ver == "" {
|
|
ver = ""
|
|
}
|
|
|
|
h.log.Info("report dev info",
|
|
zap.String("remote_ip", clientIP),
|
|
zap.String("time", time.Now().Format("2006-01-02 15:04:05")),
|
|
)
|
|
|
|
deviceInfo := map[string]string{
|
|
"mac_addr": mac,
|
|
"model": model,
|
|
"active_date": time.Now().Format("2006-01-02"),
|
|
"ip_addr": clientIP,
|
|
"ver": ver,
|
|
}
|
|
|
|
jsonData, err := json.Marshal(deviceInfo)
|
|
if err != nil {
|
|
h.log.Error("marshal device info failed", zap.Error(err))
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 500,
|
|
"msg": "系统错误",
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx := c.Request.Context()
|
|
if err := h.redis.HSet(ctx, "devices", mac, jsonData).Err(); err != nil {
|
|
h.log.Error("redis hset failed", zap.Error(err))
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 500,
|
|
"msg": "系统错误",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"msg": "操作成功",
|
|
})
|
|
}
|