license.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. CreatedAt time.Time `json:"created_at"`
  15. UpdatedAt time.Time `json:"updated_at"`
  16. }
  17. // GetBoundDeviceList 获取已绑定设备ID列表
  18. func (l *License) GetBoundDeviceList() ([]string, error) {
  19. if l.BoundDevices == "" {
  20. return []string{}, nil
  21. }
  22. var devices []string
  23. err := json.Unmarshal([]byte(l.BoundDevices), &devices)
  24. return devices, err
  25. }
  26. // SetBoundDeviceList 设置已绑定设备ID列表
  27. func (l *License) SetBoundDeviceList(devices []string) error {
  28. data, err := json.Marshal(devices)
  29. if err != nil {
  30. return err
  31. }
  32. l.BoundDevices = string(data)
  33. return nil
  34. }
  35. // IsDeviceBound 检查设备是否已绑定
  36. func (l *License) IsDeviceBound(deviceID string) (bool, error) {
  37. devices, err := l.GetBoundDeviceList()
  38. if err != nil {
  39. return false, err
  40. }
  41. for _, d := range devices {
  42. if d == deviceID {
  43. return true, nil
  44. }
  45. }
  46. return false, nil
  47. }
  48. // AddDevice 添加设备到绑定列表
  49. func (l *License) AddDevice(deviceID string) error {
  50. devices, err := l.GetBoundDeviceList()
  51. if err != nil {
  52. return err
  53. }
  54. devices = append(devices, deviceID)
  55. if err := l.SetBoundDeviceList(devices); err != nil {
  56. return err
  57. }
  58. // 记录设备激活时间
  59. return l.RecordDeviceActivation(deviceID)
  60. }
  61. // GetDeviceActivations 获取设备激活时间映射
  62. func (l *License) GetDeviceActivations() (map[string]time.Time, error) {
  63. if l.DeviceActivations == "" {
  64. return make(map[string]time.Time), nil
  65. }
  66. var activations map[string]string
  67. if err := json.Unmarshal([]byte(l.DeviceActivations), &activations); err != nil {
  68. return nil, err
  69. }
  70. result := make(map[string]time.Time)
  71. for deviceID, timeStr := range activations {
  72. t, err := time.Parse(time.RFC3339, timeStr)
  73. if err != nil {
  74. return nil, err
  75. }
  76. result[deviceID] = t
  77. }
  78. return result, nil
  79. }
  80. // RecordDeviceActivation 记录设备激活时间(仅在首次激活时记录)
  81. func (l *License) RecordDeviceActivation(deviceID string) error {
  82. activations, err := l.GetDeviceActivations()
  83. if err != nil {
  84. activations = make(map[string]time.Time)
  85. }
  86. // 如果设备已存在,不更新激活时间;如果不存在,记录当前时间
  87. if _, exists := activations[deviceID]; !exists {
  88. activations[deviceID] = time.Now()
  89. // 转换为字符串格式存储
  90. activationsStr := make(map[string]string)
  91. for id, t := range activations {
  92. activationsStr[id] = t.Format(time.RFC3339)
  93. }
  94. data, err := json.Marshal(activationsStr)
  95. if err != nil {
  96. return err
  97. }
  98. l.DeviceActivations = string(data)
  99. }
  100. return nil
  101. }
  102. // GetDeviceHeartbeats 获取设备心跳时间映射
  103. func (l *License) GetDeviceHeartbeats() (map[string]time.Time, error) {
  104. if l.DeviceHeartbeats == "" {
  105. return make(map[string]time.Time), nil
  106. }
  107. var heartbeats map[string]string
  108. if err := json.Unmarshal([]byte(l.DeviceHeartbeats), &heartbeats); err != nil {
  109. return nil, err
  110. }
  111. result := make(map[string]time.Time)
  112. for deviceID, timeStr := range heartbeats {
  113. t, err := time.Parse(time.RFC3339, timeStr)
  114. if err != nil {
  115. return nil, err
  116. }
  117. result[deviceID] = t
  118. }
  119. return result, nil
  120. }
  121. // UpdateDeviceHeartbeat 更新设备心跳时间(每次验证时更新)
  122. func (l *License) UpdateDeviceHeartbeat(deviceID string) error {
  123. heartbeats, err := l.GetDeviceHeartbeats()
  124. if err != nil {
  125. heartbeats = make(map[string]time.Time)
  126. }
  127. // 更新心跳时间为当前时间
  128. heartbeats[deviceID] = time.Now()
  129. // 转换为字符串格式存储
  130. heartbeatsStr := make(map[string]string)
  131. for id, t := range heartbeats {
  132. heartbeatsStr[id] = t.Format(time.RFC3339)
  133. }
  134. data, err := json.Marshal(heartbeatsStr)
  135. if err != nil {
  136. return err
  137. }
  138. l.DeviceHeartbeats = string(data)
  139. return nil
  140. }
  141. // CanBindMoreDevices 检查是否可以绑定更多设备
  142. func (l *License) CanBindMoreDevices() (bool, error) {
  143. devices, err := l.GetBoundDeviceList()
  144. if err != nil {
  145. return false, err
  146. }
  147. return len(devices) < l.MaxDevices, nil
  148. }