koishi-plugin-stock 2.1.5 → 2.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -65,13 +65,17 @@ npm install koishi-plugin-stock
65
65
 
66
66
  ## 更新日志
67
67
 
68
+ ### v2.1.6 (2026-03-02)
69
+ - ⚡ **性能优化**:超时时间从15秒调整为10秒
70
+ - 🔁 **重试机制**:添加3次重试机制,使用指数退避策略(2s/4s/8s)
71
+ - 🔄 **统一处理**:所有API请求使用统一的重试函数
72
+ - 📊 **可观测性**:增强日志记录,便于问题排查
73
+
68
74
  ### v2.1.5 (2026-03-02)
69
75
  - 🚀 **性能优化**:增加API请求超时时间至15秒
70
76
  - 🔧 **错误处理增强**:添加详细的错误类型识别和针对性提示
71
77
  - 🔒 **安全性提升**:统一使用HTTPS协议替代HTTP
72
78
  - 📊 **用户体验改善**:区分超时错误与其他网络错误,提供更准确的错误信息
73
-
74
- ### v2.1.4 (2026-02-28)
75
79
  - 🎉 **新增心法**:添加第58条心法 "我能怎么办,我真的我,我,我真的没招了,我"
76
80
  - ✅ 心法总数更新为58条
77
81
  - 📦 保持所有功能稳定运行
@@ -2,6 +2,35 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StockCommands = void 0;
4
4
  const blacklist_1 = require("../utils/blacklist");
5
+ // 带重试机制的HTTP请求函数
6
+ async function httpRequestWithRetry(ctx, url, options, maxRetries = 3) {
7
+ const logger = ctx.logger('stock');
8
+ let lastError = new Error('Unknown error'); // 初始化错误对象
9
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
10
+ try {
11
+ logger.info(`第${attempt}次尝试请求: ${url}`);
12
+ const result = await ctx.http.get(url, {
13
+ ...options,
14
+ timeout: 10000 // 设置为10秒超时
15
+ });
16
+ logger.info(`第${attempt}次请求成功`);
17
+ return result;
18
+ }
19
+ catch (error) {
20
+ lastError = error;
21
+ logger.warn(`第${attempt}次请求失败:`, error.message);
22
+ // 如果不是最后一次尝试,等待一段时间后重试
23
+ if (attempt < maxRetries) {
24
+ const delay = Math.pow(2, attempt) * 1000; // 指数退避: 2s, 4s, 8s
25
+ logger.info(`等待${delay}ms后进行第${attempt + 1}次重试`);
26
+ await new Promise(resolve => setTimeout(resolve, delay));
27
+ }
28
+ }
29
+ }
30
+ // 所有重试都失败了
31
+ logger.error(`所有${maxRetries}次重试都失败了`);
32
+ throw lastError;
33
+ }
5
34
  class StockCommands {
6
35
  static register(ctx, config) {
7
36
  const logger = ctx.logger('stock');
@@ -12,10 +41,9 @@ class StockCommands {
12
41
  return;
13
42
  }
14
43
  try {
15
- // 增加重试机制和更长的超时时间
16
- const responseText = await ctx.http.get('https://stock.svip886.com/api/indexes', {
17
- responseType: 'text',
18
- timeout: 15000 // 增加超时时间到15秒
44
+ // 使用带重试机制的HTTP请求
45
+ const responseText = await httpRequestWithRetry(ctx, 'https://stock.svip886.com/api/indexes', {
46
+ responseType: 'text'
19
47
  });
20
48
  return `📊 指数看板:\n\n${responseText}`;
21
49
  }
@@ -40,10 +68,9 @@ class StockCommands {
40
68
  return '请输入股票代码,格式:异动 [股票代码]';
41
69
  }
42
70
  try {
43
- // 增加重试机制和更长的超时时间
44
- const responseText = await ctx.http.get(`https://stock.svip886.com/api/analyze?code=${stockCode}`, {
45
- responseType: 'text',
46
- timeout: 15000
71
+ // 使用带重试机制的HTTP请求
72
+ const responseText = await httpRequestWithRetry(ctx, `https://stock.svip886.com/api/analyze?code=${stockCode}`, {
73
+ responseType: 'text'
47
74
  });
48
75
  return `📈 股票 ${stockCode} 异动分析:\n\n${responseText}`;
49
76
  }
@@ -64,11 +91,10 @@ class StockCommands {
64
91
  return;
65
92
  }
66
93
  try {
67
- // 增加超时时间
94
+ // 使用带重试机制的HTTP请求获取图片
68
95
  const imageUrl = 'https://stock.svip886.com/api/limit_up.png';
69
- const imageBuffer = await ctx.http.get(imageUrl, {
70
- responseType: 'arraybuffer',
71
- timeout: 15000
96
+ const imageBuffer = await httpRequestWithRetry(ctx, imageUrl, {
97
+ responseType: 'arraybuffer'
72
98
  });
73
99
  const base64Image = Buffer.from(imageBuffer).toString('base64');
74
100
  return `<img src="data:image/png;base64,${base64Image}" />`;
@@ -90,11 +116,10 @@ class StockCommands {
90
116
  return;
91
117
  }
92
118
  try {
93
- // 增加超时时间
119
+ // 使用带重试机制的HTTP请求获取图片
94
120
  const imageUrl = 'https://stock.svip886.com/api/limit_down.png';
95
- const imageBuffer = await ctx.http.get(imageUrl, {
96
- responseType: 'arraybuffer',
97
- timeout: 15000
121
+ const imageBuffer = await httpRequestWithRetry(ctx, imageUrl, {
122
+ responseType: 'arraybuffer'
98
123
  });
99
124
  const base64Image = Buffer.from(imageBuffer).toString('base64');
100
125
  return `<img src="data:image/png;base64,${base64Image}" />`;
@@ -138,10 +163,9 @@ class StockCommands {
138
163
  return `不支持的选股策略: ${strategy}\n支持的策略:N型(1)、填坑(2)、少妇(3)、突破(4)、补票(5)、少妇pro(6)`;
139
164
  }
140
165
  try {
141
- // 增加重试机制和更长的超时时间
142
- const responseText = await ctx.http.get(`https://stock.svip886.com/api/dyq_${apiEndpoint}`, {
143
- responseType: 'text',
144
- timeout: 15000
166
+ // 使用带重试机制的HTTP请求
167
+ const responseText = await httpRequestWithRetry(ctx, `https://stock.svip886.com/api/dyq_${apiEndpoint}`, {
168
+ responseType: 'text'
145
169
  });
146
170
  return `🎯 选股结果 (${strategy}): \n\n${responseText}`;
147
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-stock",
3
- "version": "2.1.5",
3
+ "version": "2.1.6",
4
4
  "description": "A Koishi plugin that fetches stock data and provides market analysis, including active market cap, stock alerts, limit-up board, stock selection features, and heart method card drawing with configurable blacklists for each command.",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",