2026-05-21 15:20:12 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2026-05-28 18:26:20 +08:00
|
|
|
"github.com/luxsin/app-api/pkg/encode"
|
2026-05-21 15:20:12 +08:00
|
|
|
"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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"))
|
2026-05-28 18:26:20 +08:00
|
|
|
clientIP := encode.ClientPublicIP(c)
|
2026-05-21 15:20:12 +08:00
|
|
|
|
|
|
|
|
if mac == "" || model == "" {
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"code": 400,
|
|
|
|
|
"msg": "参数校验失败",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ver == "" {
|
|
|
|
|
ver = ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.log.Info("report dev info",
|
2026-05-28 18:26:20 +08:00
|
|
|
zap.String("remote_ip", clientIP),
|
|
|
|
|
zap.String("time", time.Now().Format("2006-01-02 15:04:05")),
|
2026-05-21 15:20:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
deviceInfo := map[string]string{
|
|
|
|
|
"mac_addr": mac,
|
|
|
|
|
"model": model,
|
2026-05-28 18:26:20 +08:00
|
|
|
"active_date": time.Now().Format("2006-01-02"),
|
|
|
|
|
"ip_addr": clientIP,
|
2026-05-21 15:20:12 +08:00
|
|
|
"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": "操作成功",
|
|
|
|
|
})
|
|
|
|
|
}
|