feat: edit post images (upload/remove) + fix captcha input in dark mode
- 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)
This commit is contained in:
1 parent
ebc5201569
commit
4a30e79269
5 files changed
+60
-12
No files matched your search
@@ -33,19 +33,20 @@ export async function PUT(req, { params }) {
|
|||||||
if (!user) {
|
if (!user) {
|
||||||
return Response.json({ error: '请先登录' }, { status: 401 });
|
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) {
|
if (!content || content.trim().length === 0) {
|
||||||
return Response.json({ error: '请填写内容' }, { status: 400 });
|
return Response.json({ error: '请填写内容' }, { status: 400 });
|
||||||
}
|
}
|
||||||
try {
|
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.length === 0) return invalidId();
|
||||||
if (post.rows[0].user_id !== user.id) {
|
if (post.rows[0].user_id !== user.id) {
|
||||||
return Response.json({ error: '无权限编辑此帖子' }, { status: 403 });
|
return Response.json({ error: '无权限编辑此帖子' }, { status: 403 });
|
||||||
}
|
}
|
||||||
|
// images: 可选,传入完整图片 URL 数组(编辑时替换全部图片);不传则保留原图
|
||||||
const r = await pool.query(
|
const r = await pool.query(
|
||||||
`UPDATE posts SET title = $1, content = $2, edited_at = NOW() WHERE id = $3 RETURNING *`,
|
`UPDATE posts SET title = $1, content = $2, images = $3, edited_at = NOW() WHERE id = $4 RETURNING *`,
|
||||||
[title || '无标题', content, params.id]
|
[title || '无标题', content, Array.isArray(images) ? images : post.rows[0].images || [], params.id]
|
||||||
);
|
);
|
||||||
return Response.json(r.rows[0]);
|
return Response.json(r.rows[0]);
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ export default function NewPost() {
|
|||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="答案"
|
placeholder="答案"
|
||||||
style={{ width: 80, padding: '8px 12px', border: '1px solid #e2e8f0', borderRadius: 4 }}
|
style={{ width: 80, padding: '8px 12px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--bg)', color: 'var(--text)', outline: 'none' }}
|
||||||
value={captchaInput}
|
value={captchaInput}
|
||||||
onChange={(e) => setCaptchaInput(e.target.value)}
|
onChange={(e) => setCaptchaInput(e.target.value)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
// 帖子详情页
|
// 帖子详情页
|
||||||
import { useEffect, useState, useCallback } from 'react';
|
import { useEffect, useState, useCallback, useRef } from 'react';
|
||||||
import { API } from '@/lib/api';
|
import { API, uploadImage } from '@/lib/api';
|
||||||
import { useApp } from './AppProvider';
|
import { useApp } from './AppProvider';
|
||||||
import ReplyList from './ReplyList';
|
import ReplyList from './ReplyList';
|
||||||
import { Icon } from './Icons';
|
import { Icon } from './Icons';
|
||||||
@@ -21,6 +21,9 @@ export default function PostDetail({ postId }) {
|
|||||||
const [editing, setEditing] = useState(false);
|
const [editing, setEditing] = useState(false);
|
||||||
const [editTitle, setEditTitle] = useState('');
|
const [editTitle, setEditTitle] = useState('');
|
||||||
const [editContent, setEditContent] = useState('');
|
const [editContent, setEditContent] = useState('');
|
||||||
|
const [editImages, setEditImages] = useState([]);
|
||||||
|
const [editUploading, setEditUploading] = useState(false);
|
||||||
|
const editImageInputRef = useRef(null);
|
||||||
|
|
||||||
const load = useCallback(async () => {
|
const load = useCallback(async () => {
|
||||||
setError(null);
|
setError(null);
|
||||||
@@ -73,13 +76,14 @@ export default function PostDetail({ postId }) {
|
|||||||
const handleEdit = () => {
|
const handleEdit = () => {
|
||||||
setEditTitle(post.title || '');
|
setEditTitle(post.title || '');
|
||||||
setEditContent(post.content || '');
|
setEditContent(post.content || '');
|
||||||
|
setEditImages(post.images || []);
|
||||||
setEditing(true);
|
setEditing(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSaveEdit = async () => {
|
const handleSaveEdit = async () => {
|
||||||
if (!editContent.trim()) { alert('请填写内容'); return; }
|
if (!editContent.trim()) { alert('请填写内容'); return; }
|
||||||
try {
|
try {
|
||||||
await API.updatePost(postId, editTitle.trim() || '无标题', editContent.trim());
|
await API.updatePost(postId, editTitle.trim() || '无标题', editContent.trim(), editImages);
|
||||||
setEditing(false);
|
setEditing(false);
|
||||||
load();
|
load();
|
||||||
} catch (err) {
|
} 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 (
|
return (
|
||||||
<div id="pagePost" className="page-slide active">
|
<div id="pagePost" className="page-slide active">
|
||||||
<div className="page-header" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
|
<div className="page-header" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
|
||||||
@@ -169,7 +194,29 @@ export default function PostDetail({ postId }) {
|
|||||||
onChange={(e) => setEditContent(e.target.value)}
|
onChange={(e) => setEditContent(e.target.value)}
|
||||||
style={{ width: '100%', padding: 12, border: '1px solid var(--border)', borderRadius: 6, fontSize: 15, fontFamily: 'inherit', resize: 'vertical', background: 'var(--bg)', color: 'var(--text)' }}
|
style={{ width: '100%', padding: 12, border: '1px solid var(--border)', borderRadius: 6, fontSize: 15, fontFamily: 'inherit', resize: 'vertical', background: 'var(--bg)', color: 'var(--text)' }}
|
||||||
/>
|
/>
|
||||||
<button className="btn-primary" style={{ padding: '10px 32px', fontSize: 15, marginTop: 8 }} onClick={handleSaveEdit}>保存</button>
|
{/* 图片管理:查看/删除已有图片 + 上传新图片 */}
|
||||||
|
{editImages.length > 0 && (
|
||||||
|
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginTop: 10 }}>
|
||||||
|
{editImages.map((img, i) => (
|
||||||
|
<div key={img + i} style={{ position: 'relative' }}>
|
||||||
|
<img src={img} alt="" style={{ width: 72, height: 72, objectFit: 'cover', borderRadius: 6, border: '1px solid var(--border)' }} />
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
title="删除图片"
|
||||||
|
onClick={() => handleEditRemoveImage(img)}
|
||||||
|
style={{ position: 'absolute', top: -6, right: -6, width: 20, height: 20, borderRadius: '50%', background: '#ef4444', color: '#fff', border: 'none', cursor: 'pointer', fontSize: 12, lineHeight: 1, display: 'flex', alignItems: 'center', justifyContent: 'center' }}
|
||||||
|
>×</button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div style={{ display: 'flex', gap: 8, marginTop: 12 }}>
|
||||||
|
<button type="button" className="btn-secondary" style={{ padding: '8px 16px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={() => editImageInputRef.current?.click()}>
|
||||||
|
{editUploading ? '上传中...' : '📷 上传图片'}
|
||||||
|
</button>
|
||||||
|
<input type="file" ref={editImageInputRef} accept="image/*" multiple style={{ display: 'none' }} onChange={(e) => { const files = e.target.files; if (files && files.length > 0) { [...files].forEach((f) => handleEditUpload(f)); e.target.value = ''; } }} />
|
||||||
|
</div>
|
||||||
|
<button className="btn-primary" style={{ padding: '10px 32px', fontSize: 15, marginTop: 12, width: '100%' }} onClick={handleSaveEdit}>保存</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ export default function ReplyList({ postId, onRefresh }) {
|
|||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="答案"
|
placeholder="答案"
|
||||||
style={{ width: 80, padding: '8px 12px', border: '1px solid #e2e8f0', borderRadius: 4 }}
|
style={{ width: 80, padding: '8px 12px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--bg)', color: 'var(--text)', outline: 'none' }}
|
||||||
value={captchaInput}
|
value={captchaInput}
|
||||||
onChange={(e) => setCaptchaInput(e.target.value)}
|
onChange={(e) => setCaptchaInput(e.target.value)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
+2
-2
@@ -57,8 +57,8 @@ export const API = {
|
|||||||
createPost(title, content, images) {
|
createPost(title, content, images) {
|
||||||
return apiFetch('/posts', { method: 'POST', body: JSON.stringify({ title, content, images: images || [] }) });
|
return apiFetch('/posts', { method: 'POST', body: JSON.stringify({ title, content, images: images || [] }) });
|
||||||
},
|
},
|
||||||
updatePost(postId, title, content) {
|
updatePost(postId, title, content, images) {
|
||||||
return apiFetch('/posts/' + postId, { method: 'PUT', body: JSON.stringify({ title, content }) });
|
return apiFetch('/posts/' + postId, { method: 'PUT', body: JSON.stringify({ title, content, images }) });
|
||||||
},
|
},
|
||||||
togglePinPost(postId) {
|
togglePinPost(postId) {
|
||||||
return apiFetch('/posts/' + postId + '/pin', { method: 'PUT' });
|
return apiFetch('/posts/' + postId + '/pin', { method: 'PUT' });
|
||||||
|
|||||||
Reference in new issue
Block a user