verify.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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("license_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. // 设备已激活,更新心跳时间(不更新激活时间)
  98. err = license.UpdateDeviceHeartbeat(req.DeviceID)
  99. if err != nil {
  100. c.JSON(http.StatusInternalServerError, VerifyResponse{
  101. Code: 500,
  102. Msg: "更新心跳时间失败: " + err.Error(),
  103. Data: struct {
  104. Valid bool `json:"valid"`
  105. }{
  106. Valid: false,
  107. },
  108. })
  109. return
  110. }
  111. // 保存心跳时间到数据库
  112. result = db.Save(&license)
  113. if result.Error != nil {
  114. c.JSON(http.StatusInternalServerError, VerifyResponse{
  115. Code: 500,
  116. Msg: "保存心跳时间失败: " + result.Error.Error(),
  117. Data: struct {
  118. Valid bool `json:"valid"`
  119. }{
  120. Valid: false,
  121. },
  122. })
  123. return
  124. }
  125. c.JSON(http.StatusOK, VerifyResponse{
  126. Code: 0,
  127. Msg: "success",
  128. Data: struct {
  129. Valid bool `json:"valid"`
  130. }{
  131. Valid: true,
  132. },
  133. })
  134. return
  135. }
  136. // 情况C: 如果 device_id 不在列表中,且列表长度 >= MaxDevices -> 验证失败
  137. if len(boundDevices) >= license.MaxDevices {
  138. c.JSON(http.StatusOK, VerifyResponse{
  139. Code: 400,
  140. Msg: "设备数已满",
  141. Data: struct {
  142. Valid bool `json:"valid"`
  143. }{
  144. Valid: false,
  145. },
  146. })
  147. return
  148. }
  149. // 情况B: 如果 device_id 不在列表中,且当前列表长度 < MaxDevices
  150. // 将该 device_id 加入列表,重新序列化为 JSON 并保存回数据库
  151. err = license.AddDevice(req.DeviceID)
  152. if err != nil {
  153. c.JSON(http.StatusInternalServerError, VerifyResponse{
  154. Code: 500,
  155. Msg: "绑定设备失败: " + err.Error(),
  156. Data: struct {
  157. Valid bool `json:"valid"`
  158. }{
  159. Valid: false,
  160. },
  161. })
  162. return
  163. }
  164. // 保存到数据库
  165. result = db.Save(&license)
  166. if result.Error != nil {
  167. c.JSON(http.StatusInternalServerError, VerifyResponse{
  168. Code: 500,
  169. Msg: "保存绑定信息失败: " + result.Error.Error(),
  170. Data: struct {
  171. Valid bool `json:"valid"`
  172. }{
  173. Valid: false,
  174. },
  175. })
  176. return
  177. }
  178. // 验证成功,返回信息中提示"新设备已绑定"
  179. c.JSON(http.StatusOK, VerifyResponse{
  180. Code: 0,
  181. Msg: "success",
  182. Data: struct {
  183. Valid bool `json:"valid"`
  184. }{
  185. Valid: true,
  186. },
  187. })
  188. }
  189. }