diff --git a/app.py b/app.py index 7e259031..39c37f34 100755 --- a/app.py +++ b/app.py @@ -4141,6 +4141,52 @@ def get_my_event_comments(): return jsonify({'success': True, 'data': [c.to_dict() for c in comments]}) +@app.route('/api/account/events/posts', methods=['GET']) +def get_my_event_posts(): + """获取我在事件上的帖子(Post)- 用于个人中心显示""" + if 'user_id' not in session: + return jsonify({'success': False, 'error': '未登录'}), 401 + + try: + # 查询当前用户的所有 Post(按创建时间倒序) + posts = Post.query.filter_by( + user_id=session['user_id'], + status='active' + ).order_by(Post.created_at.desc()).limit(100).all() + + posts_data = [] + for post in posts: + # 获取关联的事件信息 + event = Event.query.get(post.event_id) + event_title = event.title if event else '未知事件' + + # 获取用户信息 + user = User.query.get(post.user_id) + author = user.username if user else '匿名用户' + + # ⚡ 返回格式兼容旧 EventComment.to_dict() + posts_data.append({ + 'id': post.id, + 'event_id': post.event_id, + 'event_title': event_title, # ⚡ 新增字段(旧 API 没有) + 'user_id': post.user_id, + 'author': author, # ⚡ 兼容旧格式(字符串类型) + 'content': post.content, + 'title': post.title, # Post 独有字段(可选) + 'content_type': post.content_type, # Post 独有字段 + 'likes': post.likes_count, # ⚡ 兼容旧字段名 + 'created_at': post.created_at.isoformat(), + 'updated_at': post.updated_at.isoformat(), + 'status': post.status, + }) + + return jsonify({'success': True, 'data': posts_data}) + + except Exception as e: + print(f"获取用户帖子失败: {e}") + return jsonify({'success': False, 'error': '获取帖子失败'}), 500 + + @app.route('/api/account/future-events/following', methods=['GET']) def get_my_following_future_events(): """获取当前用户关注的未来事件""" diff --git a/src/views/Dashboard/Center.js b/src/views/Dashboard/Center.js index e425982d..cc81bc3b 100644 --- a/src/views/Dashboard/Center.js +++ b/src/views/Dashboard/Center.js @@ -102,7 +102,7 @@ export default function CenterDashboard() { const [w, e, c] = await Promise.all([ fetch(base + `/api/account/watchlist?_=${ts}`, { credentials: 'include', cache: 'no-store', headers: { 'Cache-Control': 'no-cache' } }), fetch(base + `/api/account/events/following?_=${ts}`, { credentials: 'include', cache: 'no-store', headers: { 'Cache-Control': 'no-cache' } }), - fetch(base + `/api/account/events/comments?_=${ts}`, { credentials: 'include', cache: 'no-store', headers: { 'Cache-Control': 'no-cache' } }), + fetch(base + `/api/account/events/posts?_=${ts}`, { credentials: 'include', cache: 'no-store', headers: { 'Cache-Control': 'no-cache' } }), ]); const jw = await w.json(); const je = await e.json();