* 批量刷新即将过期的令牌 * @param {Array} accounts - 账户列表 * @param {number} thresholdHours - 过期阈值(小时) * @param {Function} onEachRefresh - 每次刷新成功后的回调函数 (updatedAccount, index, total) => void * @returns {Promise } 刷新结果 {refreshed: Array, failed: Array}
(accounts, thresholdHours = 24, onEachRefresh = null)
| 164 | */ |
| 165 | async batchRefreshTokens(accounts, thresholdHours = 24, onEachRefresh = null) { |
| 166 | const needsRefresh = accounts.filter(account => |
| 167 | this.isTokenExpiringSoon(account.token, thresholdHours) |
| 168 | ) |
| 169 | |
| 170 | if (needsRefresh.length === 0) { |
| 171 | logger.info('没有需要刷新的令牌', 'TOKEN') |
| 172 | return { refreshed: [], failed: [] } |
| 173 | } |
| 174 | |
| 175 | logger.info(`发现 ${needsRefresh.length} 个令牌需要刷新`, 'TOKEN') |
| 176 | |
| 177 | const refreshed = [] |
| 178 | const failed = [] |
| 179 | |
| 180 | for (let i = 0; i < needsRefresh.length; i++) { |
| 181 | const account = needsRefresh[i] |
| 182 | const updatedAccount = await this.refreshToken(account) |
| 183 | |
| 184 | if (updatedAccount) { |
| 185 | refreshed.push(updatedAccount) |
| 186 | |
| 187 | // 如果提供了回调函数,立即调用 |
| 188 | if (onEachRefresh && typeof onEachRefresh === 'function') { |
| 189 | try { |
| 190 | await onEachRefresh(updatedAccount, i + 1, needsRefresh.length) |
| 191 | } catch (error) { |
| 192 | logger.error(`刷新回调函数执行失败 (${account.email})`, 'TOKEN', '', error) |
| 193 | } |
| 194 | } |
| 195 | } else { |
| 196 | failed.push(account) |
| 197 | } |
| 198 | |
| 199 | // 添加延迟避免请求过于频繁 (jitter breaks machine-perfect timing) |
| 200 | await this._delay(jitter(1000)) |
| 201 | } |
| 202 | |
| 203 | logger.success(`令牌刷新完成: 成功 ${refreshed.length} 个,失败 ${failed.length} 个`, 'TOKEN') |
| 204 | return { refreshed, failed } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * 获取健康的令牌统计信息 |
| 209 | * @param {Array} accounts - 账户列表 |
nothing calls this directly
no test coverage detected