mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 04:29:21 +08:00
127 lines
3.9 KiB
TypeScript
127 lines
3.9 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { JwtSignerService } from '../../common/jwt-signer.service';
|
|
import { LcCustomer } from '../../entities/lc-customer.entity';
|
|
import { LcLicense } from '../../entities/lc-license.entity';
|
|
import { CustomerService } from '../customer/customer.service';
|
|
import { GenerateLicenseDto, LicenseQueryDto } from './license.dto';
|
|
|
|
@Injectable()
|
|
export class LicenseService {
|
|
constructor(
|
|
@InjectRepository(LcLicense)
|
|
private readonly licenseRepo: Repository<LcLicense>,
|
|
private readonly customerService: CustomerService,
|
|
private readonly jwtSigner: JwtSignerService,
|
|
) {}
|
|
|
|
async list(query: LicenseQueryDto) {
|
|
const page = query.page || 1;
|
|
const size = query.size || 20;
|
|
const qb = this.licenseRepo
|
|
.createQueryBuilder('l')
|
|
.leftJoinAndSelect('l.customer', 'c')
|
|
.orderBy('l.id', 'DESC')
|
|
.skip((page - 1) * size)
|
|
.take(size);
|
|
if (query.customerId) {
|
|
qb.andWhere('l.customer_id = :customerId', { customerId: query.customerId });
|
|
}
|
|
if (query.status) {
|
|
qb.andWhere('l.status = :status', { status: query.status });
|
|
}
|
|
const [items, total] = await qb.getManyAndCount();
|
|
return { items, total, page, size };
|
|
}
|
|
|
|
async findOne(id: number) {
|
|
const license = await this.licenseRepo.findOne({
|
|
where: { id },
|
|
relations: ['customer'],
|
|
});
|
|
if (!license) throw new NotFoundException('许可证不存在');
|
|
return license;
|
|
}
|
|
|
|
async listByCustomer(customerId: number) {
|
|
await this.customerService.findOne(customerId);
|
|
return this.licenseRepo.find({
|
|
where: { customerId },
|
|
order: { id: 'DESC' },
|
|
});
|
|
}
|
|
|
|
async generate(dto: GenerateLicenseDto) {
|
|
const customer = await this.customerService.findOne(dto.customerId);
|
|
const expiresAt = new Date(dto.expiresAt);
|
|
const signed = await this.jwtSigner.signLicense({
|
|
sub: customer.code,
|
|
company: dto.company,
|
|
machineFingerprint: dto.machineFingerprint,
|
|
edition: dto.edition,
|
|
features: dto.features,
|
|
expiresAt,
|
|
maxCompanies: dto.maxCompanies,
|
|
});
|
|
const license = this.licenseRepo.create({
|
|
customerId: customer.id,
|
|
licenseKey: signed.licenseKey,
|
|
jti: signed.jti,
|
|
edition: dto.edition,
|
|
machineFingerprint: dto.machineFingerprint,
|
|
features: dto.features,
|
|
status: 'active',
|
|
issuedAt: signed.issuedAt,
|
|
expiresAt: signed.expiresAt,
|
|
activationCount: 0,
|
|
maxCompanies: dto.maxCompanies ?? null,
|
|
});
|
|
const saved = await this.licenseRepo.save(license);
|
|
return {
|
|
id: saved.id,
|
|
licenseKey: saved.licenseKey,
|
|
jti: saved.jti,
|
|
edition: saved.edition,
|
|
machineFingerprint: saved.machineFingerprint,
|
|
maxCompanies: saved.maxCompanies,
|
|
expiresAt: saved.expiresAt,
|
|
status: saved.status,
|
|
};
|
|
}
|
|
|
|
async revoke(id: number) {
|
|
const license = await this.findOne(id);
|
|
license.status = 'revoked';
|
|
license.revokedAt = new Date();
|
|
await this.licenseRepo.save(license);
|
|
return { id: license.id, jti: license.jti, status: license.status, revokedAt: license.revokedAt };
|
|
}
|
|
|
|
/** 在线吊销查询(主应用可选同步) */
|
|
async getStatusByJti(jti: string) {
|
|
const license = await this.licenseRepo.findOne({ where: { jti } });
|
|
if (!license) {
|
|
throw new NotFoundException('许可证不存在');
|
|
}
|
|
// 过期自动标记
|
|
if (license.status === 'active' && license.expiresAt < new Date()) {
|
|
license.status = 'expired';
|
|
await this.licenseRepo.save(license);
|
|
}
|
|
return {
|
|
jti: license.jti,
|
|
status: license.status,
|
|
revokedAt: license.revokedAt,
|
|
};
|
|
}
|
|
|
|
async download(id: number) {
|
|
const license = await this.findOne(id);
|
|
return {
|
|
filename: `license-${license.jti}.key`,
|
|
content: license.licenseKey,
|
|
};
|
|
}
|
|
}
|