mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-07-04 14:49:19 +00:00
增加获取botid[]路由
This commit is contained in:
parent
56047155c3
commit
1efa0eec5b
@ -7,6 +7,7 @@ import config from './utils/core/config';
|
|||||||
import './services/ws/wsServer';
|
import './services/ws/wsServer';
|
||||||
import compression from 'compression';
|
import compression from 'compression';
|
||||||
import testController from './modules/test/test.controller';
|
import testController from './modules/test/test.controller';
|
||||||
|
import BotController from './modules/bot/bot.controller';
|
||||||
|
|
||||||
const apps = {
|
const apps = {
|
||||||
async createApp() {
|
async createApp() {
|
||||||
@ -25,6 +26,7 @@ const apps = {
|
|||||||
{ path: '/api/sample', name: '测试模块', controller: sampleController },
|
{ path: '/api/sample', name: '测试模块', controller: sampleController },
|
||||||
{ path: '/public', name: '文件模块', controller: imageController },
|
{ path: '/public', name: '文件模块', controller: imageController },
|
||||||
{ path: '/test', name: '测试', controller: testController },
|
{ path: '/test', name: '测试', controller: testController },
|
||||||
|
{ path: '/api/bot', name: '寄气人模块', controller: BotController },
|
||||||
];
|
];
|
||||||
|
|
||||||
modules.forEach((module) => {
|
modules.forEach((module) => {
|
||||||
|
31
src/modules/bot/bot.controller.ts
Normal file
31
src/modules/bot/bot.controller.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import express from 'express';
|
||||||
|
import response from '../../utils/core/response';
|
||||||
|
import BotService from './bot.service';
|
||||||
|
|
||||||
|
class BotController {
|
||||||
|
private readonly router: express.Router;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.router = express.Router();
|
||||||
|
this.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public getRouter(): express.Router {
|
||||||
|
return this.router;
|
||||||
|
}
|
||||||
|
|
||||||
|
private init(): void {
|
||||||
|
this.router.post(`/getBotId`, this.postBotsId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private postBotsId = async (req: express.Request, res: express.Response): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const result = await BotService.getBotId();
|
||||||
|
await response.success(res, result);
|
||||||
|
} catch (err) {
|
||||||
|
await response.error(res, `请求失败..`, 500, err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default new BotController();
|
33
src/modules/bot/bot.service.ts
Normal file
33
src/modules/bot/bot.service.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import logger from '../../utils/core/logger';
|
||||||
|
import paths from '../../utils/core/path';
|
||||||
|
import fs from 'fs/promises';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
class BotService {
|
||||||
|
public async getBotId() {
|
||||||
|
logger.debug('GetBotId..');
|
||||||
|
const userPath = paths.get('userData');
|
||||||
|
const botsPath = path.join(userPath, '/crystelfBots');
|
||||||
|
const dirData = await fs.readdir(botsPath);
|
||||||
|
const uins: string[] = [];
|
||||||
|
|
||||||
|
for (const fileName of dirData) {
|
||||||
|
if (!fileName.endsWith('.json')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const filePath = path.join(botsPath, fileName);
|
||||||
|
const fileContent = await fs.readFile(filePath, 'utf-8');
|
||||||
|
const jsonData = JSON.parse(fileContent);
|
||||||
|
if (jsonData.uin) {
|
||||||
|
uins.push(jsonData.uin);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
logger.error(`读取或解析${fileName}出错`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return uins;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default new BotService();
|
@ -1,7 +1,7 @@
|
|||||||
import WebSocket from 'ws';
|
import WebSocket from 'ws';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
const WS_URL = 'ws://127.0.0.1:3001';
|
const WS_URL = 'ws://127.0.0.1:4001';
|
||||||
const WS_SECRET = '114514';
|
const WS_SECRET = '114514';
|
||||||
const CLIENT_ID = 'test';
|
const CLIENT_ID = 'test';
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ function createWebSocketClient() {
|
|||||||
|
|
||||||
async function testGetAPI() {
|
async function testGetAPI() {
|
||||||
try {
|
try {
|
||||||
const response = await axios.get('http://localhost:3000/api/sample/hello');
|
const response = await axios.get('http://localhost:4000/api/sample/hello');
|
||||||
console.log('[HTTP][GET] Response:', response.data);
|
console.log('[HTTP][GET] Response:', response.data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[HTTP][GET] Error:', err);
|
console.error('[HTTP][GET] Error:', err);
|
||||||
@ -48,9 +48,7 @@ async function testGetAPI() {
|
|||||||
|
|
||||||
async function testPostAPI() {
|
async function testPostAPI() {
|
||||||
try {
|
try {
|
||||||
const response = await axios.post('http://localhost:3000/api/sample/greet', {
|
const response = await axios.post('http://localhost:4000/api/bot/getBotId', {});
|
||||||
name: 'Jerry',
|
|
||||||
});
|
|
||||||
console.log('[HTTP][POST] Response:', response.data);
|
console.log('[HTTP][POST] Response:', response.data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[HTTP][POST] Error:', err);
|
console.error('[HTTP][POST] Error:', err);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user