'use client' import { useEffect, useState } from 'react' import { parseFetchError } from '@/lib/error-handler' interface SuperAdmin { id: string email: string username: string realName: string | null department: string role: string createdAt: string } interface ApiKey { id: string name: string key: string isActive: boolean createdBy: string lastUsedAt: string | null createdAt: string } type NotificationAuthType = 'none' | 'bearer' | 'header' interface ExternalNotificationTarget { id: string name: string callbackUrl: string isActive: boolean authType: NotificationAuthType authConfig: Record eventTypes: string[] createdAt: string updatedAt: string } interface ExternalNotificationDelivery { id: string targetId: string targetName: string eventType: string materialId: string materialType: string taskNo: string | null status: string attemptCount: number maxAttempts: number nextRetryAt: string | null lastStatusCode: number | null lastResponseBody: string | null lastError: string | null createdAt: string updatedAt: string completedAt: string | null } interface NotificationTargetFormState { name: string callbackUrl: string authType: NotificationAuthType token: string headerName: string headerValue: string eventTypes: string[] isActive: boolean } const NOTIFICATION_EVENT_OPTIONS = [ { value: 'task-detail.full-set.ready', label: '任务-详情全套入库完成', description: '整套素材完成后推送成套资源信息。', }, { value: 'task-detail.barcode-images.updated', label: '任务-详情条码图片更新', description: '带产品条码的 SKU 预览图新增或更新后推送条码清单。', }, ] const EMPTY_NOTIFICATION_TARGET_FORM: NotificationTargetFormState = { name: '', callbackUrl: '', authType: 'none', token: '', headerName: '', headerValue: '', eventTypes: ['task-detail.full-set.ready'], isActive: true, } const NOTIFICATION_DELIVERY_STATUS_OPTIONS = [ { value: 'all', label: '全部状态' }, { value: 'pending', label: '待发送' }, { value: 'processing', label: '发送中' }, { value: 'retrying', label: '重试中' }, { value: 'succeeded', label: '已成功' }, { value: 'exhausted', label: '待人工处理' }, ] function formatDateTime(value: string | null) { if (!value) { return '无' } return new Date(value).toLocaleString('zh-CN') } function maskSecret(value: string | undefined) { const normalized = String(value || '').trim() if (!normalized) { return '未配置' } if (normalized.length <= 8) { return '已配置' } return `${normalized.slice(0, 4)}****${normalized.slice(-4)}` } function getNotificationTargetAuthSummary(target: ExternalNotificationTarget) { if (target.authType === 'bearer') { return `Bearer · ${maskSecret(target.authConfig.token)}` } if (target.authType === 'header') { const headerName = target.authConfig.headerName || '自定义请求头' return `Header · ${headerName}` } return '无鉴权' } function getNotificationDeliveryStatusLabel(status: string) { switch (status) { case 'pending': return '待发送' case 'processing': return '发送中' case 'retrying': return '重试中' case 'succeeded': return '已成功' case 'exhausted': return '待人工处理' default: return status } } function getNotificationDeliveryStatusClassName(status: string) { switch (status) { case 'succeeded': return 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200' case 'retrying': case 'processing': return 'bg-orange-100 text-orange-800 dark:bg-orange-900/40 dark:text-orange-200' case 'exhausted': return 'bg-red-100 text-red-800 dark:bg-red-900/40 dark:text-red-200' default: return 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300' } } interface SuperAdminsTabProps { showToast: (message: string, type?: 'success' | 'error') => void } export default function SuperAdminsTab({ showToast }: SuperAdminsTabProps) { const [superAdmins, setSuperAdmins] = useState([]) const [loading, setLoading] = useState(true) const [count, setCount] = useState(0) const [canAdd, setCanAdd] = useState(false) const [addingUserId, setAddingUserId] = useState('') const [adding, setAdding] = useState(false) const [removingUserId, setRemovingUserId] = useState(null) // API Keys 相关状态 const [apiKeys, setApiKeys] = useState([]) const [apiKeysLoading, setApiKeysLoading] = useState(true) const [showCreateModal, setShowCreateModal] = useState(false) const [newKeyName, setNewKeyName] = useState('') const [createdKey, setCreatedKey] = useState(null) const [notificationTargets, setNotificationTargets] = useState([]) const [notificationTargetsLoading, setNotificationTargetsLoading] = useState(true) const [notificationTargetForm, setNotificationTargetForm] = useState( EMPTY_NOTIFICATION_TARGET_FORM, ) const [editingNotificationTargetId, setEditingNotificationTargetId] = useState(null) const [savingNotificationTarget, setSavingNotificationTarget] = useState(false) const [togglingNotificationTargetId, setTogglingNotificationTargetId] = useState(null) const [notificationDeliveries, setNotificationDeliveries] = useState([]) const [notificationDeliveriesLoading, setNotificationDeliveriesLoading] = useState(true) const [notificationDeliveryStatusFilter, setNotificationDeliveryStatusFilter] = useState('all') const [retryingNotificationDeliveryId, setRetryingNotificationDeliveryId] = useState(null) // 加载超级管理员列表 const loadSuperAdmins = async () => { try { const response = await fetch('/api/admin/super-admins') if (response.ok) { const data = await response.json() setSuperAdmins(data.superAdmins) setCount(data.count) setCanAdd(data.canAdd) } else { showToast('加载超级管理员列表失败', 'error') } } catch (error) { console.error('Load super admins error:', error) showToast('加载超级管理员列表失败', 'error') } finally { setLoading(false) } } useEffect(() => { loadSuperAdmins() fetchApiKeys() fetchNotificationTargets() }, []) useEffect(() => { fetchNotificationDeliveries(notificationDeliveryStatusFilter) }, [notificationDeliveryStatusFilter]) // 添加超级管理员 const handleAddSuperAdmin = async () => { if (!addingUserId.trim()) { showToast('请输入用户ID', 'error') return } setAdding(true) try { const response = await fetch('/api/admin/super-admins', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId: addingUserId.trim() }), }) if (response.ok) { showToast('超级管理员添加成功', 'success') setAddingUserId('') await loadSuperAdmins() } else { const data = await response.json() showToast(data.error || '添加失败', 'error') } } catch (error) { console.error('Add super admin error:', error) showToast('添加失败', 'error') } finally { setAdding(false) } } // 移除超级管理员 const handleRemoveSuperAdmin = async (userId: string) => { if (!confirm('确定要移除该超级管理员权限吗?')) { return } setRemovingUserId(userId) try { const response = await fetch(`/api/admin/super-admins?userId=${userId}`, { method: 'DELETE', }) if (response.ok) { showToast('超级管理员权限已移除', 'success') await loadSuperAdmins() } else { const data = await response.json() showToast(data.error || '移除失败', 'error') } } catch (error) { console.error('Remove super admin error:', error) showToast('移除失败', 'error') } finally { setRemovingUserId(null) } } // 加载 API Keys const fetchApiKeys = async () => { try { setApiKeysLoading(true) const response = await fetch('/api/admin/api-keys') if (response.ok) { const data = await response.json() setApiKeys(data.data || []) } else { const errorData = await parseFetchError(response) showToast(errorData.message, 'error') } } catch (error) { console.error('加载 API Keys 失败:', error) const errorMessage = error instanceof Error ? error.message : '加载失败' showToast(errorMessage, 'error') } finally { setApiKeysLoading(false) } } const resetNotificationTargetForm = () => { setEditingNotificationTargetId(null) setNotificationTargetForm(EMPTY_NOTIFICATION_TARGET_FORM) } const fetchNotificationTargets = async () => { try { setNotificationTargetsLoading(true) const response = await fetch('/api/admin/external-notification-targets') if (response.ok) { const data = await response.json() setNotificationTargets(data.data || []) } else { const errorData = await parseFetchError(response) showToast(errorData.message, 'error') } } catch (error) { console.error('加载自动上架通知目标失败:', error) const errorMessage = error instanceof Error ? error.message : '加载失败' showToast(errorMessage, 'error') } finally { setNotificationTargetsLoading(false) } } const fetchNotificationDeliveries = async (status: string) => { try { setNotificationDeliveriesLoading(true) const searchParams = new URLSearchParams() if (status && status !== 'all') { searchParams.set('status', status) } searchParams.set('limit', '50') const query = searchParams.toString() const response = await fetch( `/api/admin/external-notification-deliveries${query ? `?${query}` : ''}`, ) if (response.ok) { const data = await response.json() setNotificationDeliveries(data.data || []) } else { const errorData = await parseFetchError(response) showToast(errorData.message, 'error') } } catch (error) { console.error('加载自动上架投递记录失败:', error) const errorMessage = error instanceof Error ? error.message : '加载失败' showToast(errorMessage, 'error') } finally { setNotificationDeliveriesLoading(false) } } const handleSaveNotificationTarget = async () => { if (!notificationTargetForm.name.trim()) { showToast('请填写目标系统名称', 'error') return } if (!notificationTargetForm.callbackUrl.trim()) { showToast('请填写回调地址', 'error') return } if (notificationTargetForm.authType === 'bearer' && !notificationTargetForm.token.trim()) { showToast('请选择 Bearer 鉴权时必须填写 token', 'error') return } if ( notificationTargetForm.authType === 'header' && (!notificationTargetForm.headerName.trim() || !notificationTargetForm.headerValue.trim()) ) { showToast('自定义 Header 鉴权需要填写请求头名称和值', 'error') return } if (notificationTargetForm.eventTypes.length === 0) { showToast('请至少选择 1 个通知事件', 'error') return } setSavingNotificationTarget(true) try { const authConfig = notificationTargetForm.authType === 'bearer' ? { token: notificationTargetForm.token.trim() } : notificationTargetForm.authType === 'header' ? { headerName: notificationTargetForm.headerName.trim(), headerValue: notificationTargetForm.headerValue.trim(), } : {} const payload = { name: notificationTargetForm.name.trim(), callbackUrl: notificationTargetForm.callbackUrl.trim(), isActive: notificationTargetForm.isActive, authType: notificationTargetForm.authType, authConfig, eventTypes: notificationTargetForm.eventTypes, } const response = await fetch( editingNotificationTargetId ? `/api/admin/external-notification-targets/${editingNotificationTargetId}` : '/api/admin/external-notification-targets', { method: editingNotificationTargetId ? 'PUT' : 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }, ) if (response.ok) { showToast(editingNotificationTargetId ? '通知目标已更新' : '通知目标已创建', 'success') resetNotificationTargetForm() await fetchNotificationTargets() } else { const errorData = await parseFetchError(response) showToast(errorData.message, 'error') } } catch (error) { console.error('保存自动上架通知目标失败:', error) const errorMessage = error instanceof Error ? error.message : '保存失败' showToast(errorMessage, 'error') } finally { setSavingNotificationTarget(false) } } const handleEditNotificationTarget = (target: ExternalNotificationTarget) => { setEditingNotificationTargetId(target.id) setNotificationTargetForm({ name: target.name, callbackUrl: target.callbackUrl, authType: target.authType, token: target.authConfig.token || '', headerName: target.authConfig.headerName || '', headerValue: target.authConfig.headerValue || '', eventTypes: target.eventTypes?.length > 0 ? target.eventTypes : ['task-detail.full-set.ready'], isActive: target.isActive, }) } const handleToggleNotificationTargetActive = async (target: ExternalNotificationTarget) => { setTogglingNotificationTargetId(target.id) try { const response = await fetch(`/api/admin/external-notification-targets/${target.id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ isActive: !target.isActive, }), }) if (response.ok) { showToast(target.isActive ? '通知目标已停用' : '通知目标已启用', 'success') await fetchNotificationTargets() } else { const errorData = await parseFetchError(response) showToast(errorData.message, 'error') } } catch (error) { console.error('切换通知目标状态失败:', error) const errorMessage = error instanceof Error ? error.message : '更新失败' showToast(errorMessage, 'error') } finally { setTogglingNotificationTargetId(null) } } const handleRetryNotificationDelivery = async (deliveryId: string) => { setRetryingNotificationDeliveryId(deliveryId) try { const response = await fetch(`/api/admin/external-notification-deliveries/${deliveryId}/retry`, { method: 'POST', }) if (response.ok) { showToast('补发任务已重新入队', 'success') await fetchNotificationDeliveries(notificationDeliveryStatusFilter) } else { const errorData = await parseFetchError(response) showToast(errorData.message, 'error') } } catch (error) { console.error('手工补发失败:', error) const errorMessage = error instanceof Error ? error.message : '补发失败' showToast(errorMessage, 'error') } finally { setRetryingNotificationDeliveryId(null) } } // 创建 API Key const handleCreateApiKey = async () => { if (!newKeyName.trim()) { showToast('请输入密钥名称', 'error') return } try { const response = await fetch('/api/admin/api-keys', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: newKeyName }), }) if (response.ok) { const data = await response.json() setCreatedKey(data.data.key) setNewKeyName('') showToast('API Key 创建成功', 'success') fetchApiKeys() } else { const errorData = await parseFetchError(response) showToast(errorData.message, 'error') } } catch (error) { console.error('创建失败:', error) const errorMessage = error instanceof Error ? error.message : '创建失败' showToast(errorMessage, 'error') } } // 切换 API Key 状态 const handleToggleApiKeyActive = async (id: string, isActive: boolean) => { try { const response = await fetch(`/api/admin/api-keys/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ isActive: !isActive }), }) if (response.ok) { showToast(isActive ? 'API Key 已禁用' : 'API Key 已启用', 'success') fetchApiKeys() } else { const errorData = await parseFetchError(response) showToast(errorData.message, 'error') } } catch (error) { console.error('更新失败:', error) const errorMessage = error instanceof Error ? error.message : '更新失败' showToast(errorMessage, 'error') } } // 删除 API Key const handleDeleteApiKey = async (id: string) => { if (!confirm('确定要删除此 API Key 吗?删除后无法恢复。')) return try { const response = await fetch(`/api/admin/api-keys/${id}`, { method: 'DELETE', }) if (response.ok) { showToast('API Key 已删除', 'success') fetchApiKeys() } else { const errorData = await parseFetchError(response) showToast(errorData.message, 'error') } } catch (error) { console.error('删除失败:', error) const errorMessage = error instanceof Error ? error.message : '删除失败' showToast(errorMessage, 'error') } } if (loading) { return (
加载中...
) } return (

超级管理员管理

当前:{count} / 4 个超级管理员 {!canAdd && (已达上限)}

{/* 添加超级管理员 */} {canAdd && (

添加超级管理员

setAddingUserId(e.target.value)} placeholder="输入用户ID" className="flex-1 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500" />

提示:只能添加管理员角色的用户,且最多4个超级管理员

)} {/* 超级管理员列表 */}

超级管理员列表

{superAdmins.length === 0 ? (
暂无超级管理员
) : (
{superAdmins.map((admin) => (
{admin.realName || admin.username} 超级管理员
{admin.email} {admin.department && ( · {admin.department} )}
创建时间:{new Date(admin.createdAt).toLocaleString('zh-CN')}
))}
)}
{/* API Key 管理卡片 */}

API Key 管理

{apiKeysLoading ? (
加载中...
) : apiKeys.length === 0 ? (

暂无 API Key

点击上方按钮创建第一个 API Key

) : (
{apiKeys.map((key) => ( ))}
名称 密钥 状态 最后使用 创建时间 操作
{key.name} {key.key.substring(0, 16)}... {key.isActive ? '启用' : '禁用'} {key.lastUsedAt ? new Date(key.lastUsedAt).toLocaleString('zh-CN') : '从未使用'} {new Date(key.createdAt).toLocaleString('zh-CN')}
)}

外部系统通知目标

这里配置素材库主动通知的目标系统。`task-detail.full-set.ready` 主要给自动上架平台,`task-detail.barcode-images.updated` 主要给产品平台。

已配置目标

{notificationTargetsLoading ? (
加载中...
) : notificationTargets.length === 0 ? (
暂无通知目标
) : (
{notificationTargets.map((target) => (
{target.name} {target.isActive ? '启用中' : '已停用'}
{target.callbackUrl}
鉴权:{getNotificationTargetAuthSummary(target)}
{target.eventTypes.map((eventType) => ( {eventType === 'task-detail.full-set.ready' ? '自动上架平台 / 全套入库' : eventType === 'task-detail.barcode-images.updated' ? '产品平台 / 条码图片更新' : eventType} ))}
更新时间:{formatDateTime(target.updatedAt)}
))}
)}

{editingNotificationTargetId ? '编辑通知目标' : '新增通知目标'}

setNotificationTargetForm((current) => ({ ...current, name: e.target.value, })) } placeholder="例如:自动上架平台 / 产品平台" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500" />
setNotificationTargetForm((current) => ({ ...current, callbackUrl: e.target.value, })) } placeholder="https://example.com/callback" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500" />
{notificationTargetForm.authType === 'bearer' && (
setNotificationTargetForm((current) => ({ ...current, token: e.target.value, })) } placeholder="请输入 token" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500" />
)} {notificationTargetForm.authType === 'header' && (
setNotificationTargetForm((current) => ({ ...current, headerName: e.target.value, })) } placeholder="例如:X-Signature" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500" />
setNotificationTargetForm((current) => ({ ...current, headerValue: e.target.value, })) } placeholder="请输入请求头值" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500" />
)}
订阅事件
{NOTIFICATION_EVENT_OPTIONS.map((eventOption) => ( ))}

自动上架投递记录

查看最近 50 条通知投递情况;失败或超上限的记录可以在这里手工补发。

{notificationDeliveriesLoading ? (
加载中...
) : notificationDeliveries.length === 0 ? (
当前筛选条件下暂无投递记录
) : (
{notificationDeliveries.map((delivery) => ( ))}
目标系统 素材 状态 尝试次数 最近结果 下一次重试 更新时间 操作
{delivery.targetName || '未知目标'}
{delivery.id}
{delivery.materialType}
materialId: {delivery.materialId}
taskNo: {delivery.taskNo || '无'}
{getNotificationDeliveryStatusLabel(delivery.status)} {delivery.attemptCount} / {delivery.maxAttempts}
{delivery.lastStatusCode ? `HTTP ${delivery.lastStatusCode}` : '暂无'}
{delivery.lastError || delivery.lastResponseBody || '无'}
{formatDateTime(delivery.nextRetryAt)} {formatDateTime(delivery.updatedAt)}
)}
{/* 创建 API Key 模态框 */} {showCreateModal && (

创建 API Key

{createdKey ? (

API Key 已创建,请妥善保存:

{createdKey}

注意:此密钥只显示一次,关闭后将无法再次查看

) : (
setNewKeyName(e.target.value)} placeholder="例如:任务系统" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg mb-4 bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500" onKeyPress={(e) => { if (e.key === 'Enter') { handleCreateApiKey() } }} />
)}
)}
) }