Skip to content

Commit

Permalink
fix(store): 🐛 user
Browse files Browse the repository at this point in the history
- 修复登录按钮需要按两次才能进入页面

- 修复未登录不正常跳转登录页面
  • Loading branch information
jsxiaosi committed Sep 10, 2023
1 parent 69d9bdb commit 7198854
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function App() {
}),
shallowEqual,
);
const { userInfo } = useAppSelector((state) => state.userInfo);
const { userInfo } = useAppSelector((state) => state.user);
const asyncRouter = useAppSelector((state) => state.route.asyncRouter);

const getLocale = useMemo(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/layout/Authority/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ interface AuthorityType {
}

const Authority = ({ children }: AuthorityType) => {
const userInfo = useAppSelector((state) => state.userInfo);
const user = useAppSelector((state) => state.user);

if (!userInfo) return <Navigate to="/login" />;
if (!user?.power) return <Navigate to="/login" />;

return <>{children}</>;
};
Expand Down
6 changes: 6 additions & 0 deletions src/layout/components/AppAccount/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import { useNavigate } from 'react-router-dom';
import { getAccountStyle } from './style';
import avatar from '@/assets/avatar.png';
import { removeStorage } from '@/utils/storage';
import { setSignOut } from '@/store/modules/user';
import { useAppDispatch } from '@/store/hooks';

const AppAccount = () => {
const { AccountDiv } = getAccountStyle();

const dispatch = useAppDispatch();

const navigate = useNavigate();

const items: MenuProps['items'] = [
Expand All @@ -19,6 +23,8 @@ const AppAccount = () => {

const memuChange: MenuProps['onClick'] = (_e) => {
removeStorage('userInfo');
dispatch(setSignOut());

navigate('/login');
};

Expand Down
6 changes: 3 additions & 3 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import storage from 'redux-persist/lib/storage';
// import thunk from 'redux-thunk';
import appReducer from './modules/app';
import routeReducer from './modules/route';
import userInfoReducer from './modules/userInfo';
import userReducer from './modules/user';

const reducers = combineReducers({
app: appReducer,
route: routeReducer,
userInfo: userInfoReducer,
user: userReducer,
});

const persistConfig = {
key: 'react-xs',
storage,
// 白名单
whitelist: ['app', 'route', 'userInfo'],
whitelist: ['app', 'route', 'user'],
// 黑名单
blacklist: [],
};
Expand Down
36 changes: 36 additions & 0 deletions src/store/modules/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
import type { UseInfoType } from '@/server/useInfo';

interface UserSlice {
userInfo?: UseInfoType;
power?: UseInfoType['power'];
}

const initialState: UserSlice = {};

export const UserSlice = createSlice({
name: 'userInfo',
initialState,
reducers: {
setUserInfo: (state, action: PayloadAction<UseInfoType>) => {
console.log(action.payload);
state.userInfo = action.payload;
state.power = action.payload.power;
},
setPower: (state, action: PayloadAction<UseInfoType['power']>) => {
state.power = action.payload;
if (state.userInfo) {
state.userInfo.power = action.payload;
}
},
setSignOut: (state) => {
delete state.userInfo;
delete state.power;
},
},
});

export const { setUserInfo, setPower, setSignOut } = UserSlice.actions;

export default UserSlice.reducer;
14 changes: 10 additions & 4 deletions src/views/Login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button, theme } from 'antd';
import { memo, useState } from 'react';
import { memo, useEffect, useState } from 'react';
import { LockOutlined, UserOutlined } from '@ant-design/icons';
import { useNavigate } from 'react-router-dom';
import SvgIcon from '@/components/SvgIcon';
Expand All @@ -9,11 +9,12 @@ import AppLocale from '@/components/AppLocale';
import { addClass, removeClass } from '@/utils/operate';
import { getUserInfo } from '@/server/useInfo';
import { initAsyncRoute } from '@/router/utils';
import { useAppDispatch } from '@/store/hooks';
import { setUserInfo } from '@/store/modules/userInfo';
import { useAppDispatch, useAppSelector } from '@/store/hooks';
import { setUserInfo } from '@/store/modules/user';

const Login = memo(() => {
const thme = theme.useToken();
const userStore = useAppSelector((state) => state.user);

const [user, setUser] = useState<string>('');
const [pwd, setPwd] = useState<string>('');
Expand All @@ -27,10 +28,15 @@ const Login = memo(() => {
if (res.code === 1) {
await initAsyncRoute(res.data.power);
dispatch(setUserInfo(res.data));
navigate('/home');
}
};

useEffect(() => {
if (userStore.power) {
navigate('/home');
}
}, [userStore]);

function onUserFocus() {
addClass(document.querySelector('.user'), 'focus');
}
Expand Down
4 changes: 2 additions & 2 deletions src/views/Power/Permissions/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Button } from 'antd';
import { initAsyncRoute } from '@/router/utils';
import { useAppDispatch, useAppSelector } from '@/store/hooks';
import { setPower } from '@/store/modules/userInfo';
import { setPower } from '@/store/modules/user';

const Permissions = () => {
const dispatch = useAppDispatch();

const power = useAppSelector((state) => state.userInfo.power);
const power = useAppSelector((state) => state.user.power);

const setCount = async () => {
const newPower = power === 'admin' ? 'test' : 'admin';
Expand Down

0 comments on commit 7198854

Please sign in to comment.