Explorar o código

fix: 修复卸载组件时报错的问题

lanjianrong %!s(int64=4) %!d(string=hai) anos
pai
achega
6ae84620e1

+ 19 - 6
src/components/Menu/index.tsx

@@ -1,10 +1,11 @@
 import logo from '@/assets/img/logo.png'
 import logo from '@/assets/img/logo.png'
-import store from '@/store'
-import { deleteUserInfo } from '@/store/modules/user'
+import { DEL_USER_INFO } from '@/store/modules/user/types'
+import { RootState } from '@/store/reducers'
 import { iMenuItem } from '@/types/router'
 import { iMenuItem } from '@/types/router'
 import consts from '@/utils/consts'
 import consts from '@/utils/consts'
 import { Button, Dropdown, Menu } from "antd"
 import { Button, Dropdown, Menu } from "antd"
 import React, { useEffect } from 'react'
 import React, { useEffect } from 'react'
+import { connect } from 'react-redux'
 import { Link, RouteComponentProps, withRouter } from "react-router-dom"
 import { Link, RouteComponentProps, withRouter } from "react-router-dom"
 import { apiLogout } from './api'
 import { apiLogout } from './api'
 import styles from './index.module.scss'
 import styles from './index.module.scss'
@@ -13,10 +14,12 @@ import MenuItem from './MenuItem'
 interface iMenuProps extends RouteComponentProps{
 interface iMenuProps extends RouteComponentProps{
   list: iMenuItem[],
   list: iMenuItem[],
   history: any
   history: any
+  username: string
+  logout: Function
 }
 }
 
 
 const NavSider:React.FC<iMenuProps> = (props)=> {
 const NavSider:React.FC<iMenuProps> = (props)=> {
-  const { list: MeunList, history } = props
+  const { list: MeunList, history, username, logout } = props
   const handleLogout = async () => {
   const handleLogout = async () => {
       const { code = -1 } = await apiLogout()
       const { code = -1 } = await apiLogout()
       if (code === consts.RET_CODE.SUCCESS) {
       if (code === consts.RET_CODE.SUCCESS) {
@@ -25,7 +28,7 @@ const NavSider:React.FC<iMenuProps> = (props)=> {
   }
   }
   useEffect(() => {
   useEffect(() => {
     return () => {
     return () => {
-      store.dispatch(deleteUserInfo())
+      logout()
     }
     }
   }, [])
   }, [])
   return (
   return (
@@ -65,7 +68,7 @@ const NavSider:React.FC<iMenuProps> = (props)=> {
                   </Menu>
                   </Menu>
                 )
                 )
             }} trigger={[ 'click' ]} placement="topRight">
             }} trigger={[ 'click' ]} placement="topRight">
-              <Button size="small" className={styles.bottomBtn} >张三</Button>
+              <Button size="small" className={styles.bottomBtn}>{username}</Button>
             </Dropdown>
             </Dropdown>
           </div>
           </div>
         </div>
         </div>
@@ -73,4 +76,14 @@ const NavSider:React.FC<iMenuProps> = (props)=> {
     </div>
     </div>
   )
   )
 }
 }
-export default withRouter(NavSider)
+const mapStateToProps = (state:RootState) => {
+  return {
+    username: state.user.userInfo.Name
+  }
+}
+const mapDispathToProps = {
+  logout: () => {
+    return { type: DEL_USER_INFO }
+  }
+}
+export default withRouter(connect(mapStateToProps, mapDispathToProps)(NavSider))

+ 4 - 13
src/pages/Login/index.tsx

@@ -13,30 +13,24 @@ import styles from './index.module.scss'
 import './index.scss'
 import './index.scss'
 
 
 // 正常登录Form表单
 // 正常登录Form表单
-interface iProjectInfo {
-  id?: number
-  code?: number
-  name?: string
-}
-
 const initLoginState = {
 const initLoginState = {
   projectInfo: '',
   projectInfo: '',
   projectCode: '',
   projectCode: '',
   visible: false,
   visible: false,
-  user: initUserState
+  user: initUserState.userInfo
 }
 }
 type iState = typeof initLoginState
 type iState = typeof initLoginState
 const mapStateToProps = (state: RootState) => {
 const mapStateToProps = (state: RootState) => {
   return {}
   return {}
 }
 }
 const mapDispathToProps = {
 const mapDispathToProps = {
-  saveUser: (user: iUserInfo) => {
+  saveUser(user: iUserInfo) {
     return updateUserInfo(user)
     return updateUserInfo(user)
   }
   }
 }
 }
 // @ts-ignore
 // @ts-ignore
 @connect(mapStateToProps, mapDispathToProps)
 @connect(mapStateToProps, mapDispathToProps)
-class NormalLoginForm extends Component<Partial<iLoginProps>, iState> {
+class NormalLoginForm extends Component<iLoginProps, iState> {
 
 
   constructor(props: iLoginProps) {
   constructor(props: iLoginProps) {
     super(props)
     super(props)
@@ -51,17 +45,14 @@ class NormalLoginForm extends Component<Partial<iLoginProps>, iState> {
   }
   }
 
 
   componentWillUnmount() {
   componentWillUnmount() {
-    this.props.saveUser(this.state.user)
+    this.props.saveUser!(this.state.user)
   }
   }
   handleProjectCode = async (e: any) => {
   handleProjectCode = async (e: any) => {
-    console.log(this.props)
-
     const projectCode = e.target?.value
     const projectCode = e.target?.value
     const { code = -1, data = [] } = await apiProject(projectCode)
     const { code = -1, data = [] } = await apiProject(projectCode)
     if (code === consts.RET_CODE.SUCCESS) {
     if (code === consts.RET_CODE.SUCCESS) {
       if (data.length && data[0].name) {
       if (data.length && data[0].name) {
         this.setState({ projectInfo: data[0].name })
         this.setState({ projectInfo: data[0].name })
-
       }
       }
     }
     }
 
 

+ 3 - 2
src/router/Guard.tsx

@@ -9,8 +9,9 @@ import { AnyAction } from 'redux'
 
 
 const NavigationGuards:React.FC<NavigationGuardsProps> = props => {
 const NavigationGuards:React.FC<NavigationGuardsProps> = props => {
   const { location, routeConfig, match, check, permission, isLogin } = props
   const { location, routeConfig, match, check, permission, isLogin } = props
-
-  check()
+  useEffect(() => {
+    check()
+  }, [])
   const parentPath: string | null = match?.path || null // 父路由的路径
   const parentPath: string | null = match?.path || null // 父路由的路径
   const targetPath: string | null  = location?.pathname || null// 目标路由的位置
   const targetPath: string | null  = location?.pathname || null// 目标路由的位置
   const targetRoute: RouteModol | null = targetPath && findTargetRoute(parentPath, targetPath, routeConfig) || null
   const targetRoute: RouteModol | null = targetPath && findTargetRoute(parentPath, targetPath, routeConfig) || null

+ 3 - 1
src/store/modules/user/index.ts

@@ -24,7 +24,9 @@ export function userInfoReducer(state = initUserState, action: UserInfoActionTyp
       break
       break
     case DEL_USER_INFO:
     case DEL_USER_INFO:
       delUserInfo()
       delUserInfo()
-      return { ...state }
+      return { ...initUserState }
+    case CHECK_PERMISSION:
+      return { ...action.payload }
     default:
     default:
       return state
       return state
   }
   }

+ 2 - 2
src/types/login.ts

@@ -1,6 +1,6 @@
 interface iLoginProps {
 interface iLoginProps {
   history: any
   history: any
-  saveUser: () => void
+  saveUser?: Function
 }
 }
 
 
 interface iRetrieveFormProps {
 interface iRetrieveFormProps {
@@ -17,5 +17,5 @@ export {
   iLoginProps,
   iLoginProps,
   iRetrieveFormProps,
   iRetrieveFormProps,
   iFromValues
   iFromValues
-}
+};