Release NapCat / validate-version (push) Canceled after 0s
Release NapCat / Build-Framework (push) Canceled after 0s
Release NapCat / Build-Shell (push) Canceled after 0s
Release NapCat / Download-QNX64 (push) Canceled after 0s
Release NapCat / release-napcat (push) Canceled after 0s
* feat: 优化离线重连机制,增加前端登录错误提示与二维码刷新功能 - 增加全局掉线检测弹窗 - 增强登录错误解析,支持显示 serverErrorCode 和 message - 优化二维码登录 UI,错误时显示详细原因并提供大按钮重新获取 - 核心层解耦,通过事件抛出 KickedOffLine 通知 - 支持前端点击刷新二维码接口 * feat: 新增看门狗汪汪汪 * cp napcat-shell-loader/launcher-win.bat * refactor: 重构重启流程,移除旧的重启逻辑,新增基于 WebUI 的重启请求处理 * fix: 刷新二维码清楚错误信息
239 lines
7.1 KiB
TypeScript
239 lines
7.1 KiB
TypeScript
import { Button } from '@heroui/button';
|
|
import { CardBody, CardHeader } from '@heroui/card';
|
|
import { Image } from '@heroui/image';
|
|
import { Tab, Tabs } from '@heroui/tabs';
|
|
import { useEffect, useRef, useState } from 'react';
|
|
import { toast } from 'react-hot-toast';
|
|
import { useNavigate } from 'react-router-dom';
|
|
|
|
import logo from '@/assets/images/logo.png';
|
|
|
|
import HoverEffectCard from '@/components/effect_card';
|
|
import { title } from '@/components/primitives';
|
|
import QrCodeLogin from '@/components/qr_code_login';
|
|
import QuickLogin from '@/components/quick_login';
|
|
import type { QQItem } from '@/components/quick_login';
|
|
import { ThemeSwitch } from '@/components/theme-switch';
|
|
|
|
import QQManager from '@/controllers/qq_manager';
|
|
import useDialog from '@/hooks/use-dialog';
|
|
import PureLayout from '@/layouts/pure';
|
|
import { motion } from 'motion/react';
|
|
|
|
const parseLoginError = (errorStr: string) => {
|
|
if (errorStr.startsWith('登录失败: ')) {
|
|
const jsonPart = errorStr.substring('登录失败: '.length);
|
|
|
|
try {
|
|
const parsed = JSON.parse(jsonPart);
|
|
|
|
if (Array.isArray(parsed) && parsed[1]) {
|
|
const info = parsed[1];
|
|
const codeStr = info.serverErrorCode ? ` (错误码: ${info.serverErrorCode})` : '';
|
|
|
|
return `${info.message || errorStr}${codeStr}`;
|
|
}
|
|
} catch (e) {
|
|
// 忽略解析错误
|
|
}
|
|
}
|
|
|
|
return errorStr;
|
|
};
|
|
|
|
export default function QQLoginPage () {
|
|
const navigate = useNavigate();
|
|
const dialog = useDialog();
|
|
const [uinValue, setUinValue] = useState<string>('');
|
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
const [qrcode, setQrcode] = useState<string>('');
|
|
const [loginError, setLoginError] = useState<string>('');
|
|
const lastErrorRef = useRef<string>('');
|
|
const [qqList, setQQList] = useState<(QQItem | LoginListItem)[]>([]);
|
|
const [refresh, setRefresh] = useState<boolean>(false);
|
|
const firstLoad = useRef<boolean>(true);
|
|
const onSubmit = async () => {
|
|
if (!uinValue) {
|
|
toast.error('请选择快捷登录的QQ');
|
|
|
|
return;
|
|
}
|
|
setIsLoading(true);
|
|
try {
|
|
await QQManager.setQuickLogin(uinValue);
|
|
} catch (error) {
|
|
const msg = (error as Error).message;
|
|
|
|
toast.error(`快速登录QQ失败: ${msg}`);
|
|
} finally {
|
|
setTimeout(() => {
|
|
setIsLoading(false);
|
|
}, 1000);
|
|
}
|
|
};
|
|
|
|
const onUpdateQrCode = async () => {
|
|
if (firstLoad.current) setIsLoading(true);
|
|
try {
|
|
const data = await QQManager.checkQQLoginStatusWithQrcode();
|
|
|
|
if (firstLoad.current) {
|
|
setIsLoading(false);
|
|
firstLoad.current = false;
|
|
}
|
|
if (data.isLogin) {
|
|
toast.success('QQ登录成功');
|
|
navigate('/', { replace: true });
|
|
} else {
|
|
setQrcode(data.qrcodeurl);
|
|
if (data.loginError && data.loginError !== lastErrorRef.current) {
|
|
lastErrorRef.current = data.loginError;
|
|
setLoginError(data.loginError);
|
|
const friendlyMsg = parseLoginError(data.loginError);
|
|
|
|
dialog.alert({
|
|
title: '登录失败',
|
|
content: friendlyMsg,
|
|
confirmText: '确定',
|
|
});
|
|
} else if (!data.loginError) {
|
|
lastErrorRef.current = '';
|
|
setLoginError('');
|
|
}
|
|
}
|
|
} catch (error) {
|
|
const msg = (error as Error).message;
|
|
|
|
toast.error(`获取二维码失败: ${msg}`);
|
|
}
|
|
};
|
|
|
|
const onUpdateQQList = async () => {
|
|
setRefresh(true);
|
|
try {
|
|
const data = await QQManager.getQQQuickLoginListNew();
|
|
setQQList(data);
|
|
} catch (_error) {
|
|
try {
|
|
const data = await QQManager.getQQQuickLoginList();
|
|
|
|
const qqList = data.map((item) => ({
|
|
uin: item,
|
|
}));
|
|
|
|
setQQList(qqList);
|
|
} catch (_error) {
|
|
const msg = (_error as Error).message;
|
|
|
|
toast.error(`获取QQ列表失败: ${msg}`);
|
|
}
|
|
} finally {
|
|
setRefresh(false);
|
|
}
|
|
};
|
|
|
|
const handleSelectionChange: React.ChangeEventHandler<HTMLSelectElement> = (
|
|
e
|
|
) => {
|
|
setUinValue(e.target.value);
|
|
};
|
|
|
|
const onRefreshQRCode = async () => {
|
|
try {
|
|
lastErrorRef.current = '';
|
|
setLoginError('');
|
|
await QQManager.refreshQRCode();
|
|
toast.success('已发送刷新请求');
|
|
} catch (error) {
|
|
const msg = (error as Error).message;
|
|
toast.error(`刷新二维码失败: ${msg}`);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
const timer = setInterval(() => {
|
|
onUpdateQrCode();
|
|
}, 3000);
|
|
|
|
onUpdateQrCode();
|
|
onUpdateQQList();
|
|
|
|
return () => clearInterval(timer);
|
|
}, []);
|
|
|
|
return (
|
|
<>
|
|
<title>QQ登录 - NapCat WebUI</title>
|
|
<PureLayout>
|
|
<motion.div
|
|
initial={{ opacity: 0, y: 20, scale: 0.95 }}
|
|
animate={{ opacity: 1, y: 0, scale: 1 }}
|
|
transition={{ duration: 0.5, type: 'spring', stiffness: 120, damping: 20 }}
|
|
className='w-[608px] max-w-full py-8 px-2 md:px-8 overflow-hidden'
|
|
>
|
|
<HoverEffectCard
|
|
className='items-center gap-4 pt-0 pb-6 bg-default-50'
|
|
maxXRotation={3}
|
|
maxYRotation={3}
|
|
>
|
|
<CardHeader className='inline-block max-w-lg text-center justify-center'>
|
|
<div className='flex items-center justify-center w-full gap-2 pt-10'>
|
|
<Image alt='logo' height='7em' src={logo} />
|
|
<div>
|
|
<span className={title()}>Web </span>
|
|
<span className={title({ color: 'violet' })}>
|
|
Login
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<ThemeSwitch className='absolute right-4 top-4' />
|
|
</CardHeader>
|
|
|
|
<CardBody className='flex gap-5 p-10 pt-0'>
|
|
<Tabs
|
|
fullWidth
|
|
classNames={{
|
|
tabList: 'shadow-sm dark:shadow-none',
|
|
}}
|
|
isDisabled={isLoading}
|
|
size='lg'
|
|
>
|
|
<Tab key='shortcut' title='快速登录'>
|
|
<QuickLogin
|
|
handleSelectionChange={handleSelectionChange}
|
|
isLoading={isLoading}
|
|
qqList={qqList}
|
|
refresh={refresh}
|
|
selectedQQ={uinValue}
|
|
onSubmit={onSubmit}
|
|
onUpdateQQList={onUpdateQQList}
|
|
/>
|
|
</Tab>
|
|
<Tab key='qrcode' title='扫码登录'>
|
|
<QrCodeLogin
|
|
loginError={parseLoginError(loginError)}
|
|
qrcode={qrcode}
|
|
onRefresh={onRefreshQRCode}
|
|
/>
|
|
</Tab>
|
|
</Tabs>
|
|
<Button
|
|
className='w-fit mx-auto'
|
|
variant='light'
|
|
color='primary'
|
|
onPress={() => {
|
|
navigate('/web_login', {
|
|
replace: true,
|
|
});
|
|
}}
|
|
>
|
|
返回 Web Login
|
|
</Button>
|
|
</CardBody>
|
|
</HoverEffectCard>
|
|
</motion.div>
|
|
</PureLayout>
|
|
</>
|
|
);
|
|
}
|