diff --git a/app/api/users/[id]/route.js b/app/api/users/[id]/route.js index 826c157..848675d 100644 --- a/app/api/users/[id]/route.js +++ b/app/api/users/[id]/route.js @@ -1,6 +1,6 @@ // PUT /api/users/[id] (更新资料), PUT /api/users/[id]/role (改角色) import pool from '@/lib/db'; -import { getUser, requireAdmin } from '@/lib/auth'; +import { getUser, requireAdmin, isFirstUser } from '@/lib/auth'; export async function PUT(req, { params }) { const { id } = params; @@ -22,6 +22,10 @@ export async function PUT(req, { params }) { if (id === user.id) { return Response.json({ error: '不能修改自己的角色' }, { status: 400 }); } + // 超级管理员(第一个用户)不能被降级 + if (role !== 'admin' && await isFirstUser(id)) { + return Response.json({ error: '不能降级超级管理员' }, { status: 403 }); + } try { await pool.query('UPDATE users SET role = $1 WHERE id = $2', [role, id]); return Response.json({ success: true }); diff --git a/app/globals.css b/app/globals.css index 5b3109f..b2779e7 100644 --- a/app/globals.css +++ b/app/globals.css @@ -1179,6 +1179,17 @@ svg { width: 20px; height: 20px; } + /* 手机版帖子操作按钮紧凑排列,避免删除按钮越界 */ + .post-actions { + gap: 8px; + flex-wrap: wrap; + } + .post-actions button { + padding: 4px 4px; + } + #feed .post-content { + max-height: 4.25em; /* 手机屏约 2.5 行 */ + } } /* ============================================================ @@ -1907,15 +1918,24 @@ a.btn-secondary { ============================================================ */ #feed .post-content { - display: -webkit-box; - -webkit-line-clamp: 3; - -webkit-box-orient: vertical; + position: relative; + max-height: 5.1em; /* 约 3 行(line-height 1.7) */ overflow: hidden; - text-overflow: ellipsis; - max-height: none; line-height: 1.7; } +/* 长帖:底部渐变遮罩(提示还有更多内容) */ +#feed .post-content.has-fade::after { + content: ''; + position: absolute; + bottom: 0; + left: 0; + right: 0; + height: 2.2em; + background: linear-gradient(180deg, transparent 0%, var(--glass-bg) 100%); + pointer-events: none; +} + /* ===== 全屏加载页 ===== */ .loading-screen { position: fixed; @@ -2064,3 +2084,48 @@ a.btn-secondary { #adminContent td:nth-child(2) { white-space: normal !important; } + +/* ===== Toast 通知(右上角,替代 alert) ===== */ +.toast-container { + position: fixed; + top: 20px; + right: 20px; + z-index: 100000; + display: flex; + flex-direction: column; + gap: 10px; + pointer-events: none; +} + +.toast { + pointer-events: auto; + min-width: 200px; + max-width: 340px; + padding: 12px 18px; + border-radius: 10px; + font-size: 14px; + font-weight: 500; + color: #fff; + box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2); + backdrop-filter: blur(12px); + -webkit-backdrop-filter: blur(12px); + cursor: pointer; + animation: toastIn 0.25s cubic-bezier(0.22, 1, 0.36, 1); +} + +.toast-success { + background: rgba(34, 197, 94, 0.92); +} + +.toast-error { + background: rgba(239, 68, 68, 0.92); +} + +.toast-info { + background: rgba(91, 155, 213, 0.92); +} + +@keyframes toastIn { + from { opacity: 0; transform: translateX(24px); } + to { opacity: 1; transform: translateX(0); } +} diff --git a/app/layout.js b/app/layout.js index ce8536f..43555e6 100644 --- a/app/layout.js +++ b/app/layout.js @@ -1,6 +1,7 @@ // 全局布局 import './globals.css'; import CustomCssLoader from '@/components/CustomCssLoader'; +import { ToastProvider } from '@/components/Toast'; export const metadata = { // 初始标题由客户端 AppProvider 接管(加载时轮换 Loading.,完成后显示论坛名) @@ -28,8 +29,10 @@ export default function RootLayout({ children }) { /> - - {children} + + + {children} + ); diff --git a/components/CaptchaImage.js b/components/CaptchaImage.js new file mode 100644 index 0000000..5f2e10f --- /dev/null +++ b/components/CaptchaImage.js @@ -0,0 +1,68 @@ +'use client'; + +// 图片验证码:用 canvas 绘制算式(带噪点/干扰线,挡一部分人机) +// 点击图片可刷新验证码 +import { useEffect, useRef } from 'react'; + +export default function CaptchaImage({ captcha, onRefresh }) { + const canvasRef = useRef(null); + + useEffect(() => { + const canvas = canvasRef.current; + if (!canvas || !captcha) return; + const ctx = canvas.getContext('2d'); + const W = canvas.width; + const H = canvas.height; + + // 背景(半透明玻璃感) + ctx.clearRect(0, 0, W, H); + ctx.fillStyle = 'rgba(91, 155, 213, 0.10)'; + ctx.fillRect(0, 0, W, H); + + // 干扰线 + for (let i = 0; i < 4; i++) { + ctx.strokeStyle = `rgba(${80 + Math.random() * 120}, ${80 + Math.random() * 120}, 200, ${0.2 + Math.random() * 0.3})`; + ctx.lineWidth = 1 + Math.random(); + ctx.beginPath(); + ctx.moveTo(Math.random() * W, Math.random() * H); + ctx.lineTo(Math.random() * W, Math.random() * H); + ctx.stroke(); + } + + // 噪点 + for (let i = 0; i < 30; i++) { + ctx.fillStyle = `rgba(${Math.random() * 200}, ${Math.random() * 200}, 220, ${0.3 + Math.random() * 0.5})`; + ctx.beginPath(); + ctx.arc(Math.random() * W, Math.random() * H, 1 + Math.random() * 1.5, 0, Math.PI * 2); + ctx.fill(); + } + + // 算式文字(每个字符轻微旋转/位移,增加识别难度) + const text = captcha.question.replace(' = ?', ''); + const chars = text.split(''); + const fontSize = Math.floor(H * 0.52); + const step = W / (chars.length + 1); + chars.forEach((ch, i) => { + ctx.save(); + ctx.translate(step * (i + 1) + (Math.random() - 0.5) * 6, H / 2 + (Math.random() - 0.5) * 6); + ctx.rotate((Math.random() - 0.5) * 0.25); + ctx.font = `bold ${fontSize}px -apple-system, "SF Pro Display", "Segoe UI", Roboto, sans-serif`; + ctx.fillStyle = `rgba(${40 + Math.random() * 60}, ${50 + Math.random() * 50}, 90, 0.9)`; + ctx.textAlign = 'center'; + ctx.textBaseline = 'middle'; + ctx.fillText(ch, 0, 0); + ctx.restore(); + }); + }, [captcha]); + + return ( + + ); +} diff --git a/components/Feed.js b/components/Feed.js index 70bfca1..0d6c22a 100644 --- a/components/Feed.js +++ b/components/Feed.js @@ -1,7 +1,7 @@ 'use client'; // 信息流:帖子列表 + 排序 tab + 发布新帖按钮 -import { useEffect, useState, useCallback } from 'react'; +import { useEffect, useState, useCallback, useRef } from 'react'; import { API } from '@/lib/api'; import { useApp } from './AppProvider'; import { Icon } from './Icons'; @@ -21,6 +21,7 @@ export default function Feed({ onOpenModal, onReport }) { const [page, setPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [viewerSrc, setViewerSrc] = useState(null); + const postListRef = useRef(null); const PAGE_SIZE = 20; const load = useCallback(async (targetPage) => { @@ -39,6 +40,19 @@ export default function Feed({ onOpenModal, onReport }) { useEffect(() => { load(page); }, [load]); + // 长帖内容:超过 3 行时加 .has-fade(底部渐变遮罩提示还有更多内容) + useEffect(() => { + if (!posts || posts.length === 0 || !postListRef.current) return; + const els = postListRef.current.querySelectorAll('.post-content'); + els.forEach((el) => { + if (el.scrollHeight > el.clientHeight + 4) { + el.classList.add('has-fade'); + } else { + el.classList.remove('has-fade'); + } + }); + }, [posts]); + // 排序切换时回第一页 useEffect(() => { setPage(1); }, [sort]); @@ -69,7 +83,7 @@ export default function Feed({ onOpenModal, onReport }) { 发布新帖 -
+
{posts === null ? (
加载中...
) : error ? ( @@ -119,7 +133,7 @@ export default function Feed({ onOpenModal, onReport }) { }}> 举报 - {currentUser && currentUser.id === p.user_id && ( + {currentUser && (currentUser.id === p.user_id || currentUser.role === 'admin') && (
diff --git a/components/NewPost.js b/components/NewPost.js index eec192a..15262e4 100644 --- a/components/NewPost.js +++ b/components/NewPost.js @@ -5,9 +5,12 @@ import { useState, useEffect, useRef } from 'react'; import { API, generateCaptcha, uploadImage } from '@/lib/api'; import { useApp } from './AppProvider'; import { Icon } from './Icons'; +import { useToast } from './Toast'; +import CaptchaImage from './CaptchaImage'; export default function NewPost() { const { currentUser, navigate, refresh } = useApp(); + const { toast } = useToast(); const [title, setTitle] = useState(''); const [content, setContent] = useState(''); const [images, setImages] = useState([]); // 上传后的 URL 列表 @@ -57,13 +60,13 @@ export default function NewPost() { const handleSubmit = async () => { if (!currentUser || !currentUser.id) { - alert('请先登录'); + toast('请先登录', 'error'); navigate('feed'); return; } - if (!content.trim()) { alert('请填写内容'); return; } + if (!content.trim()) { toast('请填写内容', 'error'); return; } if (!captcha || parseInt(captchaInput) !== captcha.answer) { - alert('验证码错误,请重新计算'); + toast('验证码错误,请重新计算', 'error'); setCaptcha(generateCaptcha()); setCaptchaInput(''); return; @@ -71,11 +74,11 @@ export default function NewPost() { try { await API.createPost(title.trim() || '无标题', content.trim(), images); API.logEvent('create_post').catch(() => {}); - alert('发布成功!'); + toast('发布成功', 'success'); navigate('feed'); refresh(); } catch (err) { - alert('发布失败:' + err.message); + toast('发布失败:' + err.message, 'error'); } }; @@ -125,10 +128,12 @@ export default function NewPost() { style={{ display: 'none' }} onChange={(e) => { if (e.target.files) handleFiles(Array.from(e.target.files)); }} /> -
- setCaptcha(generateCaptcha())} style={{ cursor: 'pointer' }}> - {captcha ? captcha.question : ''} - +
+ {captcha ? ( + { setCaptcha(generateCaptcha()); setCaptchaInput(''); }} /> + ) : ( + 验证码加载中... + )} { diff --git a/components/ReplyList.js b/components/ReplyList.js index 4b1756b..b31b7cd 100644 --- a/components/ReplyList.js +++ b/components/ReplyList.js @@ -6,6 +6,7 @@ import { API, generateCaptcha } from '@/lib/api'; import { useApp } from './AppProvider'; import { Icon } from './Icons'; import { renderMarkdown } from '@/lib/markdown'; +import CaptchaImage from './CaptchaImage'; function avatar(username) { return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') + @@ -84,7 +85,7 @@ export default function ReplyList({ postId, onRefresh }) { {r.username || '匿名用户'} {rTime} - {currentUser && currentUser.id === r.user_id && ( + {currentUser && (currentUser.id === r.user_id || currentUser.role === 'admin') && ( @@ -107,10 +108,12 @@ export default function ReplyList({ postId, onRefresh }) { onChange={(e) => setContent(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) handleSubmit(); }} /> -
- setCaptcha(generateCaptcha())} style={{ cursor: 'pointer' }}> - {captcha ? captcha.question : ''} - +
+ {captcha ? ( + { setCaptcha(generateCaptcha()); setCaptchaInput(''); }} /> + ) : ( + 验证码加载中... + )} { + const id = ++idCounter; + setToasts((prev) => [...prev, { id, message, type }]); + if (timersRef.current[id]) clearTimeout(timersRef.current[id]); + timersRef.current[id] = setTimeout(() => { + setToasts((prev) => prev.filter((t) => t.id !== id)); + delete timersRef.current[id]; + }, duration); + }, []); + + const dismiss = useCallback((id) => { + setToasts((prev) => prev.filter((t) => t.id !== id)); + if (timersRef.current[id]) { clearTimeout(timersRef.current[id]); delete timersRef.current[id]; } + }, []); + + return ( + + {children} + {typeof document !== 'undefined' && createPortal( +
+ {toasts.map((t) => ( +
dismiss(t.id)}> + {t.message} +
+ ))} +
, + document.body + )} +
+ ); +} diff --git a/lib/auth.js b/lib/auth.js index 87a348f..8a5d1e5 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -40,3 +40,13 @@ export async function requireAdmin(user) { return Response.json({ error: '服务器错误' }, { status: 500 }); } } + +// 判断是否为第一个注册的用户(超级管理员,按创建时间最早,兼容 UUID/自增 id 数据库) +export async function isFirstUser(userId) { + try { + const r = await pool.query('SELECT id FROM users ORDER BY created_at ASC, id ASC LIMIT 1'); + return r.rows.length > 0 && r.rows[0].id === userId; + } catch { + return false; + } +}