fix(模块): 修复了一些 bug

This commit is contained in:
cclu 2025-03-07 19:03:52 +08:00
parent 55d599824e
commit 6a7a8985ce
4 changed files with 91 additions and 41 deletions

View File

@ -14,11 +14,8 @@ export default {
], ],
routes: [ routes: [
{ {
id: 1,
key: '1',
path: '/', path: '/',
redirect: '', component: '@/layouts/index',
name: '首页'
}, },
{ {
id: 2, id: 2,
@ -117,6 +114,14 @@ export default {
name: '(404)学生', name: '(404)学生',
path: '/student', path: '/student',
redirect: '', redirect: '',
} },
{
id: 5,
key: '5',
path: '/user/login',
redirect: '',
name: '登录',
component: "@/pages/user/login",
},
] ]
}; };

View File

@ -107,8 +107,17 @@ const UserModel: UserModelType = {
}, },
effects: { effects: {
//登录 //登录
*login({ payload }, { call, put }) { *login({ payload }, { call, put, select }) {
console.log('payload', payload); // 检查当前是否已经在登录状态
const { isLogin } = yield select((state) => state.user);
if (isLogin) {
return { code: 1, message: '您已经登录' };
}
// 检查是否有payload防止自动触发
if (!payload) {
return { code: 1, message: '登录参数无效' };
}
yield put({ yield put({
type: 'save', type: 'save',
@ -117,10 +126,9 @@ const UserModel: UserModelType = {
}, },
}); });
try {
const res: API.LoginResponse = yield call(userLogin, payload); const res: API.LoginResponse = yield call(userLogin, payload);
console.log('res', res);
//登录成功之后设置token并获取用户信息等数据
if (!res.code) { if (!res.code) {
localStorage.setItem('Authorization', res.data.token); localStorage.setItem('Authorization', res.data.token);
@ -130,6 +138,7 @@ const UserModel: UserModelType = {
type: 'concurrent', type: 'concurrent',
}, },
}); });
return res;
} else { } else {
yield put({ yield put({
type: 'save', type: 'save',
@ -137,12 +146,34 @@ const UserModel: UserModelType = {
loginBtnLoading: false, loginBtnLoading: false,
}, },
}); });
return res;
}
} catch (error) {
yield put({
type: 'save',
payload: {
loginBtnLoading: false,
},
});
return { code: 1, message: '登录失败,请稍后重试' };
} }
}, },
//获取用户信息和权限以及菜单 //获取用户信息和权限以及菜单
*getUserInfoAuthorityMenu({ payload }, { call, put }) { *getUserInfoAuthorityMenu({ payload }, { call, put }) {
const { type }: { type: ReqOrder } = payload; const { type }: { type: ReqOrder } = payload;
// 如果当前在登录页面,且不是通过登录操作触发,则不执行后续操作
if (window.location.pathname === '/user/login' && type !== 'concurrent') {
yield put({
type: 'save',
payload: {
layoutWrapperLoading: false,
isLogin: false // 确保在登录页面时重置登录状态
},
});
return false;
}
let userInfoRes: API.UserInfoResponse = { let userInfoRes: API.UserInfoResponse = {
data: {}, data: {},
code: 0, code: 0,
@ -182,6 +213,13 @@ const UserModel: UserModelType = {
}, },
}); });
// 只有在非登录页面时才执行重定向
if (window.location.pathname !== '/user/login') {
yield put({
type: 'resetLoginStatus',
});
}
return false; return false;
} }
@ -245,14 +283,11 @@ const UserModel: UserModelType = {
}, },
}); });
//当前页面不是登录页, 且没有redirect参数的时候再设置redirect参数 //当前页面不是登录页时,才进行重定向
//而且这个redirect参数要包含url pathname和查询字符串参数, 即pathname及其后面的所有字符
if (window.location.pathname !== '/user/login') { if (window.location.pathname !== '/user/login') {
if (!/redirect=/.test(window.location.search)) {
const redirectValue = `${window.location.pathname}${window.location.search}`; const redirectValue = `${window.location.pathname}${window.location.search}`;
history.push(`/user/login?redirect=${encodeURIComponent(redirectValue)}`); history.push(`/user/login?redirect=${encodeURIComponent(redirectValue)}`);
} }
}
}, },
}, },
reducers: { reducers: {

View File

@ -1,5 +1,6 @@
const authority: PageAuthority = { const authority: PageAuthority = {
'/user/login': [], // 登录页面不需要权限验证
'/': [ '/': [
'/', '/',
], ],

View File

@ -1,6 +1,6 @@
import type { FC } from 'react'; import type { FC } from 'react';
import { useState } from 'react'; import { useState } from 'react';
import { Col, Divider, Row, Space, Typography } from 'antd'; import { Col, Divider, Row, Space, Typography, message } from 'antd';
import { connect, Link } from 'umi'; import { connect, Link } from 'umi';
import { LockFilled, UserOutlined } from '@ant-design/icons'; import { LockFilled, UserOutlined } from '@ant-design/icons';
import { ProFormText, LoginForm } from '@ant-design/pro-components'; import { ProFormText, LoginForm } from '@ant-design/pro-components';
@ -8,20 +8,29 @@ import Footer from '@/components/Footer';
import { retrieveCaptcha } from '@/services/user'; import { retrieveCaptcha } from '@/services/user';
import type { UserConnectedProps } from '@/models/user'; import type { UserConnectedProps } from '@/models/user';
import styles from './index.less'; import styles from './index.less';
const logo = '@/assets/logo.svg' import logo from '@/assets/logo.svg';
const loginBg = '@/assets/login-bg.png' import loginBg from '@/assets/login-bg.png';
// import logo from '@/assets/logo.svg'
// import loginBg from '@/assets/login-bg.png'
const Index: FC<UserConnectedProps> = (props) => { const Index: FC<UserConnectedProps> = (props) => {
const [type, setType] = useState<string>('account'); const [type, setType] = useState<string>('account');
const { user, dispatch } = props; const { user, dispatch } = props;
const handleSubmit = async (values: API.LoginParams) => { const handleSubmit = async (values: API.LoginParams) => {
dispatch?.({ try {
const response = await dispatch?.({
type: 'user/login', type: 'user/login',
payload: values, payload: values,
}); });
if (response?.code) {
message.error(response.message || '登录失败,请检查账号密码');
return false;
}
} catch (error) {
console.error('Login failed:', error);
message.error('登录失败,请稍后重试');
return false;
}
}; };
return ( return (