结果读取失败
{error}
'use client';
import { motion } from 'motion/react';
import { useRouter } from 'next/navigation';
import { useCallback, useEffect, useRef, useState } from 'react';
import { apiRequest, ensureLogin } from '../lib/api';
const THEMES = {
celebration: ['#ff6038', '#fffaf4', '#25211f', '#ffe568'],
editorial: ['#f3eadf', '#201d1b', '#ffffff', '#285ee8'],
notebook: ['#c9f06b', '#1e2810', '#ff72a7', '#ffffff']
};
const TOOLS = [
['filter', '滤镜'], ['text', '文字'], ['sticker', '贴纸'], ['crop', '裁剪'],
['adjust', '调整'], ['layout', '布局'], ['brush', '画笔']
];
const VIEWS = [['overview', '总览'], ['left', '左半区'], ['right', '右半区'], ['top16', '16 强'], ['top8', '8 强'], ['final', '决赛']];
export default function PosterEditor({ runId }) {
const router = useRouter();
const canvasRef = useRef(null);
const [result, setResult] = useState(null);
const [error, setError] = useState('');
const [theme, setTheme] = useState('celebration');
const [layout, setLayout] = useState('symmetric');
const [view, setView] = useState('overview');
const [tool, setTool] = useState('filter');
const [sticker, setSticker] = useState(true);
const [caption, setCaption] = useState('');
const [background, setBackground] = useState(null);
const [adjust, setAdjust] = useState({ brightness: 100, contrast: 100, saturate: 100 });
useEffect(() => {
async function loadResult() {
const cached = sessionStorage.getItem('pk_last_result');
if (cached) {
const parsed = JSON.parse(cached);
if (parsed.id === runId) return parsed;
}
await ensureLogin();
return apiRequest(`/api/v1/me/results/${runId}`);
}
loadResult().then(setResult).catch(error => setError(error.message));
}, [runId]);
const draw = useCallback(() => {
if (!result || !canvasRef.current) return;
const canvas = canvasRef.current;
const ctx = canvas.getContext('2d');
const colors = THEMES[theme];
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = colors[0];
ctx.fillRect(0, 0, 750, 1000);
if (background) {
ctx.save();
ctx.filter = `brightness(${adjust.brightness}%) contrast(${adjust.contrast}%) saturate(${adjust.saturate}%)`;
coverImage(ctx, background, 0, 0, 750, 1000);
ctx.restore();
ctx.fillStyle = `${colors[0]}d8`;
ctx.fillRect(0, 0, 750, 1000);
}
ctx.fillStyle = colors[1];
ctx.font = '700 24px system-ui';
ctx.fillText('PK 研究所 / BRACKET', 42, 62);
ctx.font = '900 48px system-ui';
ctx.fillText(result.test_title, 42, 125);
ctx.fillStyle = colors[2];
ctx.fillRect(28, 165, 694, 690);
drawBracket(ctx, result, colors, layout, view);
if (caption) {
ctx.fillStyle = colors[3];
roundRect(ctx, 42, 870, Math.min(650, 50 + caption.length * 34), 52, 26);
ctx.fill();
ctx.fillStyle = colors[2];
ctx.font = '800 25px system-ui';
ctx.fillText(caption.slice(0, 20), 67, 904);
} else {
ctx.fillStyle = colors[1];
ctx.font = '800 25px system-ui';
ctx.fillText(`冠军 ${result.champion_name}`, 42, 910);
}
if (sticker) {
ctx.fillStyle = colors[3];
ctx.beginPath(); ctx.arc(660, 100, 48, 0, Math.PI * 2); ctx.fill();
ctx.fillStyle = colors[2]; ctx.font = '900 22px system-ui'; ctx.fillText('WIN', 635, 108);
}
ctx.fillStyle = colors[1]; ctx.font = '18px system-ui';
ctx.fillText('保存分享图,看看朋友的晋级路线', 42, 962);
}, [adjust, background, caption, layout, result, sticker, theme, view]);
useEffect(() => { draw(); }, [draw]);
function pickBackground(event) {
const file = event.target.files?.[0];
if (!file) return;
const image = new Image();
image.onload = () => setBackground(image);
image.src = URL.createObjectURL(file);
}
function save() {
const link = document.createElement('a');
link.download = `我的PK冠军-${result.champion_name}.png`;
link.href = canvasRef.current.toDataURL('image/png');
link.click();
}
if (error) return {error}结果读取失败