verify.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package handlers
  2. import (
  3. "license-admin/models"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "gorm.io/gorm"
  7. )
  8. // VerifyRequest 验证请求结构
  9. type VerifyRequest struct {
  10. Key string `json:"key" binding:"required"` // 激活码
  11. DeviceID string `json:"device_id" binding:"required"` // 设备ID(客户端UUID)
  12. }
  13. // VerifyResponse 验证响应结构
  14. type VerifyResponse struct {
  15. Code int `json:"code"` // 状态码:0 表示成功,非0 表示失败
  16. Msg string `json:"msg"` // 响应消息
  17. Data struct {
  18. Valid bool `json:"valid"` // 验证是否有效
  19. } `json:"data"`
  20. }
  21. // VerifyLicense 验证许可证
  22. // 核心逻辑:
  23. // 1. 在数据库中查找该 key,如果不存在返回错误
  24. // 2. 解析 BoundDevices 字段(JSON 字符串反序列化为 Slice)
  25. // 3. 情况A: device_id 已在列表中 -> 验证成功
  26. // 4. 情况B: device_id 不在列表中,且列表长度 < MaxDevices -> 绑定新设备并保存 -> 验证成功
  27. // 5. 情况C: device_id 不在列表中,且列表长度 >= MaxDevices -> 验证失败
  28. func VerifyLicense(db *gorm.DB) gin.HandlerFunc {
  29. return func(c *gin.Context) {
  30. // 解析请求参数
  31. var req VerifyRequest
  32. if err := c.ShouldBindJSON(&req); err != nil {
  33. c.JSON(http.StatusBadRequest, VerifyResponse{
  34. Code: 400,
  35. Msg: "请求参数错误: " + err.Error(),
  36. Data: struct {
  37. Valid bool `json:"valid"`
  38. }{
  39. Valid: false,
  40. },
  41. })
  42. return
  43. }
  44. // 在数据库中查找该 key
  45. var license models.License
  46. result := db.Where("key = ?", req.Key).First(&license)
  47. if result.Error != nil {
  48. // 如果不存在,返回 404 或错误信息
  49. if result.Error == gorm.ErrRecordNotFound {
  50. c.JSON(http.StatusOK, VerifyResponse{
  51. Code: 400,
  52. Msg: "无效的激活码",
  53. Data: struct {
  54. Valid bool `json:"valid"`
  55. }{
  56. Valid: false,
  57. },
  58. })
  59. return
  60. }
  61. // 数据库查询错误
  62. c.JSON(http.StatusInternalServerError, VerifyResponse{
  63. Code: 500,
  64. Msg: "数据库查询错误: " + result.Error.Error(),
  65. Data: struct {
  66. Valid bool `json:"valid"`
  67. }{
  68. Valid: false,
  69. },
  70. })
  71. return
  72. }
  73. // 解析 BoundDevices 字段(JSON 字符串反序列化为 Slice)
  74. boundDevices, err := license.GetBoundDeviceList()
  75. if err != nil {
  76. // 解析失败,返回错误
  77. c.JSON(http.StatusInternalServerError, VerifyResponse{
  78. Code: 500,
  79. Msg: "解析绑定设备列表失败: " + err.Error(),
  80. Data: struct {
  81. Valid bool `json:"valid"`
  82. }{
  83. Valid: false,
  84. },
  85. })
  86. return
  87. }
  88. // 情况A: 如果 device_id 已经在 BoundDevices 列表中 -> 验证成功
  89. isBound := false
  90. for _, deviceID := range boundDevices {
  91. if deviceID == req.DeviceID {
  92. isBound = true
  93. break
  94. }
  95. }
  96. if isBound {
  97. c.JSON(http.StatusOK, VerifyResponse{
  98. Code: 0,
  99. Msg: "success",
  100. Data: struct {
  101. Valid bool `json:"valid"`
  102. }{
  103. Valid: true,
  104. },
  105. })
  106. return
  107. }
  108. // 情况C: 如果 device_id 不在列表中,且列表长度 >= MaxDevices -> 验证失败
  109. if len(boundDevices) >= license.MaxDevices {
  110. c.JSON(http.StatusOK, VerifyResponse{
  111. Code: 400,
  112. Msg: "设备数已满",
  113. Data: struct {
  114. Valid bool `json:"valid"`
  115. }{
  116. Valid: false,
  117. },
  118. })
  119. return
  120. }
  121. // 情况B: 如果 device_id 不在列表中,且当前列表长度 < MaxDevices
  122. // 将该 device_id 加入列表,重新序列化为 JSON 并保存回数据库
  123. err = license.AddDevice(req.DeviceID)
  124. if err != nil {
  125. c.JSON(http.StatusInternalServerError, VerifyResponse{
  126. Code: 500,
  127. Msg: "绑定设备失败: " + err.Error(),
  128. Data: struct {
  129. Valid bool `json:"valid"`
  130. }{
  131. Valid: false,
  132. },
  133. })
  134. return
  135. }
  136. // 保存到数据库
  137. result = db.Save(&license)
  138. if result.Error != nil {
  139. c.JSON(http.StatusInternalServerError, VerifyResponse{
  140. Code: 500,
  141. Msg: "保存绑定信息失败: " + result.Error.Error(),
  142. Data: struct {
  143. Valid bool `json:"valid"`
  144. }{
  145. Valid: false,
  146. },
  147. })
  148. return
  149. }
  150. // 验证成功,返回信息中提示"新设备已绑定"
  151. c.JSON(http.StatusOK, VerifyResponse{
  152. Code: 0,
  153. Msg: "success",
  154. Data: struct {
  155. Valid bool `json:"valid"`
  156. }{
  157. Valid: true,
  158. },
  159. })
  160. }
  161. }