浏览代码

fix: 成功修改密码后,退出账号重新登录

lanjianrong 3 年之前
父节点
当前提交
464f04a56a
共有 3 个文件被更改,包括 25 次插入16 次删除
  1. 14 13
      src/app.tsx
  2. 9 2
      src/pages/System/AdminUpdate/index.tsx
  3. 2 1
      src/utils/consts.ts

+ 14 - 13
src/app.tsx

@@ -9,8 +9,6 @@ import RightContent from '@/components/RightContent'
 import consts from './utils/consts'
 import { queryCurrentUser } from './services/api/login'
 
-const loginPath = '/user/login'
-
 /** 获取用户信息比较慢的时候会展示一个 loading */
 export const initialStateConfig = {
   loading: <PageLoading />
@@ -29,12 +27,12 @@ export async function getInitialState(): Promise<{
       const msg = await queryCurrentUser()
       return msg.data
     } catch (error) {
-      history.push(loginPath)
+      history.push(consts.loginPath)
     }
     return undefined
   }
   // 如果是登录页面,不执行
-  if (history.location.pathname !== loginPath) {
+  if (history.location.pathname !== consts.loginPath) {
     const currentUser = await fetchUserInfo()
     return {
       fetchUserInfo,
@@ -69,8 +67,8 @@ const codeMaps = {
 const authHeaderInterceptor = (url: string, options: RequestOptionsInit) => {
   const token = window.localStorage.getItem('TOKEN_ID')
   // 如果是登录页面,不执行
-  if (!token && history.location.pathname !== loginPath) {
-    return history.push(loginPath)
+  if (!token && history.location.pathname !== consts.loginPath) {
+    return history.replace(consts.loginPath)
   }
   // const authHeader = { Authorization: `Bearer ${JSON.parse(token)}` }
   const authHeader = { Authorization: `Bearer ${token}` }
@@ -95,14 +93,14 @@ export const request: RequestConfig = {
       }
       if (
         consts.TOKEN_INVALID_CODE.includes(response.code) &&
-        window.location.pathname !== loginPath
+        window.location.pathname !== consts.loginPath
       ) {
         notification.error({
           message: '用户信息过期',
           description: '请重新登录'
         })
         history.replace({
-          pathname: loginPath,
+          pathname: consts.loginPath,
           search: stringify({
             redirect: window.location.pathname
           })
@@ -150,11 +148,14 @@ export const layout: RunTimeLayoutConfig = ({ initialState }) => {
     //   content: initialState?.currentUser?.name
     // },
     onPageChange: () => {
-      // const { location } = history
-      // // 如果没有登录,重定向到 login
-      // if (!initialState?.currentUser && location.pathname !== loginPath) {
-      //   history.push(loginPath)
-      // }
+      if (!initialState?.currentUser?.ID && location.pathname !== consts.loginPath) {
+        history.replace({
+          pathname: consts.loginPath,
+          search: stringify({
+            redirect: history.location.pathname
+          })
+        })
+      }
     },
     menuHeaderRender: undefined,
     itemRender: (route, _, routes) => {

+ 9 - 2
src/pages/System/AdminUpdate/index.tsx

@@ -1,14 +1,21 @@
 import { updateSetting } from '@/services/api/system'
+import consts from '@/utils/consts'
 import { PageContainer } from '@ant-design/pro-layout'
 import { Card, Form, Input, Button, message } from 'antd'
 import React from 'react'
-import { useRequest } from 'umi'
+import { history, useRequest } from 'umi'
 
 const AdminUpdate = () => {
   const [form] = Form.useForm()
   const { run: tryUpdate } = useRequest(updateSetting, {
     manual: true,
-    onSuccess: () => message.success('更新成功'),
+    onSuccess: () => {
+      window.localStorage.removeItem('TOKEN_ID')
+      message.info('当前用户密码已修改,请重新登录。')
+      history.replace({
+        pathname: consts.loginPath
+      })
+    },
     onError: e => {
       console.log(e)
     }

+ 2 - 1
src/utils/consts.ts

@@ -3,5 +3,6 @@ export default {
     SUCCESS: 0,
     ERROR: -1
   },
-  TOKEN_INVALID_CODE: [2]
+  TOKEN_INVALID_CODE: [2],
+  loginPath: '/user/login'
 }