mirror of
https://gitee.com/qqtt-ah/ai-crm.git
synced 2026-11-24 23:49:20 +08:00
init
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
Post,
|
||||
Query,
|
||||
Res,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { GenerateLicenseDto, LicenseQueryDto } from './license.dto';
|
||||
import { LicenseService } from './license.service';
|
||||
|
||||
@Controller('licenses')
|
||||
export class LicenseController {
|
||||
constructor(private readonly licenseService: LicenseService) {}
|
||||
|
||||
/** 在线吊销状态查询(主应用调用,无需管理员 JWT) */
|
||||
@Get('status')
|
||||
status(@Query('jti') jti: string) {
|
||||
return this.licenseService.getStatusByJti(jti);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
list(@Query() query: LicenseQueryDto) {
|
||||
return this.licenseService.list(query);
|
||||
}
|
||||
|
||||
@Post('generate')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
generate(@Body() dto: GenerateLicenseDto) {
|
||||
return this.licenseService.generate(dto);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
findOne(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.licenseService.findOne(id);
|
||||
}
|
||||
|
||||
@Post(':id/revoke')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
revoke(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.licenseService.revoke(id);
|
||||
}
|
||||
|
||||
@Get(':id/download')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
async download(@Param('id', ParseIntPipe) id: number, @Res() res: Response) {
|
||||
const file = await this.licenseService.download(id);
|
||||
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="${file.filename}"`);
|
||||
res.send(file.content);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import {
|
||||
IsArray,
|
||||
IsDateString,
|
||||
IsInt,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Min,
|
||||
} from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class GenerateLicenseDto {
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
customerId!: number;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
company!: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
machineFingerprint!: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
edition!: string;
|
||||
|
||||
@IsDateString()
|
||||
expiresAt!: string;
|
||||
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
features!: string[];
|
||||
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@IsOptional()
|
||||
maxCompanies?: number;
|
||||
}
|
||||
|
||||
export class LicenseQueryDto {
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@IsOptional()
|
||||
page?: number = 1;
|
||||
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@IsOptional()
|
||||
size?: number = 20;
|
||||
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
customerId?: number;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
status?: string;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Module, forwardRef } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { JwtSignerService } from '../../common/jwt-signer.service';
|
||||
import { LcLicense } from '../../entities/lc-license.entity';
|
||||
import { CustomerModule } from '../customer/customer.module';
|
||||
import { LicenseController } from './license.controller';
|
||||
import { LicenseService } from './license.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([LcLicense]),
|
||||
forwardRef(() => CustomerModule),
|
||||
],
|
||||
controllers: [LicenseController],
|
||||
providers: [LicenseService, JwtSignerService],
|
||||
exports: [LicenseService, JwtSignerService],
|
||||
})
|
||||
export class LicenseModule {}
|
||||
@@ -0,0 +1,126 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user