文案模块

This commit is contained in:
Jerry 2025-08-25 22:42:14 +08:00
parent 6809d07bcf
commit e775bcdf77
7 changed files with 176 additions and 13 deletions

View File

@ -11,6 +11,7 @@ import { WsModule } from './core/ws/ws.module';
import { SystemWebModule } from './modules/system/systemWeb.module';
import { BotModule } from './modules/bot/bot.module';
import { CdnModule } from './modules/cdn/cdn.module';
import { WordsModule } from './modules/words/words.module';
@Module({
imports: [
@ -26,6 +27,7 @@ import { CdnModule } from './modules/cdn/cdn.module';
SystemWebModule,
BotModule,
CdnModule,
WordsModule,
],
})
export class AppModule {}

View File

@ -10,7 +10,14 @@ import { WsAdapter } from '@nestjs/platform-ws';
async function bootstrap() {
Logger.log('晶灵核心初始化..');
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api');
app.setGlobalPrefix('api', {
exclude: [
'cdn',
{ path: 'cdn/(.*)', method: RequestMethod.ALL },
'public',
{ path: 'public/(.*)', method: RequestMethod.ALL },
],
});
app.useGlobalInterceptors(new ResponseInterceptor());
app.useGlobalFilters(new AllExceptionsFilter());
const systemService = app.get(SystemService);

View File

@ -55,13 +55,13 @@ export class CdnController {
description: '由晶灵资源分发服务器(CDN)提供支持',
})
async getFile(@Res() res: Response, @Req() req: Request) {
const relativePath = req.url.replace('/api/cdn/', ''); //params.path;
const relativePath = req.url.replace('/cdn/', ''); //params.path;
return this.deliverFile(relativePath, res);
}
@Get('public/files/*')
async fromPublicFiles(@Res() res: Response, @Req() req: Request) {
const relativePath = req.url.replace('/api/public/files/', '');
const relativePath = req.url.replace('/public/files/', '');
this.logger.debug(
`请求 /public/files/${relativePath} → 代理到 /cdn/${relativePath}`,
);
@ -70,7 +70,7 @@ export class CdnController {
@Get('public/cdn/*')
async fromPublicCdn(@Req() req: Request, @Res() res: Response) {
const relativePath = req.url.replace('/api/public/cdn/', '');
const relativePath = req.url.replace('/public/cdn/', '');
this.logger.debug(
`请求 /public/cdn/${relativePath} → 代理到 /cdn/${relativePath}`,
);

View File

@ -1,12 +1,4 @@
import {
Controller,
Post,
Body,
UnauthorizedException,
Inject,
UseGuards,
Param,
} from '@nestjs/common';
import { Controller, Post, Inject, UseGuards, Param } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBody, ApiProperty } from '@nestjs/swagger';
import { SystemWebService } from './systemWeb.service';
import { ToolsService } from '../../core/tools/tools.service';

View File

@ -0,0 +1,83 @@
import {
Controller,
Get,
Param,
Post,
HttpException,
HttpStatus,
Logger,
Inject,
UseGuards,
} from '@nestjs/common';
import { WordsService } from './words.service';
import { TokenAuthGuard } from '../../core/tools/token-auth.guard';
import { ApiBody, ApiOperation, ApiProperty } from '@nestjs/swagger';
class WordsDto {
@ApiProperty({
description: '文案id',
example: 'poke',
})
id: string;
@ApiProperty({
description: '密钥',
example: '1111',
})
token: string;
}
@Controller('words')
export class WordsController {
private readonly logger = new Logger(WordsController.name);
constructor(
@Inject(WordsService) private readonly wordsService: WordsService,
) {}
/**
*
*/
@Get('getText/:id')
@ApiOperation({
summary: '获取随机文案',
})
async getText(@Param('id') id: string) {
try {
const texts = await this.wordsService.loadWordById(id);
if (!texts || texts.length === 0) {
throw new HttpException(
`文案 ${id} 不存在或为空..`,
HttpStatus.NOT_FOUND,
);
}
const randomIndex = Math.floor(Math.random() * texts.length);
return { success: true, data: texts[randomIndex] };
} catch (e) {
this.logger.error(`getText 失败: ${e?.message}`);
throw new HttpException('服务器错误', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
*
*/
@Post('reloadText/:id')
@ApiOperation({
summary: '重载某条文案',
})
@UseGuards(TokenAuthGuard)
@ApiBody({ type: WordsDto })
async reloadWord(@Param('id') id: string, @Param('token') token: string) {
try {
const success = await this.wordsService.reloadWord(id);
if (success) {
return '成功重载..';
} else {
throw new HttpException('重载失败..', HttpStatus.BAD_REQUEST);
}
} catch (e) {
this.logger.error(`reloadWord 失败: ${e?.message}`);
throw new HttpException('服务器错误', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

View File

@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { WordsController } from './words.controller';
import { WordsService } from './words.service';
import { PathModule } from '../../core/path/path.module';
import { ToolsModule } from '../../core/tools/tools.module';
@Module({
imports: [PathModule, ToolsModule],
controllers: [WordsController],
providers: [WordsService],
exports: [WordsService],
})
export class WordsModule {}

View File

@ -0,0 +1,66 @@
import { Inject, Injectable, Logger } from '@nestjs/common';
import * as path from 'path';
import * as fs from 'fs/promises';
import { PathService } from '../../core/path/path.service';
@Injectable()
export class WordsService {
private readonly logger = new Logger(WordsService.name);
private wordCache: Record<string, string[]> = {};
private readonly clearIntervalMs = 30 * 60 * 1000; // 30min
@Inject(PathService)
private readonly paths: PathService;
constructor() {
this.startAutoClear();
}
private startAutoClear() {
setInterval(() => {
this.logger.log('清理文案缓存..');
this.wordCache = {};
}, this.clearIntervalMs);
}
/**
* json &
*/
async loadWordById(id: string): Promise<string[] | null> {
this.logger.log(`Loading words ${id}..`);
if (this.wordCache[id]) return this.wordCache[id];
const filePath = path.join(this.paths.get('words'), `${id}.json`);
try {
const content = await fs.readFile(filePath, 'utf-8');
const parsed = JSON.parse(content);
if (Array.isArray(parsed)) {
const texts = parsed.filter((item) => typeof item === 'string');
this.wordCache[id] = texts;
return texts;
}
return null;
} catch (e) {
this.logger.error(`加载文案失败: ${id}..`, e);
return null;
}
}
/**
* json
*/
async reloadWord(id: string): Promise<boolean> {
this.logger.log(`重载文案: ${id}..`);
const filePath = path.join(this.paths.get('words'), `${id}.json`);
try {
const content = await fs.readFile(filePath, 'utf-8');
const parsed = JSON.parse(content);
if (Array.isArray(parsed)) {
this.wordCache[id] = parsed.filter((item) => typeof item === 'string');
return true;
}
return false;
} catch (e) {
this.logger.error(`重载文案失败: ${id}`, e);
return false;
}
}
}