feat: toast notifications, canvas captcha, super admin, mobile fixes
- Toast system (ToastProvider + useToast): right-top colored toasts replace alert for publish success/error; CSS + layout mount - Captcha as canvas image (noise + distortion) in NewPost/ReplyList/ register modal — blocks bots, click to refresh - Super admin: first registered user cannot be demoted (isFirstUser by created_at, DB-agnostic) - Admins can delete others' posts/replies (UI buttons shown for admin) - Mobile: post-actions wrap + compact, feed excerpt 2.5 lines - Fix: missing sun icon in Icons (dark mode toggle showed nothing) - Feed long posts: gradient fade mask instead of line-clamp
This commit is contained in:
1 parent
e860978aef
commit
b2368f68df
12 files changed
+262
-31
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
// PUT /api/users/[id] (更新资料), PUT /api/users/[id]/role (改角色)
|
// PUT /api/users/[id] (更新资料), PUT /api/users/[id]/role (改角色)
|
||||||
import pool from '@/lib/db';
|
import pool from '@/lib/db';
|
||||||
import { getUser, requireAdmin } from '@/lib/auth';
|
import { getUser, requireAdmin, isFirstUser } from '@/lib/auth';
|
||||||
|
|
||||||
export async function PUT(req, { params }) {
|
export async function PUT(req, { params }) {
|
||||||
const { id } = params;
|
const { id } = params;
|
||||||
@@ -22,6 +22,10 @@ export async function PUT(req, { params }) {
|
|||||||
if (id === user.id) {
|
if (id === user.id) {
|
||||||
return Response.json({ error: '不能修改自己的角色' }, { status: 400 });
|
return Response.json({ error: '不能修改自己的角色' }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
// 超级管理员(第一个用户)不能被降级
|
||||||
|
if (role !== 'admin' && await isFirstUser(id)) {
|
||||||
|
return Response.json({ error: '不能降级超级管理员' }, { status: 403 });
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await pool.query('UPDATE users SET role = $1 WHERE id = $2', [role, id]);
|
await pool.query('UPDATE users SET role = $1 WHERE id = $2', [role, id]);
|
||||||
return Response.json({ success: true });
|
return Response.json({ success: true });
|
||||||
|
|||||||
+70
-5
@@ -1179,6 +1179,17 @@ svg {
|
|||||||
width: 20px;
|
width: 20px;
|
||||||
height: 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 {
|
#feed .post-content {
|
||||||
display: -webkit-box;
|
position: relative;
|
||||||
-webkit-line-clamp: 3;
|
max-height: 5.1em; /* 约 3 行(line-height 1.7) */
|
||||||
-webkit-box-orient: vertical;
|
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
text-overflow: ellipsis;
|
|
||||||
max-height: none;
|
|
||||||
line-height: 1.7;
|
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 {
|
.loading-screen {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
@@ -2064,3 +2084,48 @@ a.btn-secondary {
|
|||||||
#adminContent td:nth-child(2) {
|
#adminContent td:nth-child(2) {
|
||||||
white-space: normal !important;
|
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); }
|
||||||
|
}
|
||||||
+5
-2
@@ -1,6 +1,7 @@
|
|||||||
// 全局布局
|
// 全局布局
|
||||||
import './globals.css';
|
import './globals.css';
|
||||||
import CustomCssLoader from '@/components/CustomCssLoader';
|
import CustomCssLoader from '@/components/CustomCssLoader';
|
||||||
|
import { ToastProvider } from '@/components/Toast';
|
||||||
|
|
||||||
export const metadata = {
|
export const metadata = {
|
||||||
// 初始标题由客户端 AppProvider 接管(加载时轮换 Loading.,完成后显示论坛名)
|
// 初始标题由客户端 AppProvider 接管(加载时轮换 Loading.,完成后显示论坛名)
|
||||||
@@ -28,8 +29,10 @@ export default function RootLayout({ children }) {
|
|||||||
/>
|
/>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<CustomCssLoader />
|
<ToastProvider>
|
||||||
{children}
|
<CustomCssLoader />
|
||||||
|
{children}
|
||||||
|
</ToastProvider>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -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 (
|
||||||
|
<canvas
|
||||||
|
ref={canvasRef}
|
||||||
|
width={120}
|
||||||
|
height={40}
|
||||||
|
onClick={onRefresh}
|
||||||
|
title="看不清?点击刷新"
|
||||||
|
style={{ cursor: 'pointer', borderRadius: 6, border: '1px solid var(--glass-border)', verticalAlign: 'middle' }}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
+17
-3
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
// 信息流:帖子列表 + 排序 tab + 发布新帖按钮
|
// 信息流:帖子列表 + 排序 tab + 发布新帖按钮
|
||||||
import { useEffect, useState, useCallback } from 'react';
|
import { useEffect, useState, useCallback, useRef } from 'react';
|
||||||
import { API } from '@/lib/api';
|
import { API } from '@/lib/api';
|
||||||
import { useApp } from './AppProvider';
|
import { useApp } from './AppProvider';
|
||||||
import { Icon } from './Icons';
|
import { Icon } from './Icons';
|
||||||
@@ -21,6 +21,7 @@ export default function Feed({ onOpenModal, onReport }) {
|
|||||||
const [page, setPage] = useState(1);
|
const [page, setPage] = useState(1);
|
||||||
const [totalPages, setTotalPages] = useState(1);
|
const [totalPages, setTotalPages] = useState(1);
|
||||||
const [viewerSrc, setViewerSrc] = useState(null);
|
const [viewerSrc, setViewerSrc] = useState(null);
|
||||||
|
const postListRef = useRef(null);
|
||||||
const PAGE_SIZE = 20;
|
const PAGE_SIZE = 20;
|
||||||
|
|
||||||
const load = useCallback(async (targetPage) => {
|
const load = useCallback(async (targetPage) => {
|
||||||
@@ -39,6 +40,19 @@ export default function Feed({ onOpenModal, onReport }) {
|
|||||||
|
|
||||||
useEffect(() => { load(page); }, [load]);
|
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]);
|
useEffect(() => { setPage(1); }, [sort]);
|
||||||
|
|
||||||
@@ -69,7 +83,7 @@ export default function Feed({ onOpenModal, onReport }) {
|
|||||||
<Icon name="plus" /> 发布新帖
|
<Icon name="plus" /> 发布新帖
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div id="postList">
|
<div id="postList" ref={postListRef}>
|
||||||
{posts === null ? (
|
{posts === null ? (
|
||||||
<div style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}>加载中...</div>
|
<div style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}>加载中...</div>
|
||||||
) : error ? (
|
) : error ? (
|
||||||
@@ -119,7 +133,7 @@ export default function Feed({ onOpenModal, onReport }) {
|
|||||||
}}>
|
}}>
|
||||||
<Icon name="flag" size={14} /> 举报
|
<Icon name="flag" size={14} /> 举报
|
||||||
</button>
|
</button>
|
||||||
{currentUser && currentUser.id === p.user_id && (
|
{currentUser && (currentUser.id === p.user_id || currentUser.role === 'admin') && (
|
||||||
<button className="action-delete" onClick={(e) => {
|
<button className="action-delete" onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
handleDelete(p.id);
|
handleDelete(p.id);
|
||||||
|
|||||||
@@ -32,6 +32,11 @@ const PATHS = {
|
|||||||
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
|
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
|
||||||
</>
|
</>
|
||||||
),
|
),
|
||||||
|
sun: (
|
||||||
|
<>
|
||||||
|
<circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/>
|
||||||
|
</>
|
||||||
|
),
|
||||||
logout: (
|
logout: (
|
||||||
<>
|
<>
|
||||||
<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/>
|
<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { useState, useEffect } from 'react';
|
|||||||
import { API, generateCaptcha } from '@/lib/api';
|
import { API, generateCaptcha } from '@/lib/api';
|
||||||
import { useApp } from './AppProvider';
|
import { useApp } from './AppProvider';
|
||||||
import { Icon } from './Icons';
|
import { Icon } from './Icons';
|
||||||
|
import CaptchaImage from './CaptchaImage';
|
||||||
|
|
||||||
export default function Modals({ modal, onClose, reportPostId }) {
|
export default function Modals({ modal, onClose, reportPostId }) {
|
||||||
const { login, register, currentUser, refresh } = useApp();
|
const { login, register, currentUser, refresh } = useApp();
|
||||||
@@ -106,11 +107,13 @@ export default function Modals({ modal, onClose, reportPostId }) {
|
|||||||
<input type="text" placeholder="用户名" value={regUsername} onChange={(e) => setRegUsername(e.target.value)} />
|
<input type="text" placeholder="用户名" value={regUsername} onChange={(e) => setRegUsername(e.target.value)} />
|
||||||
<input type="email" placeholder="邮箱" value={regEmail} onChange={(e) => setRegEmail(e.target.value)} />
|
<input type="email" placeholder="邮箱" value={regEmail} onChange={(e) => setRegEmail(e.target.value)} />
|
||||||
<input type="password" placeholder="密码(至少6位)" value={regPassword} onChange={(e) => setRegPassword(e.target.value)} />
|
<input type="password" placeholder="密码(至少6位)" value={regPassword} onChange={(e) => setRegPassword(e.target.value)} />
|
||||||
<div className="captcha-row">
|
<div className="captcha-row" style={{ display: 'flex', alignItems: 'center', gap: 10, margin: '12px 0' }}>
|
||||||
<span onClick={() => setRegCaptcha(generateCaptcha())} style={{ cursor: 'pointer' }}>
|
{regCaptcha ? (
|
||||||
{regCaptcha ? regCaptcha.question : ''}
|
<CaptchaImage captcha={regCaptcha} onRefresh={() => { setRegCaptcha(generateCaptcha()); setRegCaptchaInput(''); }} />
|
||||||
</span>
|
) : (
|
||||||
<input type="text" placeholder="答案" style={{ width: 80 }} value={regCaptchaInput} onChange={(e) => setRegCaptchaInput(e.target.value)} />
|
<span style={{ color: 'var(--text-light)' }}>验证码加载中...</span>
|
||||||
|
)}
|
||||||
|
<input type="text" placeholder="答案" style={{ width: 80, background: 'var(--bg)', color: 'var(--text)', border: '1px solid var(--border)', padding: '8px 12px', borderRadius: 4 }} value={regCaptchaInput} onChange={(e) => setRegCaptchaInput(e.target.value)} />
|
||||||
</div>
|
</div>
|
||||||
<button className="btn-primary" style={{ width: '100%' }} onClick={handleRegister}>注册</button>
|
<button className="btn-primary" style={{ width: '100%' }} onClick={handleRegister}>注册</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+14
-9
@@ -5,9 +5,12 @@ import { useState, useEffect, useRef } from 'react';
|
|||||||
import { API, generateCaptcha, uploadImage } from '@/lib/api';
|
import { API, generateCaptcha, uploadImage } from '@/lib/api';
|
||||||
import { useApp } from './AppProvider';
|
import { useApp } from './AppProvider';
|
||||||
import { Icon } from './Icons';
|
import { Icon } from './Icons';
|
||||||
|
import { useToast } from './Toast';
|
||||||
|
import CaptchaImage from './CaptchaImage';
|
||||||
|
|
||||||
export default function NewPost() {
|
export default function NewPost() {
|
||||||
const { currentUser, navigate, refresh } = useApp();
|
const { currentUser, navigate, refresh } = useApp();
|
||||||
|
const { toast } = useToast();
|
||||||
const [title, setTitle] = useState('');
|
const [title, setTitle] = useState('');
|
||||||
const [content, setContent] = useState('');
|
const [content, setContent] = useState('');
|
||||||
const [images, setImages] = useState([]); // 上传后的 URL 列表
|
const [images, setImages] = useState([]); // 上传后的 URL 列表
|
||||||
@@ -57,13 +60,13 @@ export default function NewPost() {
|
|||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
if (!currentUser || !currentUser.id) {
|
if (!currentUser || !currentUser.id) {
|
||||||
alert('请先登录');
|
toast('请先登录', 'error');
|
||||||
navigate('feed');
|
navigate('feed');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!content.trim()) { alert('请填写内容'); return; }
|
if (!content.trim()) { toast('请填写内容', 'error'); return; }
|
||||||
if (!captcha || parseInt(captchaInput) !== captcha.answer) {
|
if (!captcha || parseInt(captchaInput) !== captcha.answer) {
|
||||||
alert('验证码错误,请重新计算');
|
toast('验证码错误,请重新计算', 'error');
|
||||||
setCaptcha(generateCaptcha());
|
setCaptcha(generateCaptcha());
|
||||||
setCaptchaInput('');
|
setCaptchaInput('');
|
||||||
return;
|
return;
|
||||||
@@ -71,11 +74,11 @@ export default function NewPost() {
|
|||||||
try {
|
try {
|
||||||
await API.createPost(title.trim() || '无标题', content.trim(), images);
|
await API.createPost(title.trim() || '无标题', content.trim(), images);
|
||||||
API.logEvent('create_post').catch(() => {});
|
API.logEvent('create_post').catch(() => {});
|
||||||
alert('发布成功!');
|
toast('发布成功', 'success');
|
||||||
navigate('feed');
|
navigate('feed');
|
||||||
refresh();
|
refresh();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert('发布失败:' + err.message);
|
toast('发布失败:' + err.message, 'error');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -125,10 +128,12 @@ export default function NewPost() {
|
|||||||
style={{ display: 'none' }}
|
style={{ display: 'none' }}
|
||||||
onChange={(e) => { if (e.target.files) handleFiles(Array.from(e.target.files)); }}
|
onChange={(e) => { if (e.target.files) handleFiles(Array.from(e.target.files)); }}
|
||||||
/>
|
/>
|
||||||
<div className="captcha-row" style={{ margin: '12px 0' }}>
|
<div className="captcha-row" style={{ margin: '12px 0', display: 'flex', alignItems: 'center', gap: 10 }}>
|
||||||
<span onClick={() => setCaptcha(generateCaptcha())} style={{ cursor: 'pointer' }}>
|
{captcha ? (
|
||||||
{captcha ? captcha.question : ''}
|
<CaptchaImage captcha={captcha} onRefresh={() => { setCaptcha(generateCaptcha()); setCaptchaInput(''); }} />
|
||||||
</span>
|
) : (
|
||||||
|
<span style={{ color: 'var(--text-light)' }}>验证码加载中...</span>
|
||||||
|
)}
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="答案"
|
placeholder="答案"
|
||||||
|
|||||||
@@ -50,7 +50,8 @@ export default function PostDetail({ postId }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const time = post.created_at ? new Date(post.created_at).toLocaleString('zh-CN') : '';
|
const time = post.created_at ? new Date(post.created_at).toLocaleString('zh-CN') : '';
|
||||||
const canDelete = currentUser && currentUser.id === post.user_id;
|
// 作者本人 或 管理员 可删除/编辑
|
||||||
|
const canDelete = currentUser && (currentUser.id === post.user_id || currentUser.role === 'admin');
|
||||||
const canEdit = currentUser && currentUser.id === post.user_id;
|
const canEdit = currentUser && currentUser.id === post.user_id;
|
||||||
|
|
||||||
const handleDelete = async () => {
|
const handleDelete = async () => {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { API, generateCaptcha } from '@/lib/api';
|
|||||||
import { useApp } from './AppProvider';
|
import { useApp } from './AppProvider';
|
||||||
import { Icon } from './Icons';
|
import { Icon } from './Icons';
|
||||||
import { renderMarkdown } from '@/lib/markdown';
|
import { renderMarkdown } from '@/lib/markdown';
|
||||||
|
import CaptchaImage from './CaptchaImage';
|
||||||
|
|
||||||
function avatar(username) {
|
function avatar(username) {
|
||||||
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') +
|
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') +
|
||||||
@@ -84,7 +85,7 @@ export default function ReplyList({ postId, onRefresh }) {
|
|||||||
{r.username || '匿名用户'}
|
{r.username || '匿名用户'}
|
||||||
</span>
|
</span>
|
||||||
<span className="reply-time">{rTime}</span>
|
<span className="reply-time">{rTime}</span>
|
||||||
{currentUser && currentUser.id === r.user_id && (
|
{currentUser && (currentUser.id === r.user_id || currentUser.role === 'admin') && (
|
||||||
<button className="btn-sm btn-danger reply-delete-btn" onClick={() => handleDelete(r.id)}>
|
<button className="btn-sm btn-danger reply-delete-btn" onClick={() => handleDelete(r.id)}>
|
||||||
<Icon name="trash" size={12} /> 删除
|
<Icon name="trash" size={12} /> 删除
|
||||||
</button>
|
</button>
|
||||||
@@ -107,10 +108,12 @@ export default function ReplyList({ postId, onRefresh }) {
|
|||||||
onChange={(e) => setContent(e.target.value)}
|
onChange={(e) => setContent(e.target.value)}
|
||||||
onKeyDown={(e) => { if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) handleSubmit(); }}
|
onKeyDown={(e) => { if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) handleSubmit(); }}
|
||||||
/>
|
/>
|
||||||
<div className="captcha-row" style={{ margin: '10px 0' }}>
|
<div className="captcha-row" style={{ margin: '10px 0', display: 'flex', alignItems: 'center', gap: 10 }}>
|
||||||
<span onClick={() => setCaptcha(generateCaptcha())} style={{ cursor: 'pointer' }}>
|
{captcha ? (
|
||||||
{captcha ? captcha.question : ''}
|
<CaptchaImage captcha={captcha} onRefresh={() => { setCaptcha(generateCaptcha()); setCaptchaInput(''); }} />
|
||||||
</span>
|
) : (
|
||||||
|
<span style={{ color: 'var(--text-light)' }}>验证码加载中...</span>
|
||||||
|
)}
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="答案"
|
placeholder="答案"
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
// Toast 通知系统:右上角弹出,自动消失(替代 alert)
|
||||||
|
// 用法:const { toast } = useToast(); toast('发布成功', 'success');
|
||||||
|
import { createContext, useContext, useState, useCallback, useRef } from 'react';
|
||||||
|
import { createPortal } from 'react-dom';
|
||||||
|
|
||||||
|
const ToastContext = createContext(null);
|
||||||
|
|
||||||
|
export function useToast() {
|
||||||
|
return useContext(ToastContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
let idCounter = 0;
|
||||||
|
|
||||||
|
export function ToastProvider({ children }) {
|
||||||
|
const [toasts, setToasts] = useState([]);
|
||||||
|
const timersRef = useRef({});
|
||||||
|
|
||||||
|
const toast = useCallback((message, type = 'success', duration = 3000) => {
|
||||||
|
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 (
|
||||||
|
<ToastContext.Provider value={{ toast }}>
|
||||||
|
{children}
|
||||||
|
{typeof document !== 'undefined' && createPortal(
|
||||||
|
<div className="toast-container">
|
||||||
|
{toasts.map((t) => (
|
||||||
|
<div key={t.id} className={'toast toast-' + t.type} onClick={() => dismiss(t.id)}>
|
||||||
|
{t.message}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>,
|
||||||
|
document.body
|
||||||
|
)}
|
||||||
|
</ToastContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
+10
@@ -40,3 +40,13 @@ export async function requireAdmin(user) {
|
|||||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user