| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- 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("license_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,
- },
- })
- }
- }
|