Skip to content

Commit 0ecbf9a

Browse files
committed
feat: 前台登录时,重新查询用户身份信息
1 parent 4722593 commit 0ecbf9a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/api/modules/login.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ export const loginApi = (params: Login.ReqLoginForm) => {
1515
// return http.post<Login.ResLogin>(PORT1 + `/login`, params, { headers: { noLoading: true } }); // 控制当前请求不显示 loading
1616
};
1717

18+
/**
19+
* 查询当前登录的用户信息
20+
*/
21+
export const loginUserInfo = () => {
22+
return http.get(PORT1 + `/user/info`, {});
23+
};
24+
1825
export const logoutApi = () => {
1926
return http.get(PORT1 + `/logout`, {});
2027
};

src/layouts/components/Header/index.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { connect } from "react-redux";
22
import { Layout } from "antd";
33

4+
import { loginUserInfo } from "@/api/modules/login";
5+
import { setToken, setUserInfo } from "@/redux/modules/global/action";
46
import AssemblySize from "./components/AssemblySize";
57
import AvatarIcon from "./components/AvatarIcon";
68
import BreadcrumbNav from "./components/BreadcrumbNav";
@@ -11,8 +13,23 @@ import Theme from "./components/Theme";
1113
import "./index.less";
1214

1315
const LayoutHeader = (props: any) => {
14-
const { userInfo } = props || {};
16+
let { userInfo } = props || {};
17+
if (!userInfo) {
18+
let fetchUsrInfo = async () => {
19+
const { status, result } = await loginUserInfo();
20+
if (status && status.code == 0 && result && result?.userId > 0) {
21+
// fixme 拿登录的用户名、用户头像来替换默认的用户名头像
22+
setToken(result?.userId);
23+
24+
setUserInfo(result);
25+
userInfo = result;
26+
}
27+
};
28+
// 未拿到用户信息时,主动去拿一下
29+
fetchUsrInfo();
30+
}
1531
const { userName } = userInfo || {};
32+
console.log("userInfo:", userInfo, userName);
1633

1734
const { Header } = Layout;
1835

0 commit comments

Comments
 (0)