fix: 使用配置等调整完毕;当前tqsdk使用失败

master
Lxy 3 months ago
parent d647773ff6
commit de5ad20375

@ -136,7 +136,7 @@ router.post('/save', async (req, res) => {
// 保存配置到文件 // 保存配置到文件
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const configPath = path.join(__dirname, '../../config.json'); const configPath = path.join(__dirname, '../../../config.json');
fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
@ -153,7 +153,7 @@ router.get('/get', async (req, res) => {
// 从文件读取配置 // 从文件读取配置
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const configPath = path.join(__dirname, '../../config.json'); const configPath = path.join(__dirname, '../../../config.json');
let config = {}; let config = {};
if (fs.existsSync(configPath)) { if (fs.existsSync(configPath)) {

@ -4,13 +4,44 @@ import path from 'path';
dotenv.config(); dotenv.config();
// 配置文件类型定义
interface ConfigFile {
server?: {
port?: number;
};
security?: {
jwtSecret?: string;
rateLimit?: {
windowMs?: number;
max?: number;
};
cors?: {
origin?: string;
methods?: string[];
allowedHeaders?: string[];
};
};
database?: {
postgreSQL?: {
host?: string;
port?: string;
username?: string;
password?: string;
database?: string;
};
};
dataSource?: any;
}
// 从文件读取配置 // 从文件读取配置
let fileConfig = {}; let fileConfig: ConfigFile = {};
try { try {
const configPath = path.join(__dirname, '../../../config.json'); const configPath = path.join(__dirname, '../../../config.json');
if (fs.existsSync(configPath)) { if (fs.existsSync(configPath)) {
const configData = fs.readFileSync(configPath, 'utf8'); const configData = fs.readFileSync(configPath, 'utf8');
fileConfig = JSON.parse(configData); fileConfig = JSON.parse(configData);
console.log('已读取配置文件:', configPath);
console.log('配置文件内容:', fileConfig);
} }
} catch (error) { } catch (error) {
console.error('读取配置文件失败:', error); console.error('读取配置文件失败:', error);
@ -43,38 +74,48 @@ export const config = {
methods: fileConfig.security?.cors?.methods || ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], methods: fileConfig.security?.cors?.methods || ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: fileConfig.security?.cors?.allowedHeaders || ['Content-Type', 'Authorization'] allowedHeaders: fileConfig.security?.cors?.allowedHeaders || ['Content-Type', 'Authorization']
}, },
dataSource: fileConfig.dataSource || { dataSource: (() => {
test: { const dataSourceConfig = fileConfig.dataSource || {
enabled: true, test: {
timeout: 10000, enabled: true,
retries: 3, timeout: 10000,
refreshInterval: 60000 retries: 3,
}, refreshInterval: 60000
tqsdk: { },
enabled: true, tqsdk: {
apiKey: '', enabled: true,
apiSecret: '', apiKey: '',
username: '', apiSecret: '',
password: '', username: '',
timeout: 30000, password: '',
retries: 3, timeout: 30000,
maxConnections: 5 retries: 3,
}, maxConnections: 5
wind: { },
enabled: false, wind: {
apiKey: '', enabled: false,
apiSecret: '', apiKey: '',
url: 'https://api.wind.com.cn', apiSecret: '',
timeout: 30000, url: 'https://api.wind.com.cn',
retries: 3 timeout: 30000,
}, retries: 3
sina: { },
enabled: false, sina: {
url: 'https://finance.sina.com.cn', enabled: false,
timeout: 10000, url: 'https://finance.sina.com.cn',
retries: 3, timeout: 10000,
refreshInterval: 60000 retries: 3,
}, refreshInterval: 60000
defaultDataSource: 'tqsdk' },
} defaultDataSource: 'tqsdk'
};
console.log('数据源配置初始化完成:', {
hasFileConfig: !!fileConfig.dataSource,
enabledDataSources: Object.keys(dataSourceConfig).filter(key =>
key !== 'defaultDataSource' && dataSourceConfig[key].enabled
),
defaultDataSource: dataSourceConfig.defaultDataSource
});
return dataSourceConfig;
})()
}; };

@ -16,6 +16,7 @@ export class DataSourceFactory {
// 获取数据源实例 // 获取数据源实例
static async getDataSource(type: DataSourceType = DataSourceType.TQSDK, config: any = {}): Promise<DataSource> { static async getDataSource(type: DataSourceType = DataSourceType.TQSDK, config: any = {}): Promise<DataSource> {
console.log('获取数据源实例:', type);
if (!this.dataSources.has(type)) { if (!this.dataSources.has(type)) {
let dataSource: DataSource; let dataSource: DataSource;

@ -1,9 +1,10 @@
// TQSDK数据源实现 // TQSDK数据源实现
import { DataSource } from './DataSource'; import { DataSource } from './DataSource';
import { TqSdk, TqAccount } from 'tqsdk'; import TqSdk from 'tqsdk';
import TqAccount from 'tqsdk';
export class TQDataSource implements DataSource { export class TQDataSource implements DataSource {
private tq: TqSdk | null = null; private tq: any = null;
private initialized: boolean = false; private initialized: boolean = false;
private config: { private config: {
username?: string; username?: string;
@ -174,7 +175,9 @@ export class TQDataSource implements DataSource {
async close(): Promise<void> { async close(): Promise<void> {
if (this.tq) { if (this.tq) {
this.tq.close(); if (typeof this.tq.close === 'function') {
this.tq.close();
}
this.tq = null; this.tq = null;
this.initialized = false; this.initialized = false;
console.log('TQSDK数据源已关闭'); console.log('TQSDK数据源已关闭');

@ -14,7 +14,7 @@ export const fetchMarketOverview = async () => {
try { try {
// 获取数据源配置 // 获取数据源配置
const dataSourceConfig = getDataSourceConfig(); const dataSourceConfig = getDataSourceConfig();
console.log('获取数据源配置:', dataSourceConfig);
// 检查是否有可用的数据源 // 检查是否有可用的数据源
const hasAvailableDataSource = dataSourceConfig.tqsdk?.enabled || dataSourceConfig.test?.enabled; const hasAvailableDataSource = dataSourceConfig.tqsdk?.enabled || dataSourceConfig.test?.enabled;
if (!hasAvailableDataSource) { if (!hasAvailableDataSource) {
@ -43,7 +43,7 @@ export const fetchMarketOverview = async () => {
winRate: Math.floor(Math.random() * 50) + 30, // 模拟胜率 winRate: Math.floor(Math.random() * 50) + 30, // 模拟胜率
atr: +(Math.random() * 5 + 0.5).toFixed(2), // 模拟ATR atr: +(Math.random() * 5 + 0.5).toFixed(2), // 模拟ATR
adx: Math.floor(Math.random() * 60) + 10, // 模拟ADX adx: Math.floor(Math.random() * 60) + 10, // 模拟ADX
adxStatus: adx => { adxStatus: (adx: number) => {
if (adx < 20) return '无趋势/震荡'; if (adx < 20) return '无趋势/震荡';
if (adx < 40) return '弱趋势'; if (adx < 40) return '弱趋势';
return '强趋势'; return '强趋势';
@ -156,7 +156,7 @@ export const fetchMarketDetail = async (symbol: string) => {
winRate: Math.floor(Math.random() * 50) + 30, winRate: Math.floor(Math.random() * 50) + 30,
atr: +(Math.random() * 5 + 0.5).toFixed(2), atr: +(Math.random() * 5 + 0.5).toFixed(2),
adx: Math.floor(Math.random() * 60) + 10, adx: Math.floor(Math.random() * 60) + 10,
adxStatus: adx => { adxStatus: (adx: number) => {
if (adx < 20) return '无趋势/震荡'; if (adx < 20) return '无趋势/震荡';
if (adx < 40) return '弱趋势'; if (adx < 40) return '弱趋势';
return '强趋势'; return '强趋势';

Loading…
Cancel
Save