fix(webui): recover QR login after forced logout (#2030)
This commit is contained in:
1 parent
fa3fe9fdb2
commit
c75d2f7f86
8 files changed
+322
-33
No files matched your search
@@ -7,12 +7,33 @@ interface QrCodeLoginProps {
|
||||
qrcode: string;
|
||||
loginError?: string;
|
||||
onRefresh?: () => void;
|
||||
isRefreshing?: boolean;
|
||||
loginPhase?: string;
|
||||
isRecoveringLoginService?: boolean;
|
||||
}
|
||||
|
||||
const QrCodeLogin: React.FC<QrCodeLoginProps> = ({ qrcode, loginError, onRefresh }) => {
|
||||
const QrCodeLogin: React.FC<QrCodeLoginProps> = ({ qrcode, loginError, onRefresh, isRefreshing = false, loginPhase, isRecoveringLoginService = false }) => {
|
||||
const transitionMessage = loginPhase === 'qrcode_scanned'
|
||||
? '二维码已扫描,等待手机确认…'
|
||||
: loginPhase === 'initializing'
|
||||
? 'QQ 登录成功,正在启动 NapCat…'
|
||||
: loginPhase === 'reconnecting' || isRecoveringLoginService
|
||||
? '正在重新连接 QQ 登录服务…'
|
||||
: undefined;
|
||||
return (
|
||||
<div className='flex flex-col items-center'>
|
||||
{loginError
|
||||
{(isRefreshing || transitionMessage) && (
|
||||
<div className='fixed inset-0 z-50 flex items-center justify-center bg-black/40 backdrop-blur-sm'>
|
||||
<div className='flex flex-col items-center gap-4 rounded-xl bg-content1 px-10 py-8 shadow-xl'>
|
||||
<Spinner color='primary' size='lg' />
|
||||
<div className='text-lg font-medium'>{transitionMessage || '正在刷新二维码…'}</div>
|
||||
<div className='text-sm text-default-500'>
|
||||
{transitionMessage ? '请不要关闭此页面,最长可能需要 3 分钟' : '正在等待新的二维码,最长 10 秒'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{loginError && !qrcode && !isRecoveringLoginService
|
||||
? (
|
||||
<div className='flex flex-col items-center py-4'>
|
||||
<div className='w-full flex justify-center mb-6'>
|
||||
@@ -34,6 +55,8 @@ const QrCodeLogin: React.FC<QrCodeLoginProps> = ({ qrcode, loginError, onRefresh
|
||||
size='lg'
|
||||
startContent={<IoRefresh />}
|
||||
onPress={onRefresh}
|
||||
isLoading={isRefreshing}
|
||||
isDisabled={isRefreshing}
|
||||
>
|
||||
重新获取二维码
|
||||
</Button>
|
||||
@@ -42,13 +65,18 @@ const QrCodeLogin: React.FC<QrCodeLoginProps> = ({ qrcode, loginError, onRefresh
|
||||
)
|
||||
: (
|
||||
<>
|
||||
{loginError && (
|
||||
<div className='mb-4 w-full max-w-[360px] rounded-lg bg-warning-50 px-4 py-3 text-center text-sm text-warning-700 dark:bg-warning-50/20 dark:text-warning-300'>
|
||||
{loginError}
|
||||
</div>
|
||||
)}
|
||||
<div className='bg-white p-2 rounded-md w-fit mx-auto relative overflow-hidden'>
|
||||
{!qrcode && (
|
||||
<div className='absolute left-0 top-0 right-0 bottom-0 bg-white dark:bg-zinc-900 bg-opacity-90 backdrop-blur-sm flex items-center justify-center z-10'>
|
||||
<Spinner color='primary' />
|
||||
</div>
|
||||
)}
|
||||
<QRCodeSVG size={180} value={qrcode || ' '} />
|
||||
<QRCodeSVG key={qrcode} size={180} value={qrcode || ' '} />
|
||||
</div>
|
||||
<div className='mt-5 text-center text-default-500 text-sm'>请使用QQ或者TIM扫描上方二维码</div>
|
||||
{onRefresh && qrcode && (
|
||||
@@ -59,6 +87,8 @@ const QrCodeLogin: React.FC<QrCodeLoginProps> = ({ qrcode, loginError, onRefresh
|
||||
size='sm'
|
||||
startContent={<IoRefresh />}
|
||||
onPress={onRefresh}
|
||||
isLoading={isRefreshing}
|
||||
isDisabled={isRefreshing}
|
||||
>
|
||||
刷新二维码
|
||||
</Button>
|
||||
|
||||
@@ -32,14 +32,23 @@ export default class QQManager {
|
||||
|
||||
public static async checkQQLoginStatusWithQrcode () {
|
||||
const data = await serverRequest.post<
|
||||
ServerResponse<{ qrcodeurl: string; isLogin: boolean; isOffline?: boolean; loginError?: string; }>
|
||||
ServerResponse<{
|
||||
qrcodeurl: string;
|
||||
isLogin: boolean;
|
||||
isOffline?: boolean;
|
||||
loginError?: string;
|
||||
loginPhase?: 'waiting_qrcode' | 'generating_qrcode' | 'qrcode_scanned' | 'initializing' | 'ready' | 'offline' | 'reconnecting';
|
||||
qrLoginAccepted?: boolean;
|
||||
coreReady?: boolean;
|
||||
}>
|
||||
>('/QQLogin/CheckLoginStatus');
|
||||
|
||||
return data.data.data;
|
||||
}
|
||||
|
||||
public static async refreshQRCode () {
|
||||
await serverRequest.post<ServerResponse<null>>('/QQLogin/RefreshQRcode');
|
||||
const data = await serverRequest.post<ServerResponse<{ qrcodeurl: string; restarting?: boolean; }>>('/QQLogin/RefreshQRcode');
|
||||
return data.data.data;
|
||||
}
|
||||
|
||||
public static async getQQLoginQrcode () {
|
||||
|
||||
@@ -53,6 +53,11 @@ export default function QQLoginPage () {
|
||||
const [uinValue, setUinValue] = useState<string>('');
|
||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||
const [qrcode, setQrcode] = useState<string>('');
|
||||
const [loginPhase, setLoginPhase] = useState<string>('waiting_qrcode');
|
||||
const [isRecoveringLoginService, setIsRecoveringLoginService] = useState(false);
|
||||
const qrcodeRef = useRef<string>('');
|
||||
const qrcodeRequestVersionRef = useRef(0);
|
||||
const isRefreshingQRCodeRef = useRef(false);
|
||||
const [loginError, setLoginError] = useState<string>('');
|
||||
const lastErrorRef = useRef<string>('');
|
||||
const [qqList, setQQList] = useState<(QQItem | LoginListItem)[]>([]);
|
||||
@@ -199,9 +204,14 @@ export default function QQLoginPage () {
|
||||
};
|
||||
|
||||
const onUpdateQrCode = async () => {
|
||||
const requestVersion = qrcodeRequestVersionRef.current;
|
||||
if (firstLoad.current) setIsLoading(true);
|
||||
try {
|
||||
const data = await QQManager.checkQQLoginStatusWithQrcode();
|
||||
setLoginPhase(data.loginPhase || (data.isLogin ? 'ready' : 'waiting_qrcode'));
|
||||
if (data.loginPhase === 'reconnecting') {
|
||||
setIsRecoveringLoginService(true);
|
||||
}
|
||||
|
||||
if (firstLoad.current) {
|
||||
setIsLoading(false);
|
||||
@@ -211,17 +221,26 @@ export default function QQLoginPage () {
|
||||
toast.success('QQ登录成功');
|
||||
navigate('/', { replace: true });
|
||||
} else {
|
||||
setQrcode(data.qrcodeurl);
|
||||
// 刷新二维码是一个需要等待结果的操作。忽略在它开始前发出的
|
||||
// 轮询响应,避免旧 URL 在刷新完成后覆盖刚拿到的新二维码。
|
||||
if (requestVersion === qrcodeRequestVersionRef.current && !isRefreshingQRCodeRef.current) {
|
||||
qrcodeRef.current = data.qrcodeurl;
|
||||
setQrcode(data.qrcodeurl);
|
||||
if (data.qrcodeurl) {
|
||||
setIsRecoveringLoginService(false);
|
||||
}
|
||||
}
|
||||
if (data.loginError && data.loginError !== lastErrorRef.current) {
|
||||
lastErrorRef.current = data.loginError;
|
||||
setLoginError(data.loginError);
|
||||
const friendlyMsg = parseLoginError(data.loginError);
|
||||
|
||||
// 仅在扫码登录 Tab 下才弹窗,或者错误不是"二维码已过期"
|
||||
// 如果是 "二维码已过期",且不在 qrcode tab,则不弹窗
|
||||
// 被踢下线时页面会自动进入登录服务恢复流程。它不是可由用户
|
||||
// 处理的登录表单错误,也不应弹出对话框遮住恢复状态。
|
||||
const isRecovering = data.loginPhase === 'reconnecting' || isRecoveringLoginService;
|
||||
const isQrCodeExpired = friendlyMsg.includes('二维码') && (friendlyMsg.includes('过期') || friendlyMsg.includes('失效'));
|
||||
|
||||
if (!isQrCodeExpired || activeTab === 'qrcode') {
|
||||
if (!isRecovering && (!isQrCodeExpired || activeTab === 'qrcode')) {
|
||||
dialog.alert({
|
||||
title: '登录失败',
|
||||
content: friendlyMsg,
|
||||
@@ -235,8 +254,11 @@ export default function QQLoginPage () {
|
||||
}
|
||||
} catch (error) {
|
||||
const msg = (error as Error).message;
|
||||
|
||||
toast.error(`获取二维码失败: ${msg}`);
|
||||
// Worker 重启时,旧连接被关闭是预期现象。恢复状态持续轮询即可,
|
||||
// 不要把 ERR_EMPTY_RESPONSE 作为用户可操作的错误反复弹出。
|
||||
if (!isRecoveringLoginService) {
|
||||
toast.error(`获取二维码失败: ${msg}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -271,29 +293,74 @@ export default function QQLoginPage () {
|
||||
};
|
||||
|
||||
const onRefreshQRCode = async () => {
|
||||
if (isRecoveringLoginService || loginPhase === 'reconnecting') {
|
||||
toast('QQ 登录服务正在恢复,请等待新的二维码…', { icon: '⏳' });
|
||||
return;
|
||||
}
|
||||
if (isRefreshingQRCodeRef.current) {
|
||||
return;
|
||||
}
|
||||
qrcodeRequestVersionRef.current += 1;
|
||||
isRefreshingQRCodeRef.current = true;
|
||||
setIsRefreshingQRCode(true);
|
||||
try {
|
||||
lastErrorRef.current = '';
|
||||
setLoginError('');
|
||||
await QQManager.refreshQRCode();
|
||||
toast.success('已发送刷新请求');
|
||||
const { qrcodeurl, restarting } = await QQManager.refreshQRCode();
|
||||
if (restarting) {
|
||||
setIsRecoveringLoginService(true);
|
||||
setLoginPhase('reconnecting');
|
||||
return;
|
||||
}
|
||||
if (!qrcodeurl || qrcodeurl === qrcodeRef.current) {
|
||||
throw new Error('未获取到新的二维码');
|
||||
}
|
||||
qrcodeRef.current = qrcodeurl;
|
||||
setQrcode(qrcodeurl);
|
||||
toast.success('二维码已刷新');
|
||||
} catch (error) {
|
||||
const msg = (error as Error).message;
|
||||
toast.error(`刷新二维码失败: ${msg}`);
|
||||
if (msg.includes('QQ_LOGIN_SERVICE_RESTARTING')) {
|
||||
setIsRecoveringLoginService(true);
|
||||
setLoginPhase('reconnecting');
|
||||
toast('QQ 登录服务正在重启,正在生成新的二维码…', { icon: '⏳' });
|
||||
} else if (msg.includes('二维码刷新超时')) {
|
||||
toast.error('未能取得新的二维码,请稍后重试');
|
||||
} else {
|
||||
toast.error(`刷新二维码失败: ${msg}`);
|
||||
}
|
||||
} finally {
|
||||
qrcodeRequestVersionRef.current += 1;
|
||||
isRefreshingQRCodeRef.current = false;
|
||||
setIsRefreshingQRCode(false);
|
||||
}
|
||||
};
|
||||
|
||||
const [showGUIDManager, setShowGUIDManager] = useState(false);
|
||||
const [isRefreshingQRCode, setIsRefreshingQRCode] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const isLoginTransitioning = loginPhase === 'qrcode_scanned' || loginPhase === 'initializing' || loginPhase === 'reconnecting' || isRecoveringLoginService;
|
||||
const pollInterval = isRefreshingQRCode || isLoginTransitioning ? 500 : 3000;
|
||||
const timer = setInterval(() => {
|
||||
onUpdateQrCode();
|
||||
}, 3000);
|
||||
}, pollInterval);
|
||||
|
||||
onUpdateQrCode();
|
||||
onUpdateQQList();
|
||||
|
||||
return () => clearInterval(timer);
|
||||
}, []);
|
||||
}, [isRefreshingQRCode, loginPhase, isRecoveringLoginService]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isRecoveringLoginService) return;
|
||||
const timer = setTimeout(() => {
|
||||
setIsRecoveringLoginService(false);
|
||||
setLoginPhase('offline');
|
||||
setLoginError('QQ 登录服务恢复超时,请稍后重试');
|
||||
}, 180_000);
|
||||
return () => clearTimeout(timer);
|
||||
}, [isRecoveringLoginService]);
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -369,6 +436,9 @@ export default function QQLoginPage () {
|
||||
loginError={parseLoginError(loginError)}
|
||||
qrcode={qrcode}
|
||||
onRefresh={onRefreshQRCode}
|
||||
isRefreshing={isRefreshingQRCode}
|
||||
loginPhase={loginPhase}
|
||||
isRecoveringLoginService={isRecoveringLoginService}
|
||||
/>
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
Reference in new issue
Block a user