blob: 493eede3fdb69b739e15c622274053fe1da8e179 [file] [log] [blame]
Krishya6bf199c2025-06-06 21:14:23 +08001
Krishyadbfadaa2025-06-09 20:33:15 +08002// import React, { createContext, useContext, useState, useEffect } from 'react';
3
4// const avatarBaseUrl = process.env.REACT_APP_AVATAR_BASE_URL || '';
5
6// export const UserContext = createContext();
7
8// export const UserProvider = ({ children }) => {
9// const [user, setUser] = useState(null);
10// const [loading, setLoading] = useState(true);
11
12// const formatUser = (raw) => {
13// if (!raw) return null;
14// return {
15// userId: raw.userId || raw.id || null,
16// username: raw.username || '匿名用户',
17// avatarUrl: raw.avatarUrl ? `${avatarBaseUrl}${raw.avatarUrl}` : null,
18// ...raw,
19// };
20// };
21
22// useEffect(() => {
23// const storedUser = localStorage.getItem('user');
24// if (storedUser) {
25// try {
26// const parsed = JSON.parse(storedUser);
27// setUser(formatUser(parsed));
28// } catch (err) {
29// console.error('本地用户信息解析失败:', err);
30// localStorage.removeItem('user');
31// setUser(null);
32// }
33// } else {
34// // 可以默认不自动登录,退出就是空
35// setUser(null);
36// }
37// setLoading(false);
38// }, []);
39
40// const saveUser = (userData) => {
41// if (userData && (userData.userId || userData.id)) {
42// localStorage.setItem('user', JSON.stringify(userData));
43// setUser(formatUser(userData));
44// } else {
45// console.error('无效的用户数据:', userData);
46// }
47// };
48
49// const logout = () => {
50// localStorage.removeItem('user');
51// setUser(null);
52// };
53
54// return (
55// <UserContext.Provider value={{ user, saveUser, logout, loading }}>
56// {children}
57// </UserContext.Provider>
58// );
59// };
60
61// export const useUser = () => {
62// const context = useContext(UserContext);
63// if (!context) {
64// throw new Error('useUser 必须在 UserProvider 内使用');
65// }
66// return context;
67// };
Krishya75e43c02025-04-05 21:16:30 +080068import React, { createContext, useContext, useState, useEffect } from 'react';
69
22301009df48f962025-06-05 13:40:44 +080070const avatarBaseUrl = process.env.REACT_APP_AVATAR_BASE_URL || '';
71
Krishya8f2fec82025-06-04 21:54:46 +080072export const UserContext = createContext();
Krishya75e43c02025-04-05 21:16:30 +080073
74export const UserProvider = ({ children }) => {
75 const [user, setUser] = useState(null);
76 const [loading, setLoading] = useState(true);
77
22301009df48f962025-06-05 13:40:44 +080078 const formatUser = (raw) => {
79 if (!raw) return null;
80 return {
81 userId: raw.userId || raw.id || null,
82 username: raw.username || '匿名用户',
Krishyadbfadaa2025-06-09 20:33:15 +080083 avatarUrl: raw.avatarUrl
84 ? raw.avatarUrl.startsWith('http')
85 ? raw.avatarUrl
86 : `${avatarBaseUrl}${raw.avatarUrl}`
87 : null,
Krishya6bf199c2025-06-06 21:14:23 +080088 ...raw,
22301009df48f962025-06-05 13:40:44 +080089 };
90 };
91
Krishya75e43c02025-04-05 21:16:30 +080092 useEffect(() => {
93 const storedUser = localStorage.getItem('user');
94 if (storedUser) {
22301009df48f962025-06-05 13:40:44 +080095 try {
96 const parsed = JSON.parse(storedUser);
97 setUser(formatUser(parsed));
98 } catch (err) {
99 console.error('本地用户信息解析失败:', err);
100 localStorage.removeItem('user');
101 setUser(null);
102 }
2230100937ebec12025-06-03 21:17:04 +0800103 } else {
Krishya6bf199c2025-06-06 21:14:23 +0800104 setUser(null);
Krishya75e43c02025-04-05 21:16:30 +0800105 }
106 setLoading(false);
107 }, []);
108
Krishya75e43c02025-04-05 21:16:30 +0800109 const saveUser = (userData) => {
22301009df48f962025-06-05 13:40:44 +0800110 if (userData && (userData.userId || userData.id)) {
Krishyadbfadaa2025-06-09 20:33:15 +0800111 const formatted = formatUser(userData);
112 localStorage.setItem('user', JSON.stringify(formatted));
113 setUser(formatted);
Krishya8f2fec82025-06-04 21:54:46 +0800114 } else {
115 console.error('无效的用户数据:', userData);
116 }
Krishya75e43c02025-04-05 21:16:30 +0800117 };
118
Krishya75e43c02025-04-05 21:16:30 +0800119 const logout = () => {
120 localStorage.removeItem('user');
121 setUser(null);
122 };
123
124 return (
Krishya8f2fec82025-06-04 21:54:46 +0800125 <UserContext.Provider value={{ user, saveUser, logout, loading }}>
Krishya75e43c02025-04-05 21:16:30 +0800126 {children}
127 </UserContext.Provider>
128 );
129};
130
Krishya8f2fec82025-06-04 21:54:46 +0800131export const useUser = () => {
132 const context = useContext(UserContext);
133 if (!context) {
Krishya6bf199c2025-06-06 21:14:23 +0800134 throw new Error('useUser 必须在 UserProvider 内使用');
Krishya8f2fec82025-06-04 21:54:46 +0800135 }
136 return context;
22301009df48f962025-06-05 13:40:44 +0800137};