refactor: 调整系统初始化模块位置

This commit is contained in:
2026-07-28 13:40:44 +08:00
parent d719a0ca3f
commit 095efa4e64
41 changed files with 941 additions and 381 deletions
+1
View File
@@ -41,6 +41,7 @@ CREATE TABLE IF NOT EXISTS lc_license (
expires_at TIMESTAMP NOT NULL,
revoked_at TIMESTAMP NULL,
max_activations INT NULL,
max_companies INT NULL COMMENT '允许创建的最大公司数量,NULL 表示使用版本默认值',
activation_count INT NOT NULL DEFAULT 0,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
@@ -11,6 +11,7 @@ export interface SignLicenseInput {
edition: string;
features: string[];
expiresAt: Date;
maxCompanies?: number;
}
export interface SignLicenseResult {
@@ -43,12 +44,16 @@ export class JwtSignerService implements OnModuleInit {
}
const jti = uuidv4();
const issuedAt = new Date();
const licenseKey = await new SignJWT({
const claims: Record<string, unknown> = {
company: input.company,
machineFingerprint: input.machineFingerprint,
edition: input.edition,
features: input.features,
})
};
if (input.maxCompanies != null && input.maxCompanies > 0) {
claims.maxCompanies = input.maxCompanies;
}
const licenseKey = await new SignJWT(claims)
.setProtectedHeader({ alg: 'EdDSA', typ: 'JWT', kid: this.kid })
.setIssuer('ecom-license-server')
.setSubject(input.sub)
@@ -51,6 +51,9 @@ export class LcLicense {
@Column({ name: 'max_activations', type: 'int', nullable: true })
maxActivations!: number | null;
@Column({ name: 'max_companies', type: 'int', nullable: true })
maxCompanies!: number | null;
@Column({ name: 'activation_count', type: 'int', default: 0 })
activationCount!: number;
@@ -32,6 +32,12 @@ export class GenerateLicenseDto {
@IsArray()
@IsString({ each: true })
features!: string[];
@Type(() => Number)
@IsInt()
@Min(1)
@IsOptional()
maxCompanies?: number;
}
export class LicenseQueryDto {
@@ -62,6 +62,7 @@ export class LicenseService {
edition: dto.edition,
features: dto.features,
expiresAt,
maxCompanies: dto.maxCompanies,
});
const license = this.licenseRepo.create({
customerId: customer.id,
@@ -74,6 +75,7 @@ export class LicenseService {
issuedAt: signed.issuedAt,
expiresAt: signed.expiresAt,
activationCount: 0,
maxCompanies: dto.maxCompanies ?? null,
});
const saved = await this.licenseRepo.save(license);
return {
@@ -82,6 +84,7 @@ export class LicenseService {
jti: saved.jti,
edition: saved.edition,
machineFingerprint: saved.machineFingerprint,
maxCompanies: saved.maxCompanies,
expiresAt: saved.expiresAt,
status: saved.status,
};
+12
View File
@@ -37,6 +37,7 @@ interface GenerateResult {
jti: string;
edition: string;
machineFingerprint: string;
maxCompanies: number | null;
expiresAt: string;
status: string;
}
@@ -114,6 +115,14 @@ async function licenseGenerate() {
validate: (v) => v.startsWith('FP-') || v === '*' || '格式应为 FP-...',
});
const defaultMaxCompanies = fingerprint === '*' ? 100 : 2;
const maxCompaniesInput = await input({
message: `最大公司数量(默认 ${defaultMaxCompanies})`,
default: String(defaultMaxCompanies),
validate: (v) => /^\d+$/.test(v) || '请输入正整数',
});
const maxCompanies = Number(maxCompaniesInput);
const edition = await select({
message: '版本类型',
choices: [
@@ -157,6 +166,7 @@ async function licenseGenerate() {
`客户 ID: ${customerId}`,
`公司: ${company}`,
`指纹: ${fingerprint}`,
`最大公司数: ${maxCompanies}`,
`版本: ${edition}`,
`过期: ${expiresAt}`,
`功能: ${features.join(', ')}`,
@@ -171,6 +181,7 @@ async function licenseGenerate() {
edition,
expiresAt: new Date(expiresAt).toISOString(),
features,
maxCompanies,
});
printBox(
@@ -178,6 +189,7 @@ async function licenseGenerate() {
[
`ID: ${result.id}`,
`JTI: ${result.jti}`,
`最大公司数: ${result.maxCompanies ?? (fingerprint === '*' ? 100 : 2)}`,
`过期: ${result.expiresAt?.slice(0, 10)}`,
'',
result.licenseKey,