feat(backend): 授权改为时长兑换券体系 + 退役 ed25519/HMAC + 平台生码工具

- 新增 license_codes 码池表 + model.LicenseCode;licenses 加 tier 档位列
- LicenseService.Redeem:单事务 FOR UPDATE 校验码未用 → 时长叠加(可叠加,0=永久)
  → 写 type/tier/max_devices → 绑设备(超限整笔回滚) → 标记已用 → 即时失效 phase 缓存
  路由仍 POST /license/activate,客户端零破坏
- util.GenerateRedeemCode/NormalizeCode:JIUKU-XXXX-XXXX 短码(crypto/rand)
- cmd/gencode:平台批量生成兑换码并落库;删除 cmd/issue、cmd/genkey
- 退役 ed25519 + HMAC:删 util/license_key、GenerateKey、License 全部 config 字段
  及生产启动私钥校验;trial 改直接建行(无需私钥、去 Fatal)
- tier 档位钩子默认 standard,分档消费模式后续设计
- 测试:Redeem 全场景(叠加/过期重置/永久/一码一次/无效/设备上限回滚)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-06-19 12:14:27 +08:00
parent 914e2fb533
commit 23dff69c62
20 changed files with 541 additions and 642 deletions
+17 -3
View File
@@ -27,9 +27,6 @@ func InitConfig() {
AccessExpireMin: 60,
RefreshExpireH: 168,
},
License: config.LicenseConfig{
HMACSecret: "test-license-hmac-secret",
},
Session: config.SessionConfig{
LimitDesktop: 2,
LimitMobile: 2,
@@ -125,12 +122,29 @@ func SetupTestDB() *gorm.DB {
license_key TEXT UNIQUE,
device_id TEXT,
type TEXT DEFAULT 'trial',
tier TEXT DEFAULT 'standard',
expires_at DATETIME,
is_active INTEGER DEFAULT 1,
max_devices INTEGER DEFAULT 3,
features TEXT,
activated_at DATETIME
)`,
`CREATE TABLE IF NOT EXISTS license_codes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
code TEXT UNIQUE NOT NULL,
type TEXT DEFAULT 'annual',
tier TEXT DEFAULT 'standard',
duration_days INTEGER NOT NULL DEFAULT 0,
max_devices INTEGER NOT NULL DEFAULT 0,
status TEXT NOT NULL DEFAULT 'unused',
redeemed_shop_id INTEGER,
redeemed_at DATETIME,
redeemed_device_id TEXT,
batch TEXT,
note TEXT,
created_at DATETIME,
updated_at DATETIME
)`,
`CREATE TABLE IF NOT EXISTS license_devices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
license_id INTEGER NOT NULL,