feat: 添加账户订阅到期时间管理功能

## 新增功能
- 支持为 Claude 账户设置订阅到期时间
- 前端提供到期时间选择器(快捷选项 + 自定义日期)
- 账户列表显示到期状态(已过期/即将过期/永不过期)
- 新增独立的到期时间编辑弹窗组件

## 技术变更
- 后端新增 subscriptionExpiresAt 字段存储
- 前端使用 expiresAt 字段进行交互
- 支持创建、编辑、显示完整流程

## 包含文件
- src/routes/admin.js: POST/PUT 端点支持 expiresAt 字段
- src/services/claudeAccountService.js: 存储和返回到期时间
- web/admin-spa/src/components/accounts/AccountForm.vue: 表单添加到期时间选择
- web/admin-spa/src/views/AccountsView.vue: 列表显示和编辑功能
- web/admin-spa/src/components/accounts/AccountExpiryEditModal.vue: 新增编辑弹窗
- account_expire_feature.md: 代码评审报告和优化建议

## 注意事项
⚠️ 本次提交包含初步实现,详细的优化建议请查看 account_expire_feature.md

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
litongtongxue
2025-10-11 01:05:21 +08:00
parent 80059e2b09
commit a82dcebd7b
6 changed files with 1484 additions and 10 deletions

View File

@@ -206,6 +206,21 @@
/>
<i v-else class="fas fa-sort ml-1 text-gray-400" />
</th>
<th
class="w-[12%] min-w-[110px] cursor-pointer px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-600"
@click="sortAccounts('expiresAt')"
>
到期时间
<i
v-if="accountsSortBy === 'expiresAt'"
:class="[
'fas',
accountsSortOrder === 'asc' ? 'fa-sort-up' : 'fa-sort-down',
'ml-1'
]"
/>
<i v-else class="fas fa-sort ml-1 text-gray-400" />
</th>
<th
class="w-[12%] min-w-[100px] cursor-pointer px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-600"
@click="sortAccounts('status')"
@@ -557,6 +572,49 @@
</div>
</div>
</td>
<td class="whitespace-nowrap px-3 py-4">
<div class="flex flex-col gap-1">
<!-- 已设置过期时间 -->
<span v-if="account.expiresAt">
<span
v-if="isExpired(account.expiresAt)"
class="inline-flex cursor-pointer items-center text-red-600 hover:underline"
style="font-size: 13px"
@click.stop="startEditAccountExpiry(account)"
>
<i class="fas fa-exclamation-circle mr-1 text-xs" />
已过期
</span>
<span
v-else-if="isExpiringSoon(account.expiresAt)"
class="inline-flex cursor-pointer items-center text-orange-600 hover:underline"
style="font-size: 13px"
@click.stop="startEditAccountExpiry(account)"
>
<i class="fas fa-clock mr-1 text-xs" />
{{ formatExpireDate(account.expiresAt) }}
</span>
<span
v-else
class="cursor-pointer text-gray-600 hover:underline dark:text-gray-400"
style="font-size: 13px"
@click.stop="startEditAccountExpiry(account)"
>
{{ formatExpireDate(account.expiresAt) }}
</span>
</span>
<!-- 永不过期 -->
<span
v-else
class="inline-flex cursor-pointer items-center text-gray-400 hover:underline dark:text-gray-500"
style="font-size: 13px"
@click.stop="startEditAccountExpiry(account)"
>
<i class="fas fa-infinity mr-1 text-xs" />
永不过期
</span>
</div>
</td>
<td class="whitespace-nowrap px-3 py-4">
<div class="flex flex-col gap-1">
<span
@@ -1644,6 +1702,15 @@
:summary="accountUsageSummary"
@close="closeAccountUsageModal"
/>
<!-- 账户过期时间编辑弹窗 -->
<AccountExpiryEditModal
ref="expiryEditModalRef"
:account="editingExpiryAccount || { id: null, expiresAt: null, name: '' }"
:show="!!editingExpiryAccount"
@close="closeAccountExpiryEdit"
@save="handleSaveAccountExpiry"
/>
</div>
</template>
@@ -1655,6 +1722,7 @@ import { useConfirm } from '@/composables/useConfirm'
import AccountForm from '@/components/accounts/AccountForm.vue'
import CcrAccountForm from '@/components/accounts/CcrAccountForm.vue'
import AccountUsageDetailModal from '@/components/accounts/AccountUsageDetailModal.vue'
import AccountExpiryEditModal from '@/components/accounts/AccountExpiryEditModal.vue'
import ConfirmModal from '@/components/common/ConfirmModal.vue'
import CustomDropdown from '@/components/common/CustomDropdown.vue'
@@ -1711,6 +1779,10 @@ const supportedUsagePlatforms = [
'droid'
]
// 过期时间编辑弹窗状态
const editingExpiryAccount = ref(null)
const expiryEditModalRef = ref(null)
// 缓存状态标志
const apiKeysLoaded = ref(false)
const groupsLoaded = ref(false)
@@ -3433,6 +3505,70 @@ watch(paginatedAccounts, () => {
watch(accounts, () => {
cleanupSelectedAccounts()
})
// 到期时间相关方法
const formatExpireDate = (dateString) => {
if (!dateString) return ''
const date = new Date(dateString)
return date.toLocaleDateString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit'
})
}
const isExpired = (expiresAt) => {
if (!expiresAt) return false
return new Date(expiresAt) < new Date()
}
const isExpiringSoon = (expiresAt) => {
if (!expiresAt) return false
const now = new Date()
const expireDate = new Date(expiresAt)
const daysUntilExpire = (expireDate - now) / (1000 * 60 * 60 * 24)
return daysUntilExpire > 0 && daysUntilExpire <= 7
}
// 开始编辑账户过期时间
const startEditAccountExpiry = (account) => {
editingExpiryAccount.value = account
}
// 关闭账户过期时间编辑
const closeAccountExpiryEdit = () => {
editingExpiryAccount.value = null
}
// 保存账户过期时间
const handleSaveAccountExpiry = async ({ accountId, expiresAt }) => {
try {
const data = await apiClient.put(`/admin/claude-accounts/${accountId}`, {
expiresAt: expiresAt || null
})
if (data.success) {
showToast('账户到期时间已更新', 'success')
// 更新本地数据
const account = accounts.value.find((acc) => acc.id === accountId)
if (account) {
account.expiresAt = expiresAt || null
}
closeAccountExpiryEdit()
} else {
showToast(data.message || '更新失败', 'error')
// 重置保存状态
if (expiryEditModalRef.value) {
expiryEditModalRef.value.resetSaving()
}
}
} catch (error) {
showToast('更新失败', 'error')
// 重置保存状态
if (expiryEditModalRef.value) {
expiryEditModalRef.value.resetSaving()
}
}
}
onMounted(() => {
// 首次加载时强制刷新所有数据