diff --git a/src/user/profile/profile.tsx b/src/user/profile/profile.tsx index 8d16019..cc9b799 100644 --- a/src/user/profile/profile.tsx +++ b/src/user/profile/profile.tsx @@ -34,7 +34,7 @@ interface InputEvent { } function Profile() { const formId = Number(router?.params.id) - const {user, updateUser} = useUser() + const {user, updateUser, loading} = useUser() const [sex, setSex] = useState() const [FormData, setFormData] = useState( @@ -49,16 +49,27 @@ function Profile() { comments: undefined } ) + const [pageLoading, setPageLoading] = useState(true) const reload = () => { // 获取数据字典 pageDictData({limit: 200}).then(res => { setSex(res?.list.filter((item) => item.dictCode === 'sex')) + }).catch(err => { + console.error('获取数据字典失败:', err) }) // 获取用户信息 getUserInfo().then((data) => { // 更新表单数据 setFormData(data); + }).catch(err => { + console.error('获取用户信息失败:', err) + Taro.showToast({ + title: '获取用户信息失败', + icon: 'none' + }) + }).finally(() => { + setPageLoading(false) }) } @@ -140,9 +151,12 @@ function Profile() { }); } + // 等待 useUser 初始化完成后再加载数据 useEffect(() => { - reload() - }, []); + if (!loading) { + reload() + } + }, [loading]); // 监听 useUser hook 中的用户信息变化,同步更新表单数据 useEffect(() => { @@ -151,6 +165,15 @@ function Profile() { } }, [user]); + // 加载中显示 + if (loading || pageLoading) { + return ( + + 加载中... + + ) + } + return ( <>
@@ -223,14 +246,14 @@ function Profile() { label="备注信息" name="comments" initialValue={FormData.comments} - rules={[{message: '备注信息'}]} + rules={[{message: '请输入备注信息'}]} > -