diff --git a/config/config.ts b/config/config.ts index 75ff48b..371ee20 100644 --- a/config/config.ts +++ b/config/config.ts @@ -14,11 +14,8 @@ export default { ], routes: [ { - id: 1, - key: '1', path: '/', - redirect: '', - name: '首页' + component: '@/layouts/index', }, { id: 2, @@ -40,7 +37,7 @@ export default { key: '2-1-1', name: '关于你1', path: '/about/u/index', - component:"@/pages/about/u/index", + component: "@/pages/about/u/index", redirect: '', pid: 21 }, @@ -49,7 +46,7 @@ export default { key: '2-1-2', name: '关于你2', path: '/about/u/2', - component:"@/pages/about/u/$id", + component: "@/pages/about/u/$id", redirect: '', pid: 21 } @@ -61,7 +58,7 @@ export default { path: '/about/m', redirect: '', name: '(页面元素权限)关于我', - component:"@/pages/about/m", + component: "@/pages/about/m", pid: 2 }, { @@ -70,7 +67,7 @@ export default { path: '/about/um', redirect: '', name: '关于你和我', - component:"@/pages/about/um", + component: "@/pages/about/um", pid: 2 } ] @@ -88,7 +85,7 @@ export default { path: '/teacher/u', redirect: '', name: '(403)关于你教师', - component:"@/pages/teacher/u", + component: "@/pages/teacher/u", pid: 3 }, { @@ -97,7 +94,7 @@ export default { path: '/teacher/m', redirect: '', name: '关于我教师', - component:"@/pages/teacher/m", + component: "@/pages/teacher/m", pid: 3 }, { @@ -106,7 +103,7 @@ export default { path: '/teacher/um', redirect: '', name: '关于你和我教师', - component:"@/pages/teacher/um", + component: "@/pages/teacher/um", pid: 3 } ] @@ -117,6 +114,14 @@ export default { name: '(404)学生', path: '/student', redirect: '', - } + }, + { + id: 5, + key: '5', + path: '/user/login', + redirect: '', + name: '登录', + component: "@/pages/user/login", + }, ] }; diff --git a/src/models/user.ts b/src/models/user.ts index 834344b..d200056 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -107,8 +107,17 @@ const UserModel: UserModelType = { }, effects: { //登录 - *login({ payload }, { call, put }) { - console.log('payload', payload); + *login({ payload }, { call, put, select }) { + // 检查当前是否已经在登录状态 + const { isLogin } = yield select((state) => state.user); + if (isLogin) { + return { code: 1, message: '您已经登录' }; + } + + // 检查是否有payload,防止自动触发 + if (!payload) { + return { code: 1, message: '登录参数无效' }; + } yield put({ type: 'save', @@ -117,32 +126,54 @@ const UserModel: UserModelType = { }, }); - const res: API.LoginResponse = yield call(userLogin, payload); - console.log('res', res); + try { + const res: API.LoginResponse = yield call(userLogin, payload); - //登录成功之后设置token并获取用户信息等数据 - if (!res.code) { - localStorage.setItem('Authorization', res.data.token); + if (!res.code) { + localStorage.setItem('Authorization', res.data.token); - yield put({ - type: 'getUserInfoAuthorityMenu', - payload: { - type: 'concurrent', - }, - }); - } else { + yield put({ + type: 'getUserInfoAuthorityMenu', + payload: { + type: 'concurrent', + }, + }); + return res; + } else { + yield put({ + type: 'save', + payload: { + loginBtnLoading: false, + }, + }); + return res; + } + } catch (error) { yield put({ type: 'save', payload: { loginBtnLoading: false, }, }); + return { code: 1, message: '登录失败,请稍后重试' }; } }, //获取用户信息和权限以及菜单 *getUserInfoAuthorityMenu({ payload }, { call, put }) { 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 = { data: {}, code: 0, @@ -182,6 +213,13 @@ const UserModel: UserModelType = { }, }); + // 只有在非登录页面时才执行重定向 + if (window.location.pathname !== '/user/login') { + yield put({ + type: 'resetLoginStatus', + }); + } + return false; } @@ -245,13 +283,10 @@ const UserModel: UserModelType = { }, }); - //当前页面不是登录页, 且没有redirect参数的时候再设置redirect参数 - //而且这个redirect参数要包含url pathname和查询字符串参数, 即pathname及其后面的所有字符 + //当前页面不是登录页时,才进行重定向 if (window.location.pathname !== '/user/login') { - if (!/redirect=/.test(window.location.search)) { - const redirectValue = `${window.location.pathname}${window.location.search}`; - history.push(`/user/login?redirect=${encodeURIComponent(redirectValue)}`); - } + const redirectValue = `${window.location.pathname}${window.location.search}`; + history.push(`/user/login?redirect=${encodeURIComponent(redirectValue)}`); } }, }, diff --git a/src/pages/authority.ts b/src/pages/authority.ts index 77bec68..cc2ff23 100644 --- a/src/pages/authority.ts +++ b/src/pages/authority.ts @@ -1,5 +1,6 @@ const authority: PageAuthority = { + '/user/login': [], // 登录页面不需要权限验证 '/': [ '/', ], diff --git a/src/pages/user/login/index.tsx b/src/pages/user/login/index.tsx index aa1201e..77e8d50 100644 --- a/src/pages/user/login/index.tsx +++ b/src/pages/user/login/index.tsx @@ -1,6 +1,6 @@ import type { FC } 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 { LockFilled, UserOutlined } from '@ant-design/icons'; import { ProFormText, LoginForm } from '@ant-design/pro-components'; @@ -8,20 +8,29 @@ import Footer from '@/components/Footer'; import { retrieveCaptcha } from '@/services/user'; import type { UserConnectedProps } from '@/models/user'; import styles from './index.less'; -const logo = '@/assets/logo.svg' -const loginBg = '@/assets/login-bg.png' -// import logo from '@/assets/logo.svg' -// import loginBg from '@/assets/login-bg.png' +import logo from '@/assets/logo.svg'; +import loginBg from '@/assets/login-bg.png'; const Index: FC = (props) => { const [type, setType] = useState('account'); const { user, dispatch } = props; const handleSubmit = async (values: API.LoginParams) => { - dispatch?.({ - type: 'user/login', - payload: values, - }); + try { + const response = await dispatch?.({ + type: 'user/login', + payload: values, + }); + + if (response?.code) { + message.error(response.message || '登录失败,请检查账号密码'); + return false; + } + } catch (error) { + console.error('Login failed:', error); + message.error('登录失败,请稍后重试'); + return false; + } }; return (