package handlers import ( "license-admin/models" "net/http" "github.com/gin-gonic/gin" "gorm.io/gorm" ) // VerifyRequest 验证请求结构 type VerifyRequest struct { Key string `json:"key" binding:"required"` // 激活码 DeviceID string `json:"device_id" binding:"required"` // 设备ID(客户端UUID) } // VerifyResponse 验证响应结构 type VerifyResponse struct { Code int `json:"code"` // 状态码:0 表示成功,非0 表示失败 Msg string `json:"msg"` // 响应消息 Data struct { Valid bool `json:"valid"` // 验证是否有效 } `json:"data"` } // VerifyLicense 验证许可证 // 核心逻辑: // 1. 在数据库中查找该 key,如果不存在返回错误 // 2. 解析 BoundDevices 字段(JSON 字符串反序列化为 Slice) // 3. 情况A: device_id 已在列表中 -> 验证成功 // 4. 情况B: device_id 不在列表中,且列表长度 < MaxDevices -> 绑定新设备并保存 -> 验证成功 // 5. 情况C: device_id 不在列表中,且列表长度 >= MaxDevices -> 验证失败 func VerifyLicense(db *gorm.DB) gin.HandlerFunc { return func(c *gin.Context) { // 解析请求参数 var req VerifyRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, VerifyResponse{ Code: 400, Msg: "请求参数错误: " + err.Error(), Data: struct { Valid bool `json:"valid"` }{ Valid: false, }, }) return } // 在数据库中查找该 key var license models.License result := db.Where("key = ?", req.Key).First(&license) if result.Error != nil { // 如果不存在,返回 404 或错误信息 if result.Error == gorm.ErrRecordNotFound { c.JSON(http.StatusOK, VerifyResponse{ Code: 400, Msg: "无效的激活码", Data: struct { Valid bool `json:"valid"` }{ Valid: false, }, }) return } // 数据库查询错误 c.JSON(http.StatusInternalServerError, VerifyResponse{ Code: 500, Msg: "数据库查询错误: " + result.Error.Error(), Data: struct { Valid bool `json:"valid"` }{ Valid: false, }, }) return } // 解析 BoundDevices 字段(JSON 字符串反序列化为 Slice) boundDevices, err := license.GetBoundDeviceList() if err != nil { // 解析失败,返回错误 c.JSON(http.StatusInternalServerError, VerifyResponse{ Code: 500, Msg: "解析绑定设备列表失败: " + err.Error(), Data: struct { Valid bool `json:"valid"` }{ Valid: false, }, }) return } // 情况A: 如果 device_id 已经在 BoundDevices 列表中 -> 验证成功 isBound := false for _, deviceID := range boundDevices { if deviceID == req.DeviceID { isBound = true break } } if isBound { c.JSON(http.StatusOK, VerifyResponse{ Code: 0, Msg: "success", Data: struct { Valid bool `json:"valid"` }{ Valid: true, }, }) return } // 情况C: 如果 device_id 不在列表中,且列表长度 >= MaxDevices -> 验证失败 if len(boundDevices) >= license.MaxDevices { c.JSON(http.StatusOK, VerifyResponse{ Code: 400, Msg: "设备数已满", Data: struct { Valid bool `json:"valid"` }{ Valid: false, }, }) return } // 情况B: 如果 device_id 不在列表中,且当前列表长度 < MaxDevices // 将该 device_id 加入列表,重新序列化为 JSON 并保存回数据库 err = license.AddDevice(req.DeviceID) if err != nil { c.JSON(http.StatusInternalServerError, VerifyResponse{ Code: 500, Msg: "绑定设备失败: " + err.Error(), Data: struct { Valid bool `json:"valid"` }{ Valid: false, }, }) return } // 保存到数据库 result = db.Save(&license) if result.Error != nil { c.JSON(http.StatusInternalServerError, VerifyResponse{ Code: 500, Msg: "保存绑定信息失败: " + result.Error.Error(), Data: struct { Valid bool `json:"valid"` }{ Valid: false, }, }) return } // 验证成功,返回信息中提示"新设备已绑定" c.JSON(http.StatusOK, VerifyResponse{ Code: 0, Msg: "success", Data: struct { Valid bool `json:"valid"` }{ Valid: true, }, }) } }