refactor: 优化账户过期检查逻辑和代码一致性

## 主要改进

### 1. 添加缺失的过期检查方法
- 在 `claudeConsoleAccountService` 中添加 `isSubscriptionExpired()` 方法
- 在 `ccrAccountService` 中添加 `isSubscriptionExpired()` 方法
- 与其他 7 个账户服务保持一致的实现方式

### 2. 统一过期检查逻辑
- 重构 `unifiedClaudeScheduler` 中的 5 处手动日期检查代码
- 统一调用服务层的 `isSubscriptionExpired()` 方法
- 消除重复代码,提升可维护性

### 3. 统一字段映射顺序
- 调整 Claude 账户更新端点的 `mapExpiryField()` 调用时机
- 与其他账户类型保持一致的处理顺序
- 提升代码可读性和一致性

## 技术细节

**修改文件**:
- `src/services/claudeConsoleAccountService.js`: 添加 `isSubscriptionExpired()`
- `src/services/ccrAccountService.js`: 添加 `isSubscriptionExpired()`
- `src/services/unifiedClaudeScheduler.js`: 5 处调用统一为服务方法
- `src/routes/admin.js`: 统一字段映射顺序

**改进效果**:
-  代码一致性提升:所有账户服务统一实现
-  可维护性提升:过期逻辑集中管理
-  减少重复代码:消除 4 处重复实现

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
mrlitong
2025-10-14 08:35:13 +00:00
parent cd5df4f76b
commit 1f61478fbc
4 changed files with 72 additions and 61 deletions

View File

@@ -913,6 +913,19 @@ class CcrAccountService {
throw error
}
}
/**
* ⏰ 检查账户订阅是否过期
* @param {Object} account - 账户对象
* @returns {boolean} - true: 已过期, false: 未过期
*/
isSubscriptionExpired(account) {
if (!account.subscriptionExpiresAt) {
return false // 未设置视为永不过期
}
const expiryDate = new Date(account.subscriptionExpiresAt)
return expiryDate <= new Date()
}
}
module.exports = new CcrAccountService()

View File

@@ -1254,6 +1254,19 @@ class ClaudeConsoleAccountService {
throw error
}
}
/**
* ⏰ 检查账户订阅是否过期
* @param {Object} account - 账户对象
* @returns {boolean} - true: 已过期, false: 未过期
*/
isSubscriptionExpired(account) {
if (!account.subscriptionExpiresAt) {
return false // 未设置视为永不过期
}
const expiryDate = new Date(account.subscriptionExpiresAt)
return expiryDate <= new Date()
}
}
module.exports = new ClaudeConsoleAccountService()

View File

@@ -546,15 +546,11 @@ class UnifiedClaudeScheduler {
}
// 检查订阅是否过期
if (account.subscriptionExpiresAt) {
const expiryDate = new Date(account.subscriptionExpiresAt)
const now = new Date()
if (expiryDate <= now) {
logger.debug(
`⏰ Claude Console account ${account.name} (${account.id}) expired at ${account.subscriptionExpiresAt}`
)
continue
}
if (claudeConsoleAccountService.isSubscriptionExpired(account)) {
logger.debug(
`⏰ Claude Console account ${account.name} (${account.id}) expired at ${account.subscriptionExpiresAt}`
)
continue
}
// 主动触发一次额度检查,确保状态即时生效
@@ -655,15 +651,11 @@ class UnifiedClaudeScheduler {
}
// 检查订阅是否过期
if (account.subscriptionExpiresAt) {
const expiryDate = new Date(account.subscriptionExpiresAt)
const now = new Date()
if (expiryDate <= now) {
logger.debug(
`⏰ CCR account ${account.name} (${account.id}) expired at ${account.subscriptionExpiresAt}`
)
continue
}
if (ccrAccountService.isSubscriptionExpired(account)) {
logger.debug(
`⏰ CCR account ${account.name} (${account.id}) expired at ${account.subscriptionExpiresAt}`
)
continue
}
// 检查是否被限流
@@ -799,15 +791,11 @@ class UnifiedClaudeScheduler {
return false
}
// 检查订阅是否过期
if (account.subscriptionExpiresAt) {
const expiryDate = new Date(account.subscriptionExpiresAt)
const now = new Date()
if (expiryDate <= now) {
logger.debug(
`⏰ Claude Console account ${account.name} (${accountId}) expired at ${account.subscriptionExpiresAt} (session check)`
)
return false
}
if (claudeConsoleAccountService.isSubscriptionExpired(account)) {
logger.debug(
`⏰ Claude Console account ${account.name} (${accountId}) expired at ${account.subscriptionExpiresAt} (session check)`
)
return false
}
// 检查是否超额
try {
@@ -868,15 +856,11 @@ class UnifiedClaudeScheduler {
return false
}
// 检查订阅是否过期
if (account.subscriptionExpiresAt) {
const expiryDate = new Date(account.subscriptionExpiresAt)
const now = new Date()
if (expiryDate <= now) {
logger.debug(
`⏰ CCR account ${account.name} (${accountId}) expired at ${account.subscriptionExpiresAt} (session check)`
)
return false
}
if (ccrAccountService.isSubscriptionExpired(account)) {
logger.debug(
`⏰ CCR account ${account.name} (${accountId}) expired at ${account.subscriptionExpiresAt} (session check)`
)
return false
}
// 检查是否超额
try {
@@ -1400,15 +1384,11 @@ class UnifiedClaudeScheduler {
}
// 检查订阅是否过期
if (account.subscriptionExpiresAt) {
const expiryDate = new Date(account.subscriptionExpiresAt)
const now = new Date()
if (expiryDate <= now) {
logger.debug(
`⏰ CCR account ${account.name} (${account.id}) expired at ${account.subscriptionExpiresAt}`
)
continue
}
if (ccrAccountService.isSubscriptionExpired(account)) {
logger.debug(
`⏰ CCR account ${account.name} (${account.id}) expired at ${account.subscriptionExpiresAt}`
)
continue
}
// 检查是否被限流或超额