diff --git a/packages/napcat-webui-frontend/src/pages/dashboard/config/index.tsx b/packages/napcat-webui-frontend/src/pages/dashboard/config/index.tsx index 889ffd46b..17dd49ab6 100644 --- a/packages/napcat-webui-frontend/src/pages/dashboard/config/index.tsx +++ b/packages/napcat-webui-frontend/src/pages/dashboard/config/index.tsx @@ -6,7 +6,8 @@ import { useNavigate, useSearchParams } from 'react-router-dom'; import key from '@/const/key'; -import ChangePasswordCard from './change_password'; +//把修改密码移动到WebUI栏目中 +//import ChangePasswordCard from './change_password'; import LoginConfigCard from './login'; import OneBotConfigCard from './onebot'; import ServerConfigCard from './server'; @@ -101,11 +102,16 @@ export default function ConfigPage () { + //把修改密码移动到WebUI栏目中 + { + /* + */ + } diff --git a/packages/napcat-webui-frontend/src/pages/dashboard/config/webui.tsx b/packages/napcat-webui-frontend/src/pages/dashboard/config/webui.tsx index 90e53c04d..c4e27fc5f 100644 --- a/packages/napcat-webui-frontend/src/pages/dashboard/config/webui.tsx +++ b/packages/napcat-webui-frontend/src/pages/dashboard/config/webui.tsx @@ -1,8 +1,10 @@ import { Button } from '@heroui/button'; +import { Input } from '@heroui/input'; import { useLocalStorage } from '@uidotdev/usehooks'; import { useEffect, useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; import toast from 'react-hot-toast'; +import { useNavigate } from 'react-router-dom'; import key from '@/const/key'; @@ -50,6 +52,39 @@ const WebUIConfigCard = () => { const [registrationOptions, setRegistrationOptions] = useState(null); const [isLoadingOptions, setIsLoadingOptions] = useState(false); + // 修改密码独立表单 + const navigate = useNavigate(); + const [, setToken] = useLocalStorage(key.token, ''); + const { + control: pwdControl, + handleSubmit: handlePwdSubmit, + formState: { isSubmitting: isPwdSubmitting, errors: pwdErrors }, + watch, + } = useForm<{ + oldToken: string; + newToken: string; + }>({ + defaultValues: { + oldToken: '', + newToken: '', + }, + }); + const oldTokenValue = watch('oldToken'); + + // 修复类型错误:显式包装为无参函数,满足 onPress 的类型要求 + const onPasswordSubmit = handlePwdSubmit(async (data) => { + try { + await WebUIManager.changePassword(data.oldToken, data.newToken); + toast.success('修改成功'); + setToken(''); + localStorage.removeItem(key.token); + navigate('/web_login'); + } catch (error) { + const msg = (error as Error).message; + toast.error(`修改失败: ${msg}`); + } + }); + // 预先获取注册选项(可以在任何时候调用) const preloadRegistrationOptions = async () => { setIsLoadingOptions(true); @@ -90,6 +125,86 @@ const WebUIConfigCard = () => { return ( <> WebUI配置 - NapCat WebUI + + {/* 修改密码区块 - 移到顶部 */} +
+
修改密码
+ { + if (!value || value.trim().length === 0) { + return '旧密码不能为空'; + } + return true; + }, + }} + render={({ field }) => ( + + )} + /> + { + if (!value || value.trim().length === 0) { + return '新密码不能为空'; + } + if (value.trim().length !== value.length) { + return '新密码不能包含前后空格'; + } + if (value === oldTokenValue) { + return '新密码不能与旧密码相同'; + } + if (!/[a-zA-Z]/.test(value)) { + return '新密码必须包含字母'; + } + if (!/[0-9]/.test(value)) { + return '新密码必须包含数字'; + } + return true; + }, + }} + render={({ field }) => ( + + )} + /> + {/* 自定义确认按钮,无取消按钮;onPress 调用 onPasswordSubmit(已通过 handlePwdSubmit 包装,类型兼容) */} +
+ +
+
+
背景图
{ )}
+ { ); }; -export default WebUIConfigCard; +export default WebUIConfigCard; \ No newline at end of file