var api = require('../../utils/api'); var posterEditor = require('../../vendor/poster-editor.cjs'); var THEMES = { celebration: { bg: '#ff6b35', ink: '#fffaf4', card: '#25211f', accent: '#ffe568', label: '庆祝派对' }, editorial: { bg: '#f3eadf', ink: '#201d1b', card: '#ffffff', accent: '#285ee8', label: '杂志封面' }, sticker: { bg: '#c9f06b', ink: '#1e2810', card: '#ff72a7', accent: '#ffffff', label: '贴纸手账' } }; Page({ data: { result: null, template: 'celebration', themeList: [ { name: 'celebration', label: '庆祝派对' }, { name: 'editorial', label: '杂志封面' }, { name: 'sticker', label: '贴纸手账' } ], sticker: true, bracketLayout: 'symmetric', bracketView: 'overview', activeTool: 'filter', backgroundPath: '', tools: [ { name: 'filter', label: '滤镜' }, { name: 'text', label: '文字' }, { name: 'sticker', label: '贴纸' }, { name: 'crop', label: '裁剪' }, { name: 'adjust', label: '调整' }, { name: 'layout', label: '布局' }, { name: 'brush', label: '画笔' } ], saving: false }, onLoad: function (options) { this.posterDocument = posterEditor.createDocument({ id: 'run-' + options.id, seed: options.id, width: 750, height: 1000, meta: { sourceType: 'pk-run', sourceId: options.id } }); var cached = xhs.getStorageSync('pk_last_result'); this.runId = options.id; if (cached && cached.id === options.id) this.setData({ result: cached }); else this.load(); }, onReady: function () { if (this.data.result) this.drawPoster(); }, load: function () { var self = this; api.ensureLogin().then(function () { return api.request('/api/v1/me/results/' + self.runId); }).then(function (result) { self.setData({ result: result }); self.drawPoster(); }).catch(function (error) { xhs.showModal({ title: '读取失败', content: error.message || '请稍后重试', showCancel: false }); }); }, pickTemplate: function (event) { this.setData({ template: event.currentTarget.dataset.name }); this.drawPoster(); }, toggleSticker: function () { this.setData({ sticker: !this.data.sticker }); this.drawPoster(); }, pickBracketLayout: function (event) { this.setData({ bracketLayout: event.currentTarget.dataset.layout }); this.drawPoster(); }, pickBracketView: function (event) { this.setData({ bracketView: event.currentTarget.dataset.view }); this.drawPoster(); }, pickTool: function (event) { this.setData({ activeTool: event.currentTarget.dataset.tool }); }, chooseBackground: function () { var self = this; xhs.chooseImage({ count: 1, success: function (result) { var path = result.tempFilePaths && result.tempFilePaths[0]; if (path) { self.setData({ backgroundPath: path }); self.drawPoster(); } } }); }, drawPoster: function () { var result = this.data.result; if (!result) return; var theme = THEMES[this.data.template]; var isCelebration = this.data.template === 'celebration'; var showSticker = this.data.sticker; var layout = this.data.bracketLayout; var view = this.data.bracketView; var backgroundPath = this.data.backgroundPath; xhs.createSelectorQuery().select('.poster').boundingClientRect(function (rect) { var ctx = xhs.createCanvasContext('poster'); if (rect) ctx.scale(rect.width / 750, rect.height / 1000); ctx.setFillStyle(theme.bg); ctx.fillRect(0, 0, 750, 1000); if (backgroundPath) { ctx.drawImage(backgroundPath, 0, 0, 750, 1000); ctx.setGlobalAlpha(.82); ctx.setFillStyle(theme.bg); ctx.fillRect(0, 0, 750, 1000); ctx.setGlobalAlpha(1); } ctx.setFillStyle(theme.ink); ctx.setFontSize(24); ctx.fillText('PK 研究所 / MY PICK', 54, 72); ctx.setFontSize(58); ctx.fillText('我的冠军是', 54, 156); ctx.setFillStyle(theme.card); ctx.fillRect(30, 190, 690, 610); ctx.setFillStyle(isCelebration ? '#fffaf4' : theme.ink); ctx.setFontSize(22); ctx.fillText(result.test_title, 54, 235); drawBracket(ctx, result, layout, view, theme, isCelebration); if (showSticker) { ctx.setFillStyle(theme.accent); ctx.beginPath(); ctx.arc(645, 125, 58, 0, Math.PI * 2); ctx.fill(); ctx.setFillStyle(theme.card); ctx.setFontSize(24); ctx.fillText('WIN', 615, 134); } ctx.setFillStyle(theme.ink); ctx.setFontSize(20); ctx.fillText('长按保存,把答案分享给同频的人', 54, 956); ctx.draw(); }).exec(); }, savePoster: function () { var self = this; this.setData({ saving: true }); xhs.canvasToTempFilePath({ canvasId: 'poster', destWidth: 750, destHeight: 1000, success: function (file) { xhs.saveImageToPhotosAlbum({ filePath: file.tempFilePath, success: function () { xhs.showToast({ title: '已保存到相册' }); }, fail: function () { xhs.showToast({ title: '保存失败,请检查相册权限', icon: 'none' }); }, complete: function () { self.setData({ saving: false }); } }); }, fail: function () { self.setData({ saving: false }); xhs.showToast({ title: '海报生成失败', icon: 'none' }); } }); }, replay: function () { xhs.redirectTo({ url: '/pages/test/test?slug=' + this.data.result.test_slug }); } }); function drawBracket(ctx, result, layout, view, theme, isCelebration) { var decisions = result.decisions || []; var names = result.candidate_names || {}; if (!decisions.length) { ctx.setFillStyle(isCelebration ? '#fffaf4' : theme.ink); ctx.setFontSize(72); ctx.fillText(result.champion_name, 70, 480); return; } var maxRound = decisions.reduce(function (max, item) { return Math.max(max, item.round); }, 1); var minimumRound = view === 'final' ? maxRound : view === 'top8' ? Math.max(1, maxRound - 2) : view === 'top16' ? Math.max(1, maxRound - 3) : 1; var visible = decisions.filter(function (item) { return item.round >= minimumRound; }); if (view === 'left' || view === 'right') { visible = visible.filter(function (item, index) { return item.round === maxRound || index % 2 === (view === 'left' ? 0 : 1); }); } var grouped = {}; visible.forEach(function (item) { (grouped[item.round] || (grouped[item.round] = [])).push(item); }); var positions = {}; Object.keys(grouped).forEach(function (roundKey) { var round = Number(roundKey), items = grouped[round], count = items.length; items.forEach(function (item, index) { var centered = layout === 'symmetric'; var side = centered && round < maxRound && index % 2 ? 1 : -1; var depth = maxRound - round; var x = centered ? 375 + side * Math.min(250, 82 + depth * 68) : 64 + (round - minimumRound) * (590 / Math.max(1, maxRound - minimumRound)); var y = 300 + (index + 1) * (440 / (count + 1)); positions[item.winner_id + ':' + item.round] = { x: x, y: y, side: side, item: item }; }); }); ctx.setLineWidth(3); ctx.setStrokeStyle(theme.accent); Object.keys(positions).forEach(function (key) { var pos = positions[key], parent = null; visible.forEach(function (candidate) { if (!parent && candidate.round > pos.item.round && (candidate.left_id === pos.item.winner_id || candidate.right_id === pos.item.winner_id)) parent = positions[candidate.winner_id + ':' + candidate.round]; }); if (parent) { ctx.beginPath(); ctx.moveTo(pos.x, pos.y); ctx.lineTo((pos.x + parent.x) / 2, pos.y); ctx.lineTo((pos.x + parent.x) / 2, parent.y); ctx.lineTo(parent.x, parent.y); ctx.stroke(); } }); Object.keys(positions).forEach(function (key) { var pos = positions[key], label = names[pos.item.winner_id] || result.champion_name; ctx.setFillStyle(pos.item.round === maxRound ? theme.accent : theme.bg); ctx.fillRect(pos.x - 58, pos.y - 21, 116, 42); ctx.setFillStyle(theme.ink); ctx.setFontSize(label.length > 6 ? 14 : 17); ctx.fillText(label.slice(0, 8), pos.x - 48, pos.y + 6); }); ctx.setFillStyle(theme.ink); ctx.setFontSize(24); ctx.fillText('冠军 ' + result.champion_name, 54, 850); }