Files
Ydisks-Xianyu-Helper/frontend/components/AccountList.tsx
T

475 lines
20 KiB
TypeScript

import React, { useEffect, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import { AccountDetail } from '../types';
import {
updateAccountStatus,
deleteAccount,
generateQRLogin,
checkQRLoginStatus,
completeQRVerification,
refreshAccountProfile,
} from '../app/features/accounts/api';
import { Loader2, QrCode, User } from 'lucide-react';
import { createLatestRequestGate, createQRLoginPoller } from './qrPolling';
import AccountAutomationModal from './AccountAutomationModal';
import { AccountEditModal } from '../app/features/accounts/components/AccountEditModal';
import { AccountAISettingsModal } from '../app/features/accounts/components/AccountAISettingsModal';
import { AccountCard } from '../app/features/accounts/components/AccountCard';
import { AccountDeleteDialog } from '../app/features/accounts/components/AccountDeleteDialog';
import { AccountQRCodeModal } from '../app/features/accounts/components/AccountQRCodeModal';
import { useAccountsData } from '../app/features/accounts/hooks';
import { useAccountSubmodules, type AccountModalType } from '../app/features/accounts/submoduleHooks';
import type { AccountEditForm } from '../app/features/accounts/types';
// AccountList 渲染账号列表组件。
const AccountList: React.FC = () => {
// accountData 保存账号列表及其加载控制器。
const { accounts, setAccounts, loading, loadAccounts } = useAccountsData();
// accountSearch 保存列表过滤关键词。
const [accountSearch, setAccountSearch] = useState('');
// refreshingProfileId 保存正在刷新资料的账号 ID。
const [refreshingProfileId, setRefreshingProfileId] = useState<string>('');
// deletingAccountId 保存正在删除的账号 ID。
const [deletingAccountId, setDeletingAccountId] = useState<string>('');
// deleteDialogAccount 保存待确认删除的账号。
const [deleteDialogAccount, setDeleteDialogAccount] = useState<AccountDetail | null>(null);
// deleteError 保存删除失败提示。
const [deleteError, setDeleteError] = useState('');
// showQRModal 表示二维码登录弹窗是否打开。
const [showQRModal, setShowQRModal] = useState(false);
// qrCodeUrl 保存当前二维码登录地址。
const [qrCodeUrl, setQrCodeUrl] = useState<string>('');
// qrStatus 保存二维码登录流程状态。
const [qrStatus, setQrStatus] = useState<string>('pending');
// qrErrorMessage 保存二维码登录错误说明。
const [qrErrorMessage, setQrErrorMessage] = useState<string>('');
// verificationScreenshot 保存风控验证截图地址。
const [verificationScreenshot, setVerificationScreenshot] = useState<string>('');
// faceQrUrl 保存人脸验证二维码地址。
const [faceQrUrl, setFaceQrUrl] = useState<string>('');
// qrReauthTarget 保存需要重新授权的目标账号。
const [qrReauthTarget, setQrReauthTarget] = useState<AccountDetail | null>(null);
// activeModal 保存当前打开的账号配置弹窗。
const [activeModal, setActiveModal] = useState<AccountModalType>(null);
// editingAccount 保存当前编辑账号。
const [editingAccount, setEditingAccount] = useState<AccountDetail | null>(null);
// taskAccount 保存当前打开自动化任务弹窗的账号。
const [taskAccount, setTaskAccount] = useState<AccountDetail | null>(null);
// 编辑表单状态。
// editForm 保存编辑弹窗中的账号草稿。
const [editForm, setEditForm] = useState<AccountEditForm>({
remark: '',
cookie: '',
auto_confirm: false,
pause_duration: 0,
username: '',
login_password: '',
show_browser: false,
showLoginPassword: false,
clear_password: false,
});
// accountSubmodules 集中管理编辑弹窗的长登录、通知绑定、AI 和密码登录状态。
const accountSubmodules = useAccountSubmodules({
editingAccount,
setEditingAccount,
setActiveModal,
editForm,
setEditForm,
loadAccounts,
});
// submoduleHandlers 保存编辑弹窗子模块的状态和事件处理函数。
const {
longLogin,
notifChannels,
selectedChannelIds,
bindingsLoaded,
bindingsLoading,
bindingsLoadError,
aiSettings,
saving,
passwordLoginView,
setAiSettings,
setBindingsDirty,
openEditModal,
closeEditModal,
openAIModal,
closeAIModal,
loadNotificationBindings,
toggleNotificationChannel,
handleLongLoginToggle,
handleSaveAISettings,
handleSaveEdit,
handleRestartPause,
handlePasswordLogin,
handleCancelPasswordLogin,
} = accountSubmodules;
// qrPollerRef 保存二维码登录状态轮询器。
const qrPollerRef = useRef<ReturnType<typeof createQRLoginPoller> | null>(null);
// qrRequestGateRef 隔离过期的二维码生成请求。
const qrRequestGateRef = useRef<ReturnType<typeof createLatestRequestGate> | null>(null);
// qrGenerateAbortRef 保存二维码生成请求控制器。
const qrGenerateAbortRef = useRef<AbortController | null>(null);
// qrCloseTimerRef 保存二维码弹窗延迟关闭定时器。
const qrCloseTimerRef = useRef<number | null>(null);
if (qrPollerRef.current === null) {
qrPollerRef.current = createQRLoginPoller();
}
if (qrRequestGateRef.current === null) {
qrRequestGateRef.current = createLatestRequestGate();
}
// clearQRCloseTimer 清理二维码弹窗延迟关闭定时器。
const clearQRCloseTimer = () => {
if (qrCloseTimerRef.current !== null) {
window.clearTimeout(qrCloseTimerRef.current);
qrCloseTimerRef.current = null;
}
};
// stopQRPolling 停止二维码登录状态轮询。
const stopQRPolling = () => {
qrPollerRef.current?.stop();
};
// closeQRModal 关闭二维码登录弹窗并取消请求。
const closeQRModal = () => {
qrGenerateAbortRef.current?.abort();
qrRequestGateRef.current?.cancel();
stopQRPolling();
clearQRCloseTimer();
setShowQRModal(false);
};
// scheduleQRModalClose 在登录成功后延迟关闭二维码弹窗。
const scheduleQRModalClose = () => {
clearQRCloseTimer();
qrCloseTimerRef.current = window.setTimeout(/* 当前回调处理用户交互或异步状态变化。 */ () => {
qrCloseTimerRef.current = null;
setShowQRModal(false);
loadAccounts();
}, 1000);
};
useEffect(/* 当前回调同步 React 副作用和资源生命周期。 */ () => {
// 页面卸载时取消二维码、通知绑定、AI 设置和密码登录的异步资源。
return /* 当前回调处理用户交互或异步状态变化。 */ () => {
stopQRPolling();
qrRequestGateRef.current?.cancel();
qrGenerateAbortRef.current?.abort();
clearQRCloseTimer();
};
}, []);
// handleToggle 切换账号启用状态。
const handleToggle = async (id: string, currentStatus: boolean) => {
await updateAccountStatus(id, !currentStatus);
loadAccounts();
};
// openDeleteDialog 打开账号删除确认框。
const openDeleteDialog = (account: AccountDetail) => {
if (deletingAccountId) return;
setDeleteError('');
setDeleteDialogAccount(account);
};
// closeDeleteDialog 关闭账号删除确认框。
const closeDeleteDialog = () => {
if (deletingAccountId) return;
setDeleteError('');
setDeleteDialogAccount(null);
};
// confirmDeleteAccount 执行账号删除并刷新列表状态。
const confirmDeleteAccount = async () => {
// account 保存当前确认删除的账号。
const account = deleteDialogAccount;
if (!account || deletingAccountId) return;
setDeletingAccountId(account.id);
setDeleteError('');
try {
await deleteAccount(account.id);
setAccounts(/* 当前回调处理集合中的单个元素。 */ current => current.filter(/* 当前回调处理集合中的单个元素。 */ item => item.id !== account.id));
setDeleteDialogAccount(null);
} catch (/* error 表示错误。 */ error: any) {
console.error('删除账号失败:', error);
setDeleteError(error?.message || '删除账号失败,请稍后重试');
} finally {
setDeletingAccountId('');
}
};
// handleRefreshProfile 刷新账号资料并同步列表。
const handleRefreshProfile = async (account: AccountDetail) => {
setRefreshingProfileId(account.id);
try {
// res 保存资料刷新接口返回值。
const res = await refreshAccountProfile(account.id);
if (res?.profile_error) {
alert('资料刷新失败:' + res.profile_error);
}
await loadAccounts();
} catch (/* error 表示错误。 */ error: any) {
console.error('刷新账号资料失败:', error);
alert(error?.message || '刷新账号资料失败,请先重新授权该账号');
} finally {
setRefreshingProfileId('');
}
};
// completeAndPersistQRSession 完成二维码风控验证并持久化授权结果。
const completeAndPersistQRSession = async (sessionId: string, target?: AccountDetail | null) => {
// res 保存风控验证完成接口返回值。
const res = await completeQRVerification(sessionId, target?.id);
if (!res.success || !res.account_id) {
throw new Error(res.message || '保存扫码授权失败');
}
return res.account_id;
};
// startQRLogin 启动二维码登录或账号重新授权流程。
const startQRLogin = async (target?: AccountDetail) => {
stopQRPolling();
qrGenerateAbortRef.current?.abort();
// controller 控制当前二维码生成请求的取消。
const controller = new AbortController();
qrGenerateAbortRef.current = controller;
// requestGeneration 标识当前二维码生成请求代次。
const requestGeneration = qrRequestGateRef.current!.next();
clearQRCloseTimer();
// targetAccount 保存当前二维码授权目标账号。
const targetAccount = target || null;
setQrReauthTarget(targetAccount);
setShowQRModal(true);
setQrStatus('loading');
setQrErrorMessage('');
setQrCodeUrl('');
setVerificationScreenshot('');
setFaceQrUrl('');
try {
// res 保存二维码生成接口返回值。
const res = await generateQRLogin({ signal: controller.signal });
if (!qrRequestGateRef.current?.isCurrent(requestGeneration)) return;
if (!res.success || !res.qr_code_url || !res.session_id) {
throw new Error(res.message || '闲鱼未返回可用的登录二维码');
}
if (res.success && res.qr_code_url && res.session_id) {
// generatedSessionID 保存后端生成的二维码登录会话。
const generatedSessionID = res.session_id;
setQrCodeUrl(res.qr_code_url);
setQrStatus('waiting');
qrPollerRef.current?.start(generatedSessionID, checkQRLoginStatus, {
onSuccess: /* 当前回调处理用户交互或异步状态变化。 */ async () => {
try {
// accountId 账号标识,负责当前功能中的对应处理。
const accountId = await completeAndPersistQRSession(generatedSessionID, targetAccount);
if (!accountId) {
setQrStatus('error');
return;
}
} catch (/* e 表示e。 */ e) {
console.error('保存扫码授权失败', e);
setQrStatus('error');
return;
}
setQrStatus('success');
scheduleQRModalClose();
},
onScanned: /* 当前回调处理用户交互或异步状态变化。 */ () => {
setQrStatus('waiting'); // 已扫描,继续等待确认
},
onTerminalError: /* 当前回调处理用户交互或异步状态变化。 */ () => {
setQrStatus('error');
},
onPollError: /* 当前回调处理用户交互或异步状态变化。 */ (error) => {
console.error('轮询扫码状态失败', error);
setQrStatus('error');
},
onVerificationRequired: /* 当前回调处理用户交互或异步状态变化。 */ (statusRes) => {
setQrStatus('verification');
if (statusRes.face_qr_url) {
setFaceQrUrl(statusRes.face_qr_url);
}
if (statusRes.verification_screenshot) {
setVerificationScreenshot(statusRes.verification_screenshot);
}
},
});
}
} catch (/* e 表示e。 */ e) {
if (!qrRequestGateRef.current?.isCurrent(requestGeneration)) return;
setQrErrorMessage(e instanceof Error ? e.message : '二维码获取失败,请稍后重试');
setQrStatus('error');
} finally {
if (qrGenerateAbortRef.current === controller) qrGenerateAbortRef.current = null;
}
};
if (loading) return <div className="p-20 flex justify-center"><Loader2 className="w-8 h-8 text-brand animate-spin"/></div>;
// filteredAccounts 过滤后的账号列表,负责当前功能中的对应处理。
const filteredAccounts = accounts.filter(/* 当前回调处理集合中的单个元素。 */ account => {
// keyword 搜索关键词。
const keyword = accountSearch.trim().toLowerCase();
if (!keyword) return true;
return [
account.id,
account.nickname,
account.remark,
account.username,
account.runtime_message,
].some(/* 当前回调处理用户交互或异步状态变化。 */ value => (value || '').toLowerCase().includes(keyword));
});
return (
<div className="space-y-8 animate-fade-in relative">
<div className="flex flex-col xl:flex-row xl:justify-between xl:items-end gap-4">
<div>
<h2 className="text-4xl font-extrabold text-gray-900 tracking-tight">账号管理</h2>
<p className="text-gray-500 mt-2 font-medium">管理您的闲鱼授权账号及设置。建议给账号填写备注,便于多账号区分。</p>
</div>
<div className="flex flex-col sm:flex-row gap-3">
<input
value={accountSearch}
onChange={/* 当前回调处理用户交互或异步状态变化。 */ event => setAccountSearch(event.target.value)}
placeholder="搜索昵称 / 备注 / 账号ID"
className="ios-input px-4 py-3 rounded-2xl text-sm w-full sm:w-72"
/>
<button
onClick={/* 当前回调处理用户交互或异步状态变化。 */ () => startQRLogin()}
className="ios-btn-primary flex items-center gap-2 px-6 py-3 rounded-2xl font-bold shadow-lg shadow-blue-200 transition-transform hover:scale-105 active:scale-95"
>
<QrCode className="w-5 h-5" />
扫码添加新账号
</button>
</div>
</div>
<div className="rounded-2xl border border-blue-100 bg-blue-50 px-5 py-4 text-sm text-blue-900 flex flex-col md:flex-row md:items-center md:justify-between gap-2">
<div className="font-bold">当前显示 {filteredAccounts.length} / {accounts.length} 个账号</div>
<div className="text-blue-700">如果某个账号只显示 ID,点该账号右侧“刷新资料”;若刷新失败,先点二维码重新授权。</div>
</div>
{/* Account Grid */}
<div className="grid grid-cols-1 gap-6">
{filteredAccounts.map(/* 当前回调处理集合中的单个元素。 */ account => (
<AccountCard
key={account.id}
account={account}
refreshing={refreshingProfileId === account.id}
deleting={deletingAccountId === account.id}
onRefreshProfile={handleRefreshProfile}
onReauthorize={startQRLogin}
onEdit={openEditModal}
onAI={openAIModal}
onTasks={setTaskAccount}
onToggle={handleToggle}
onDelete={openDeleteDialog}
/>
))}
{accounts.length === 0 && (
<div className="ios-card p-12 text-center">
<div className="w-20 h-20 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4">
<User className="w-10 h-10 text-gray-400" />
</div>
<h3 className="text-lg font-bold text-gray-900">暂无账号</h3>
<p className="text-gray-500 mt-1">请点击右上角扫码添加您的闲鱼账号</p>
</div>
)}
{accounts.length > 0 && filteredAccounts.length === 0 && (
<div className="ios-card p-12 text-center">
<h3 className="text-lg font-bold text-gray-900">没有匹配的账号</h3>
<p className="text-gray-500 mt-1">换一个关键词搜索昵称、备注或账号ID。</p>
</div>
)}
</div>
{taskAccount && createPortal(
<AccountAutomationModal
account={taskAccount}
onClose={/* 当前回调处理用户交互或异步状态变化。 */ () => setTaskAccount(null)}
onSaved={/* 当前回调处理集合中的单个元素。 */ settings => {
setAccounts(/* 当前回调处理集合中的单个元素。 */ current => current.map(/* 当前回调处理集合中的单个元素。 */ account => account.id === taskAccount.id ? {
...account,
auto_rate_enabled: settings.auto_rate_enabled,
rate_content: settings.rate_content,
auto_polish_enabled: settings.auto_polish_enabled,
polish_time: settings.polish_time,
last_rate_scan_at: settings.last_rate_scan_at,
last_polish_date: settings.last_polish_date,
last_polish_at: settings.last_polish_at,
} : account));
}}
/>,
document.body
)}
{deleteDialogAccount && (
<AccountDeleteDialog
account={deleteDialogAccount}
deleting={deletingAccountId === deleteDialogAccount.id}
error={deleteError}
onClose={closeDeleteDialog}
onConfirm={confirmDeleteAccount}
/>
)}
{showQRModal && (
<AccountQRCodeModal
target={qrReauthTarget}
status={qrStatus}
codeUrl={qrCodeUrl}
errorMessage={qrErrorMessage}
faceQrUrl={faceQrUrl}
verificationScreenshot={verificationScreenshot}
onClose={closeQRModal}
/>
)}
{/* 编辑账号弹窗由 accounts feature 组件负责渲染和表单交互。 */}
{activeModal === 'edit' && editingAccount && (
<AccountEditModal
account={editingAccount}
editForm={editForm}
setEditForm={setEditForm}
saving={saving}
onClose={closeEditModal}
onSave={handleSaveEdit}
onRestartPause={handleRestartPause}
longLogin={longLogin}
onToggleLongLogin={handleLongLoginToggle}
passwordLoginView={passwordLoginView}
onPasswordLogin={handlePasswordLogin}
onCancelPasswordLogin={handleCancelPasswordLogin}
notifChannels={notifChannels}
selectedChannelIds={selectedChannelIds}
bindingsLoaded={bindingsLoaded}
bindingsLoading={bindingsLoading}
bindingsLoadError={bindingsLoadError}
onRetryBindings={/* 当前回调处理用户交互或异步状态变化。 */ () => loadNotificationBindings(editingAccount.id)}
onToggleChannel={toggleNotificationChannel}
onSettingsDirty={/* 当前回调处理用户交互或异步状态变化。 */ () => setBindingsDirty(true)}
/>
)}
{activeModal === 'ai-settings' && editingAccount && (
<AccountAISettingsModal
account={editingAccount}
settings={aiSettings}
saving={saving}
onChange={setAiSettings}
onClose={closeAIModal}
onSave={handleSaveAISettings}
/>
)}
</div>
);
};
export default AccountList;