license.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. Key string `gorm:"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. MaxDevices int `gorm:"default:2" json:"max_devices"`
  13. CreatedAt time.Time `json:"created_at"`
  14. UpdatedAt time.Time `json:"updated_at"`
  15. }
  16. // GetBoundDeviceList 获取已绑定设备ID列表
  17. func (l *License) GetBoundDeviceList() ([]string, error) {
  18. if l.BoundDevices == "" {
  19. return []string{}, nil
  20. }
  21. var devices []string
  22. err := json.Unmarshal([]byte(l.BoundDevices), &devices)
  23. return devices, err
  24. }
  25. // SetBoundDeviceList 设置已绑定设备ID列表
  26. func (l *License) SetBoundDeviceList(devices []string) error {
  27. data, err := json.Marshal(devices)
  28. if err != nil {
  29. return err
  30. }
  31. l.BoundDevices = string(data)
  32. return nil
  33. }
  34. // IsDeviceBound 检查设备是否已绑定
  35. func (l *License) IsDeviceBound(deviceID string) (bool, error) {
  36. devices, err := l.GetBoundDeviceList()
  37. if err != nil {
  38. return false, err
  39. }
  40. for _, d := range devices {
  41. if d == deviceID {
  42. return true, nil
  43. }
  44. }
  45. return false, nil
  46. }
  47. // AddDevice 添加设备到绑定列表
  48. func (l *License) AddDevice(deviceID string) error {
  49. devices, err := l.GetBoundDeviceList()
  50. if err != nil {
  51. return err
  52. }
  53. devices = append(devices, deviceID)
  54. if err := l.SetBoundDeviceList(devices); err != nil {
  55. return err
  56. }
  57. // 记录设备激活时间
  58. return l.RecordDeviceActivation(deviceID)
  59. }
  60. // GetDeviceActivations 获取设备激活时间映射
  61. func (l *License) GetDeviceActivations() (map[string]time.Time, error) {
  62. if l.DeviceActivations == "" {
  63. return make(map[string]time.Time), nil
  64. }
  65. var activations map[string]string
  66. if err := json.Unmarshal([]byte(l.DeviceActivations), &activations); err != nil {
  67. return nil, err
  68. }
  69. result := make(map[string]time.Time)
  70. for deviceID, timeStr := range activations {
  71. t, err := time.Parse(time.RFC3339, timeStr)
  72. if err != nil {
  73. return nil, err
  74. }
  75. result[deviceID] = t
  76. }
  77. return result, nil
  78. }
  79. // RecordDeviceActivation 记录设备激活时间
  80. func (l *License) RecordDeviceActivation(deviceID string) error {
  81. activations, err := l.GetDeviceActivations()
  82. if err != nil {
  83. activations = make(map[string]time.Time)
  84. }
  85. // 如果设备已存在,不更新激活时间;如果不存在,记录当前时间
  86. if _, exists := activations[deviceID]; !exists {
  87. activations[deviceID] = time.Now()
  88. }
  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. return nil
  100. }
  101. // CanBindMoreDevices 检查是否可以绑定更多设备
  102. func (l *License) CanBindMoreDevices() (bool, error) {
  103. devices, err := l.GetBoundDeviceList()
  104. if err != nil {
  105. return false, err
  106. }
  107. return len(devices) < l.MaxDevices, nil
  108. }