mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-10-13 21:09:19 +00:00
feat:添加文案查询功能
This commit is contained in:
parent
426fb886ae
commit
2d3d11be88
@ -89,4 +89,31 @@ export class WordsController {
|
||||
throw new HttpException('服务器错误', HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@Post('list')
|
||||
@ApiOperation({ summary: '获取指定类型下的所有文案名称' })
|
||||
@ApiBody({
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
type: { type: 'string', example: 'poke' },
|
||||
},
|
||||
required: ['type'],
|
||||
},
|
||||
})
|
||||
public async listWords(@Body('type') type: string) {
|
||||
try {
|
||||
const names = await this.wordsService.listWordNames(type);
|
||||
if (names.length === 0) {
|
||||
throw new HttpException(
|
||||
`类型 ${type} 下没有可用文案或目录不存在..`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
return names;
|
||||
} catch (e) {
|
||||
this.logger.error(`listWords 失败: ${e}`);
|
||||
throw new HttpException('服务器错误', HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +59,9 @@ export class WordsService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 从本地加载文案到内存
|
||||
* 加载文案
|
||||
* @param type 文案类型
|
||||
* @param name 文案名称
|
||||
*/
|
||||
public async loadWord(type: string, name: string): Promise<string[] | null> {
|
||||
const safeType = this.safePathSegment(type);
|
||||
@ -91,7 +93,9 @@ export class WordsService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载文案
|
||||
* 重载某条文案
|
||||
* @param type 文案类型
|
||||
* @param name 文案名称
|
||||
*/
|
||||
public async reloadWord(type: string, name: string): Promise<boolean> {
|
||||
const safeType = this.safePathSegment(type);
|
||||
@ -119,6 +123,27 @@ export class WordsService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文案名称数组
|
||||
* @param type 文案类型
|
||||
*/
|
||||
public async listWordNames(type: string): Promise<string[]> {
|
||||
const safeType = this.safePathSegment(type);
|
||||
const dirPath = path.join(this.paths.get('words'), safeType);
|
||||
|
||||
try {
|
||||
const files = await fs.readdir(dirPath, { withFileTypes: true });
|
||||
const names = files
|
||||
.filter((f) => f.isFile() && f.name.endsWith('.json'))
|
||||
.map((f) => f.name.replace(/\.json$/, ''));
|
||||
this.logger.log(`扫描文案类型 ${safeType} 下的文件: ${names.join(', ')}`);
|
||||
return names;
|
||||
} catch (e) {
|
||||
this.logger.error(`读取文案目录失败: ${safeType}`, e);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private safePathSegment(segment: string): string {
|
||||
// 将不安全字符转义为安全文件名形式
|
||||
return segment.replace(/[\\\/:*?"<>|]/g, '_');
|
||||
|
Loading…
x
Reference in New Issue
Block a user