forum
This commit is contained in:
@@ -37,15 +37,7 @@ export const enrollCourse = (data) => api.post('/course-enrollments/', data);
|
||||
export const sendSms = (data) => api.post('/auth/send-sms/', data);
|
||||
export const queryMyOrders = (data) => api.post('/orders/my_orders/', data);
|
||||
export const phoneLogin = (data) => api.post('/auth/phone-login/', data);
|
||||
export const getUserInfo = () => {
|
||||
// 如果没有获取用户信息的接口,可以暂时从本地解析或依赖 update_user_info 的返回
|
||||
// 但后端有 /wechat/update/ 可以返回用户信息,或者我们可以加一个 /auth/me/
|
||||
// 目前 phone_login 返回了用户信息,前端可以保存。
|
||||
// 如果需要刷新,可以复用 update_user_info(虽然名字叫update,但传空通常返回当前信息,需确认后端逻辑)
|
||||
// 查看后端逻辑:update_user_info 是 patch 更新,如果 data 为空,update 不会执行但会返回 serializer.data
|
||||
return api.post('/wechat/update/', {});
|
||||
};
|
||||
|
||||
export const getUserInfo = () => api.get('/users/me/');
|
||||
export const updateUserInfo = (data) => api.post('/wechat/update/', data);
|
||||
export const uploadUserAvatar = (data) => {
|
||||
// 使用 axios 直接请求外部接口,避免 base URL 和拦截器干扰
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import React, { createContext, useState, useEffect, useContext } from 'react';
|
||||
|
||||
import { getUserInfo } from '../api';
|
||||
|
||||
const AuthContext = createContext(null);
|
||||
|
||||
export const AuthProvider = ({ children }) => {
|
||||
@@ -7,16 +9,46 @@ export const AuthProvider = ({ children }) => {
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const storedUser = localStorage.getItem('user');
|
||||
if (storedUser) {
|
||||
try {
|
||||
setUser(JSON.parse(storedUser));
|
||||
} catch (e) {
|
||||
console.error("Failed to parse user from storage", e);
|
||||
localStorage.removeItem('user');
|
||||
}
|
||||
}
|
||||
setLoading(false);
|
||||
const initAuth = async () => {
|
||||
const storedToken = localStorage.getItem('token');
|
||||
const storedUser = localStorage.getItem('user');
|
||||
|
||||
if (storedToken) {
|
||||
try {
|
||||
// 1. 优先尝试从本地获取
|
||||
if (storedUser) {
|
||||
try {
|
||||
const parsedUser = JSON.parse(storedUser);
|
||||
// 如果本地数据包含 ID,直接使用
|
||||
if (parsedUser.id) {
|
||||
setUser(parsedUser);
|
||||
} else {
|
||||
// 如果没有 ID,标记为需要刷新
|
||||
throw new Error("Missing ID in stored user");
|
||||
}
|
||||
} catch (e) {
|
||||
// 解析失败或数据不完整,继续从服务器获取
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 总是尝试从服务器获取最新信息(或作为兜底)
|
||||
// 这样可以确保 ID 存在,且信息是最新的
|
||||
const res = await getUserInfo();
|
||||
if (res.data) {
|
||||
setUser(res.data);
|
||||
localStorage.setItem('user', JSON.stringify(res.data));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch user info:", error);
|
||||
// 如果 token 失效,可能需要登出?
|
||||
// 暂时不强制登出,只清除无效的本地 user
|
||||
if (!user) localStorage.removeItem('user');
|
||||
}
|
||||
}
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
initAuth();
|
||||
}, []);
|
||||
|
||||
const login = (userData) => {
|
||||
|
||||
@@ -167,7 +167,14 @@ const ForumDetail = () => {
|
||||
返回列表
|
||||
</Button>
|
||||
|
||||
{user && topic.author === user.id && (
|
||||
{/* Debug Info: Remove in production */}
|
||||
{/* <div style={{ color: 'red', fontSize: 10 }}>
|
||||
User ID: {user?.id} ({typeof user?.id})<br/>
|
||||
Topic Author: {topic.author} ({typeof topic.author})<br/>
|
||||
Match: {String(topic.author) === String(user?.id) ? 'Yes' : 'No'}
|
||||
</div> */}
|
||||
|
||||
{user && String(topic.author) === String(user.id) && (
|
||||
<Button
|
||||
type="primary"
|
||||
ghost
|
||||
@@ -308,7 +315,15 @@ const ForumDetail = () => {
|
||||
showUploadList={false}
|
||||
accept="image/*,video/*"
|
||||
>
|
||||
<Button icon={<UploadOutlined />} loading={replyUploading} size="small" ghost>
|
||||
<Button
|
||||
icon={<UploadOutlined />}
|
||||
loading={replyUploading}
|
||||
style={{
|
||||
color: '#fff',
|
||||
background: 'rgba(255,255,255,0.1)',
|
||||
border: '1px solid rgba(255,255,255,0.2)'
|
||||
}}
|
||||
>
|
||||
插入图片/视频
|
||||
</Button>
|
||||
</Upload>
|
||||
|
||||
Reference in New Issue
Block a user