From a0754cde00f12c0a499fe6f459181a48299a5c2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BA=E7=8B=A8?= <18119604035@163.com> Date: Mon, 17 Apr 2023 11:34:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=98=E5=82=A8=E9=83=A8=E9=97=A8=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/compenents/header/index.tsx | 5 ++++- src/store/user/loginUserSlice.ts | 3 ++- src/utils/index.ts | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/compenents/header/index.tsx b/src/compenents/header/index.tsx index 80e414a..3ea76a0 100644 --- a/src/compenents/header/index.tsx +++ b/src/compenents/header/index.tsx @@ -8,6 +8,7 @@ import { logoutAction, saveCurrentDepId, } from "../../store/user/loginUserSlice"; +import { setDepKey, setDepName, getDepName } from "../../utils/index"; import { ChangePasswordModel } from "../change-password"; import { UserInfoModel } from "../user-info"; import { ExclamationCircleFilled } from "@ant-design/icons"; @@ -32,7 +33,7 @@ export const Header: React.FC = () => { useEffect(() => { if (departments.length > 0) { - setCurrentDepartment(departments[0].name); + setCurrentDepartment(getDepName() || departments[0].name); const arr: any = [ { key: "1", @@ -129,6 +130,8 @@ export const Header: React.FC = () => { onOk() { setCurrentDepartment(name); dispatch(saveCurrentDepId(Number(key))); + setDepKey(key); + setDepName(name); const box = [...departments]; const arr: any = [ { diff --git a/src/store/user/loginUserSlice.ts b/src/store/user/loginUserSlice.ts index ac24a80..237e761 100644 --- a/src/store/user/loginUserSlice.ts +++ b/src/store/user/loginUserSlice.ts @@ -1,4 +1,5 @@ import { createSlice } from "@reduxjs/toolkit"; +import { getDepKey } from "../../utils/index"; type UserStoreInterface = { user: null; @@ -10,7 +11,7 @@ type UserStoreInterface = { let defaultValue: UserStoreInterface = { user: null, departments: [], - currentDepId: 0, + currentDepId: Number(getDepKey()) || 0, isLogin: false, }; diff --git a/src/utils/index.ts b/src/utils/index.ts index 2ab988f..52657e3 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -72,3 +72,26 @@ export function inStrArray(array: string[], value: string): boolean { } return false; } + +export function getDepKey(): string { + return window.localStorage.getItem("playedu-frontend-depatmentKey") || ""; +} + +export function setDepKey(token: string) { + window.localStorage.setItem("playedu-frontend-depatmentKey", token); +} + +export function clearDepKey() { + window.localStorage.removeItem("playedu-frontend-depatmentKey"); +} +export function getDepName(): string { + return window.localStorage.getItem("playedu-frontend-depatmentName") || ""; +} + +export function setDepName(token: string) { + window.localStorage.setItem("playedu-frontend-depatmentName", token); +} + +export function clearDepName() { + window.localStorage.removeItem("playedu-frontend-depatmentName"); +}