feat(backend): license_devices 表 + 注册自动签发 trial (21B+21C)
21B — DB & model: - licenses: 扩展 license_key 为 2048 字节(容纳 Ed25519 JWT), 新增 max_devices INT DEFAULT 3,device_id/activated_at 标为 deprecated - 新增 license_devices 表(license_id+device_id 唯一索引) - model/license_device.go:LicenseDevice struct - main.go AutoMigrate 加入 LicenseDevice - testutil/setup.go 同步 SQLite DDL 21C — trial at register: - config: 新增 Ed25519PrivateKey 配置项(LICENSE_ED25519_PRIVATE_KEY 环境变量) - service/license.go: createTrialLicense(tx, shopID) — 签发 30d trial, 私钥未配置时静默跳过(开发/测试不影响) - service/auth.go: Register 事务末尾调用 createTrialLicense Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,12 +4,14 @@ import "time"
|
||||
|
||||
type License struct {
|
||||
Base
|
||||
ShopID uint64 `gorm:"not null;index" json:"shop_id"`
|
||||
LicenseKey string `gorm:"size:255;uniqueIndex" json:"license_key"`
|
||||
DeviceID string `gorm:"size:255" json:"device_id"`
|
||||
Type string `gorm:"type:enum('trial','monthly','annual','lifetime');default:'trial'" json:"type"`
|
||||
ExpiresAt *time.Time `json:"expires_at"`
|
||||
IsActive bool `gorm:"default:true" json:"is_active"`
|
||||
Features JSON `gorm:"type:json" json:"features,omitempty"`
|
||||
ActivatedAt *time.Time `json:"activated_at"`
|
||||
ShopID uint64 `gorm:"not null;index" json:"shop_id"`
|
||||
LicenseKey string `gorm:"size:2048;uniqueIndex" json:"license_key"`
|
||||
Type string `gorm:"type:enum('trial','monthly','annual','lifetime');default:'trial'" json:"type"`
|
||||
ExpiresAt *time.Time `json:"expires_at"`
|
||||
IsActive bool `gorm:"default:true" json:"is_active"`
|
||||
MaxDevices int `gorm:"default:3" json:"max_devices"`
|
||||
Features JSON `gorm:"type:json" json:"features,omitempty"`
|
||||
// Deprecated: device binding is now tracked in license_devices table.
|
||||
DeviceID string `gorm:"size:255" json:"device_id,omitempty"`
|
||||
ActivatedAt *time.Time `json:"activated_at,omitempty"`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
type LicenseDevice struct {
|
||||
ID uint64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
LicenseID uint64 `gorm:"not null;index" json:"license_id"`
|
||||
ShopID uint64 `gorm:"not null;index" json:"shop_id"`
|
||||
DeviceID string `gorm:"size:255;not null" json:"device_id"`
|
||||
DeviceName string `gorm:"size:255" json:"device_name"`
|
||||
Platform string `gorm:"size:50" json:"platform"` // windows|macos|android|ios|web
|
||||
ActivatedAt time.Time `gorm:"not null;autoCreateTime" json:"activated_at"`
|
||||
LastSeenAt time.Time `gorm:"not null;autoUpdateTime" json:"last_seen_at"`
|
||||
}
|
||||
|
||||
func (LicenseDevice) TableName() string { return "license_devices" }
|
||||
@@ -123,6 +123,8 @@ func (s *AuthService) Register(in RegisterInput) (*RegisterResult, error) {
|
||||
return err
|
||||
}
|
||||
|
||||
createTrialLicense(tx, shop.ID)
|
||||
|
||||
result = RegisterResult{
|
||||
ShopCode: shop.Code,
|
||||
ShopName: shop.Name,
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/base32"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
|
||||
"github.com/wangjia/jiu/backend/config"
|
||||
"github.com/wangjia/jiu/backend/internal/model"
|
||||
"github.com/wangjia/jiu/backend/internal/util"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -98,3 +100,40 @@ func (s *LicenseService) Deactivate(shopID uint64, deviceID string) error {
|
||||
Where("shop_id = ? AND device_id = ?", shopID, deviceID).
|
||||
Updates(map[string]interface{}{"device_id": "", "activated_at": nil}).Error
|
||||
}
|
||||
|
||||
// createTrialLicense 在注册事务中为新门店签发 30 天 trial license。
|
||||
// 若私钥未配置则静默跳过(开发/测试环境可不配置私钥)。
|
||||
func createTrialLicense(tx *gorm.DB, shopID uint64) {
|
||||
privKey := config.C.License.Ed25519PrivateKey
|
||||
if privKey == "" {
|
||||
log.Printf("[license] Ed25519 private key not configured, skipping trial for shop %d", shopID)
|
||||
return
|
||||
}
|
||||
|
||||
expiresAt := time.Now().Add(30 * 24 * time.Hour)
|
||||
expiresUnix := expiresAt.Unix()
|
||||
payload := util.LicensePayload{
|
||||
ShopID: shopID,
|
||||
Type: "trial",
|
||||
IssuedAt: time.Now().Unix(),
|
||||
ExpiresAt: &expiresUnix,
|
||||
MaxDevices: 1,
|
||||
}
|
||||
token, err := util.IssueLicenseToken(payload, privKey)
|
||||
if err != nil {
|
||||
log.Printf("[license] failed to issue trial token for shop %d: %v", shopID, err)
|
||||
return
|
||||
}
|
||||
|
||||
lic := model.License{
|
||||
ShopID: shopID,
|
||||
LicenseKey: token,
|
||||
Type: "trial",
|
||||
ExpiresAt: &expiresAt,
|
||||
IsActive: true,
|
||||
MaxDevices: 1,
|
||||
}
|
||||
if err := tx.Create(&lic).Error; err != nil {
|
||||
log.Printf("[license] failed to create trial license for shop %d: %v", shopID, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user