license.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package models
  2. import (
  3. "encoding/json"
  4. "time"
  5. )
  6. // License 许可证模型
  7. type License struct {
  8. ID uint `gorm:"primarykey" json:"id"`
  9. LicenseKey string `gorm:"column:license_key;uniqueIndex;not null" json:"key"`
  10. BoundDevices string `gorm:"type:text" json:"bound_devices"` // JSON 数组字符串,例如 '["uuid-1", "uuid-2"]'
  11. DeviceActivations string `gorm:"type:text" json:"device_activations"` // JSON 对象字符串,记录首次激活时间,例如 '{"uuid-1": "2024-01-01T00:00:00Z", "uuid-2": "2024-01-02T00:00:00Z"}'
  12. DeviceHeartbeats string `gorm:"type:text" json:"device_heartbeats"` // JSON 对象字符串,记录心跳时间(最后验证时间),例如 '{"uuid-1": "2024-01-01T12:00:00Z", "uuid-2": "2024-01-02T12:00:00Z"}'
  13. MaxDevices int `gorm:"default:2" json:"max_devices"`
  14. Remark string `gorm:"type:text" json:"remark"` // 备注信息
  15. CreatedAt time.Time `json:"created_at"`
  16. UpdatedAt time.Time `json:"updated_at"`
  17. }
  18. // GetBoundDeviceList 获取已绑定设备ID列表
  19. func (l *License) GetBoundDeviceList() ([]string, error) {
  20. if l.BoundDevices == "" {
  21. return []string{}, nil
  22. }
  23. var devices []string
  24. err := json.Unmarshal([]byte(l.BoundDevices), &devices)
  25. return devices, err
  26. }
  27. // SetBoundDeviceList 设置已绑定设备ID列表
  28. func (l *License) SetBoundDeviceList(devices []string) error {
  29. data, err := json.Marshal(devices)
  30. if err != nil {
  31. return err
  32. }
  33. l.BoundDevices = string(data)
  34. return nil
  35. }
  36. // IsDeviceBound 检查设备是否已绑定
  37. func (l *License) IsDeviceBound(deviceID string) (bool, error) {
  38. devices, err := l.GetBoundDeviceList()
  39. if err != nil {
  40. return false, err
  41. }
  42. for _, d := range devices {
  43. if d == deviceID {
  44. return true, nil
  45. }
  46. }
  47. return false, nil
  48. }
  49. // AddDevice 添加设备到绑定列表
  50. func (l *License) AddDevice(deviceID string) error {
  51. devices, err := l.GetBoundDeviceList()
  52. if err != nil {
  53. return err
  54. }
  55. devices = append(devices, deviceID)
  56. if err := l.SetBoundDeviceList(devices); err != nil {
  57. return err
  58. }
  59. // 记录设备激活时间
  60. return l.RecordDeviceActivation(deviceID)
  61. }
  62. // GetDeviceActivations 获取设备激活时间映射
  63. func (l *License) GetDeviceActivations() (map[string]time.Time, error) {
  64. if l.DeviceActivations == "" {
  65. return make(map[string]time.Time), nil
  66. }
  67. var activations map[string]string
  68. if err := json.Unmarshal([]byte(l.DeviceActivations), &activations); err != nil {
  69. return nil, err
  70. }
  71. result := make(map[string]time.Time)
  72. for deviceID, timeStr := range activations {
  73. t, err := time.Parse(time.RFC3339, timeStr)
  74. if err != nil {
  75. return nil, err
  76. }
  77. result[deviceID] = t
  78. }
  79. return result, nil
  80. }
  81. // RecordDeviceActivation 记录设备激活时间(仅在首次激活时记录)
  82. func (l *License) RecordDeviceActivation(deviceID string) error {
  83. activations, err := l.GetDeviceActivations()
  84. if err != nil {
  85. activations = make(map[string]time.Time)
  86. }
  87. // 如果设备已存在,不更新激活时间;如果不存在,记录当前时间
  88. if _, exists := activations[deviceID]; !exists {
  89. activations[deviceID] = time.Now()
  90. // 转换为字符串格式存储
  91. activationsStr := make(map[string]string)
  92. for id, t := range activations {
  93. activationsStr[id] = t.Format(time.RFC3339)
  94. }
  95. data, err := json.Marshal(activationsStr)
  96. if err != nil {
  97. return err
  98. }
  99. l.DeviceActivations = string(data)
  100. }
  101. return nil
  102. }
  103. // GetDeviceHeartbeats 获取设备心跳时间映射
  104. func (l *License) GetDeviceHeartbeats() (map[string]time.Time, error) {
  105. if l.DeviceHeartbeats == "" {
  106. return make(map[string]time.Time), nil
  107. }
  108. var heartbeats map[string]string
  109. if err := json.Unmarshal([]byte(l.DeviceHeartbeats), &heartbeats); err != nil {
  110. return nil, err
  111. }
  112. result := make(map[string]time.Time)
  113. for deviceID, timeStr := range heartbeats {
  114. t, err := time.Parse(time.RFC3339, timeStr)
  115. if err != nil {
  116. return nil, err
  117. }
  118. result[deviceID] = t
  119. }
  120. return result, nil
  121. }
  122. // UpdateDeviceHeartbeat 更新设备心跳时间(每次验证时更新)
  123. func (l *License) UpdateDeviceHeartbeat(deviceID string) error {
  124. heartbeats, err := l.GetDeviceHeartbeats()
  125. if err != nil {
  126. heartbeats = make(map[string]time.Time)
  127. }
  128. // 更新心跳时间为当前时间
  129. heartbeats[deviceID] = time.Now()
  130. // 转换为字符串格式存储
  131. heartbeatsStr := make(map[string]string)
  132. for id, t := range heartbeats {
  133. heartbeatsStr[id] = t.Format(time.RFC3339)
  134. }
  135. data, err := json.Marshal(heartbeatsStr)
  136. if err != nil {
  137. return err
  138. }
  139. l.DeviceHeartbeats = string(data)
  140. return nil
  141. }
  142. // CanBindMoreDevices 检查是否可以绑定更多设备
  143. func (l *License) CanBindMoreDevices() (bool, error) {
  144. devices, err := l.GetBoundDeviceList()
  145. if err != nil {
  146. return false, err
  147. }
  148. return len(devices) < l.MaxDevices, nil
  149. }