Compare commits

...

2 Commits

Author SHA1 Message Date
Shu Guang
b763c47e48 fix: 修复登录逻辑 2025-05-18 18:56:54 +08:00
Shu Guang
0b06d3dd0e fix: 修复我的图表 & 头像上传 2025-05-18 18:56:40 +08:00
6 changed files with 77 additions and 31 deletions

View File

@ -1,11 +1,13 @@
export default [
{path: '/user',layout: false,routes: [
{ name: '登录', path: '/user/login', component: './User/Login' },
{ name: '注册', path: '/user/register', component: './User/Register' }
{ name: '注册', path: '/user/register', component: './User/Register' },
{ name :"权限引导",path: '/user/access', component: './Access'},
]},
{path:"/", redirect: "/home"},
{path:"/", redirect: "/user/access"},
{ path: '/home', name :"首页", icon: "PieChartOutlined", component: './HomePage',access: 'canAdmin', },
// { path: '/access', name :"权限引导", icon: "PieChartOutlined", component: './Access',hidden: true},
{ path: '/add_chart', name :"智能分析", icon: "barChart", component: './AddChart' },
{ path: '/add_async', name: "异步分析", icon: "DotChartOutlined", component: './AddChartAsync' },
{ path: '/my_chart', name: "我的图表", icon: "PictureOutlined", component: './MyChart' },

View File

@ -0,0 +1,22 @@
// 负责处理默认重定向
import { useEffect } from 'react';
import { useModel } from 'umi';
import { history } from '@@/core/history';
export default () => {
const { initialState } = useModel('@@initialState');
const { currentUser } = initialState || {};
useEffect(() => {
if (currentUser) {
// 获取用户角色,优先使用 currentUser.userRole
const userRole = currentUser?.userRole || currentUser?.data?.userRole;
// 根据用户角色重定向到不同页面
history.push(userRole === 'user' ? '/add_chart' : '/home');
} else {
history.push('/user/login');
}
}, [currentUser]);
return <div>Loading...</div>;
};

View File

@ -1,4 +1,4 @@
import { listChartByPageUsingPost } from '@/services/hebi/chartController';
import { listChartByPageUsingPost,listMyChartByPageUsingPost } from '@/services/hebi/chartController';
import { useModel } from '@@/exports';
import { Avatar, Card, Input, List, message, Result } from 'antd';
import ReactECharts from 'echarts-for-react';
@ -30,7 +30,7 @@ const MyChartPage: React.FC = () => {
const loadData = async () => {
setLoading(true);
try {
const res = await listChartByPageUsingPost(searchParams);
const res = currentUser?.userRole==='user'?await listMyChartByPageUsingPost(searchParams):await listChartByPageUsingPost(searchParams);
if (res.data) {
setChartList(res.data.records ?? []);
setTotal(res.data.total ?? 0);

View File

@ -25,6 +25,7 @@ import { useModel } from '@umijs/max';
import type { UploadProps } from 'antd/es/upload';
import { getUserByIdUsingGet, updateUserUsingPost } from '@/services/hebi/userController';
import { countChartsUsingGet } from '@/services/hebi/chartController'; // 新增
import { uploadFileUsingPost } from '@/services/hebi/fileController';
import dayjs from 'dayjs'; // 新增
const { TabPane } = Tabs;
@ -37,7 +38,7 @@ const UserInfo: React.FC = () => {
const [loading, setLoading] = useState(false);
const [form] = Form.useForm();
const [apiCallCount, setApiCallCount] = useState<number>(0); // 新增
const [avatarUrl, setAvatarUrl] = useState<string>();
// 拉取用户信息
useEffect(() => {
const fetchUserInfo = async () => {
@ -73,7 +74,7 @@ const UserInfo: React.FC = () => {
const res = await updateUserUsingPost({
id: userInfo.id,
userName: values.userName,
userAvatar: userInfo.userAvatar,
userAvatar: avatarUrl||userInfo.userAvatar,
userProfile: values.phone, // 假设 userProfile 存手机号(如有 phone 字段请替换)
// 其他字段如有需要可补充
});
@ -91,6 +92,39 @@ const UserInfo: React.FC = () => {
}
};
const handleUploadChange = async (info: any) => {
if (info.file.status === 'uploading') {
return;
}
if (info.file.status === 'done') {
const response = info.file.response;
if (response.code === 0) {
setAvatarUrl(response.data);
message.success('头像上传成功');
// 更新表单字段值
form.setFieldValue('userAvatar', response.data);
} else {
message.error(response.message || '头像上传失败');
}
}
};
const customRequest = async ({ file, onSuccess, onError }: any) => {
try {
const res = await uploadFileUsingPost(file, {
file,
biz: 'user_avatar',
});
if (res.code === 0) {
onSuccess(res);
} else {
onError(new Error(res.message));
}
} catch (error) {
onError(error);
}
};
// 处理头像上传
const handleAvatarUpload: UploadProps['onChange'] = async (info) => {
if (info.file.status === 'uploading') {
@ -124,12 +158,14 @@ const UserInfo: React.FC = () => {
<Upload
name="avatar"
showUploadList={false}
onChange={handleAvatarUpload}
// onChange={handleAvatarUpload}
customRequest={customRequest}
onChange={handleUploadChange}
>
<Space direction="vertical" size="large">
<Avatar
size={120}
src={userInfo?.userAvatar}
src={avatarUrl || userInfo?.userAvatar}
icon={<UserOutlined />}
/>
<Button icon={<UploadOutlined />} loading={loading}>

View File

@ -94,10 +94,11 @@ useEffect(()=>{
message.success(defaultLoginSuccessMessage);
await fetchUserInfo();
// 登录成功后,设置刷新标记并刷新页面
const urlParams = new URL(window.location.href).searchParams;
history.push(urlParams.get('redirect') || '/');
// const urlParams = new URL(window.location.href).searchParams;
// history.push(urlParams.get('redirect') || '/');
sessionStorage.setItem('avatar_refreshed', '1');
window.location.href = urlParams.get('redirect') || '/';
window.location.href = '/user/access';
history.push('/user/access');
return;
} else {
message.error(res.message);

View File

@ -1,5 +1,6 @@
import { Footer } from '@/components';
import { userRegisterUsingPost } from '@/services/hebi/userController';
import { uploadFileUsingPost } from '@/services/hebi/fileController';
import { LockOutlined, PictureOutlined, UserOutlined } from '@ant-design/icons';
import { ProForm, ProFormText } from '@ant-design/pro-components';
import { Helmet, history, Link } from '@umijs/max';
@ -62,7 +63,7 @@ const Register: React.FC = () => {
return;
}
const res = await userRegisterUsingPost({ ...values, userAvatar: avatarUrl });
const res = await userRegisterUsingPost({ ...values, userAvatar: 'https://img-oss.shuguangwl.com/2025/05/18/6829ae97cee35.png' });
if (res.code === 0) {
message.success('注册成功!');
history.push('/user/login');
@ -81,6 +82,8 @@ const Register: React.FC = () => {
</div>
);
return (
<div className={styles.container}>
<Helmet>
@ -111,25 +114,7 @@ const Register: React.FC = () => {
},
}}
>
<ProForm.Item
name="userAvatar"
rules={[{ required: true, message: '请上传头像' }]}
className={styles.avatarUploader}
>
<Upload
name="avatar"
listType="picture-card"
showUploadList={false}
action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
onChange={(info) => {
if (info.file.status === 'done') {
setAvatarUrl(info.file.response.url);
}
}}
>
{avatarUrl ? <Avatar src={avatarUrl} size={64} /> : uploadButton}
</Upload>
</ProForm.Item>
<ProFormText
name="userAccount"