From 4a30e792692a261c325d49282ef68130ed4150d4 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Mon, 10 Aug 2026 17:41:46 +0800 Subject: [PATCH] feat: edit post images (upload/remove) + fix captcha input in dark mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - PostDetail edit modal: show existing images with delete button, upload new images (multi-file, 5MB limit), save replaces image list - API PUT /posts/:id accepts images array; omitting keeps original - lib/api updatePost passes images - NewPost/ReplyList captcha answer input: use CSS vars (was hardcoded #e2e8f0 border with white bg — glaring in dark mode) --- app/api/posts/[id]/route.js | 9 +++--- components/NewPost.js | 2 +- components/PostDetail.js | 55 ++++++++++++++++++++++++++++++++++--- components/ReplyList.js | 2 +- lib/api.js | 4 +-- 5 files changed, 60 insertions(+), 12 deletions(-) diff --git a/app/api/posts/[id]/route.js b/app/api/posts/[id]/route.js index 3edda4a..c5eec11 100644 --- a/app/api/posts/[id]/route.js +++ b/app/api/posts/[id]/route.js @@ -33,19 +33,20 @@ export async function PUT(req, { params }) { if (!user) { return Response.json({ error: '请先登录' }, { status: 401 }); } - const { title, content } = await req.json(); + const { title, content, images } = await req.json(); if (!content || content.trim().length === 0) { return Response.json({ error: '请填写内容' }, { status: 400 }); } try { - const post = await pool.query('SELECT user_id FROM posts WHERE id = $1', [params.id]); + const post = await pool.query('SELECT user_id, images FROM posts WHERE id = $1', [params.id]); if (post.rows.length === 0) return invalidId(); if (post.rows[0].user_id !== user.id) { return Response.json({ error: '无权限编辑此帖子' }, { status: 403 }); } + // images: 可选,传入完整图片 URL 数组(编辑时替换全部图片);不传则保留原图 const r = await pool.query( - `UPDATE posts SET title = $1, content = $2, edited_at = NOW() WHERE id = $3 RETURNING *`, - [title || '无标题', content, params.id] + `UPDATE posts SET title = $1, content = $2, images = $3, edited_at = NOW() WHERE id = $4 RETURNING *`, + [title || '无标题', content, Array.isArray(images) ? images : post.rows[0].images || [], params.id] ); return Response.json(r.rows[0]); } catch { diff --git a/components/NewPost.js b/components/NewPost.js index 902d48e..f6e1099 100644 --- a/components/NewPost.js +++ b/components/NewPost.js @@ -132,7 +132,7 @@ export default function NewPost() { setCaptchaInput(e.target.value)} /> diff --git a/components/PostDetail.js b/components/PostDetail.js index 275160d..0a16789 100644 --- a/components/PostDetail.js +++ b/components/PostDetail.js @@ -1,8 +1,8 @@ 'use client'; // 帖子详情页 -import { useEffect, useState, useCallback } from 'react'; -import { API } from '@/lib/api'; +import { useEffect, useState, useCallback, useRef } from 'react'; +import { API, uploadImage } from '@/lib/api'; import { useApp } from './AppProvider'; import ReplyList from './ReplyList'; import { Icon } from './Icons'; @@ -21,6 +21,9 @@ export default function PostDetail({ postId }) { const [editing, setEditing] = useState(false); const [editTitle, setEditTitle] = useState(''); const [editContent, setEditContent] = useState(''); + const [editImages, setEditImages] = useState([]); + const [editUploading, setEditUploading] = useState(false); + const editImageInputRef = useRef(null); const load = useCallback(async () => { setError(null); @@ -73,13 +76,14 @@ export default function PostDetail({ postId }) { const handleEdit = () => { setEditTitle(post.title || ''); setEditContent(post.content || ''); + setEditImages(post.images || []); setEditing(true); }; const handleSaveEdit = async () => { if (!editContent.trim()) { alert('请填写内容'); return; } try { - await API.updatePost(postId, editTitle.trim() || '无标题', editContent.trim()); + await API.updatePost(postId, editTitle.trim() || '无标题', editContent.trim(), editImages); setEditing(false); load(); } catch (err) { @@ -87,6 +91,27 @@ export default function PostDetail({ postId }) { } }; + // 编辑时上传新图片 + const handleEditUpload = async (file) => { + if (!file) return; + if (!file.type.startsWith('image/')) { alert('请选择图片文件'); return; } + if (file.size > 5 * 1024 * 1024) { alert('图片不能超过 5MB'); return; } + setEditUploading(true); + try { + const url = await uploadImage(file); + setEditImages((prev) => [...prev, url]); + } catch (err) { + alert('上传失败:' + err.message); + } finally { + setEditUploading(false); + } + }; + + // 编辑时删除图片 + const handleEditRemoveImage = (url) => { + setEditImages((prev) => prev.filter((u) => u !== url)); + }; + return (